2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.oic.simulator.server.test;
19 import java.util.Vector;
21 import org.oic.simulator.InvalidArgsException;
22 import org.oic.simulator.SimulatorException;
23 import org.oic.simulator.SimulatorManager;
24 import org.oic.simulator.server.SimulatorCollectionResource;
25 import org.oic.simulator.server.SimulatorResource;
26 import org.oic.simulator.test.ExceptionType;
28 import junit.framework.TestCase;
30 public class SimulatorCollectionResourceTest extends TestCase {
32 System.loadLibrary("SimulatorManager");
33 System.loadLibrary("RamlParser");
34 System.loadLibrary("oc");
35 System.loadLibrary("oc_logger");
36 System.loadLibrary("octbstack");
39 private static final String COLLECTION_RES_RAML = "./ramls/oic.d.airconditioner.raml";
40 private SimulatorCollectionResource collectionResource = null;
42 protected void setUp() throws Exception {
44 // collectionResource = (SimulatorCollectionResource)
45 // SimulatorManager.createResource(
46 // COLLECTION_RES_RAML);
47 collectionResource = (SimulatorCollectionResource) SimulatorManager
48 .createResource(SimulatorResource.Type.COLLECTION,
49 "test-collection", "/test/collection",
53 protected void tearDown() throws Exception {
55 collectionResource = null;
58 public void testAddChildResource_P01() {
59 SimulatorResource childResource = null;
62 String name = "child-resource";
63 String uri = "/child/resource";
64 String resType = "child.resource";
66 childResource = SimulatorManager.createResource(
67 SimulatorResource.Type.SINGLE, name, uri, resType);
68 } catch (InvalidArgsException e1) {
70 } catch (SimulatorException e1) {
74 if (null == childResource)
77 Vector<SimulatorResource> result = null;
79 collectionResource.addChildResource(childResource);
80 result = collectionResource.getChildResource();
81 } catch (InvalidArgsException e) {
83 } catch (SimulatorException e) {
87 assertNotNull(result);
88 assertEquals(1, result.size());
91 public void testAddChildResource_P02() {
92 SimulatorResource childResource = null;
95 String name = "child-resource";
96 String uri = "/child/resource";
97 String resType = "child.resource";
99 childResource = SimulatorManager.createResource(
100 SimulatorResource.Type.COLLECTION, name, uri, resType);
101 } catch (InvalidArgsException e1) {
102 e1.printStackTrace();
103 } catch (SimulatorException e1) {
104 e1.printStackTrace();
107 if (null == childResource)
110 Vector<SimulatorResource> result = null;
112 collectionResource.addChildResource(childResource);
113 result = collectionResource.getChildResource();
114 } catch (InvalidArgsException e) {
116 } catch (SimulatorException e) {
120 assertNotNull(result);
121 assertEquals(1, result.size());
124 public void testAddChildResource_P03() {
125 SimulatorResource childResource1 = null;
126 SimulatorResource childResource2 = null;
129 String name = "child-resource";
130 String uri = "/child/resource";
131 String resType = "child.resource";
133 childResource1 = SimulatorManager.createResource(
134 SimulatorResource.Type.SINGLE, name, uri, resType);
135 childResource2 = SimulatorManager.createResource(
136 SimulatorResource.Type.COLLECTION, name + "-2", uri + "/2",
138 } catch (InvalidArgsException e1) {
139 e1.printStackTrace();
140 } catch (SimulatorException e1) {
141 e1.printStackTrace();
144 if (null == childResource1 || null == childResource2)
147 Vector<SimulatorResource> result = null;
149 collectionResource.addChildResource(childResource1);
150 collectionResource.addChildResource(childResource2);
151 result = collectionResource.getChildResource();
152 } catch (InvalidArgsException e) {
154 } catch (SimulatorException e) {
158 assertNotNull(result);
159 assertEquals(2, result.size());
162 public void testAddChildResource_N01() {
163 ExceptionType exType = ExceptionType.UNKNOWN;
166 collectionResource.addChildResource(null);
167 } catch (InvalidArgsException e) {
168 exType = ExceptionType.INVALID_ARGS;
169 } catch (SimulatorException e) {
170 exType = ExceptionType.SIMULATOR;
173 assertTrue(exType == ExceptionType.INVALID_ARGS);
176 public void testRemoveChildResource_P01() {
177 SimulatorResource childResource = null;
178 Vector<SimulatorResource> result = null;
181 String name = "child-resource";
182 String uri = "/child/resource";
183 String resType = "child.resource";
185 childResource = SimulatorManager.createResource(
186 SimulatorResource.Type.COLLECTION, name, uri, resType);
187 collectionResource.addChildResource(childResource);
188 result = collectionResource.getChildResource();
189 } catch (InvalidArgsException e1) {
190 e1.printStackTrace();
191 } catch (SimulatorException e1) {
192 e1.printStackTrace();
195 if (null == childResource || null == result)
198 int count = result.size();
200 collectionResource.removeChildResource(childResource);
201 result = collectionResource.getChildResource();
202 } catch (InvalidArgsException e) {
204 } catch (SimulatorException e) {
208 assertTrue((count == 1 && result == null)
209 || (count > 1 && result.size() == count - 1));
212 public void testRemoveChildResource_N01() {
213 ExceptionType exType = ExceptionType.UNKNOWN;
216 collectionResource.removeChildResource(null);
217 } catch (InvalidArgsException e) {
218 exType = ExceptionType.INVALID_ARGS;
219 } catch (SimulatorException e) {
220 exType = ExceptionType.SIMULATOR;
223 assertTrue(exType == ExceptionType.INVALID_ARGS);
226 public void testRemoveChildResourceByUri_P01() {
227 SimulatorResource childResource = null;
228 Vector<SimulatorResource> result = null;
231 String name = "child-resource";
232 String uri = "/child/resource";
233 String resType = "child.resource";
235 childResource = SimulatorManager.createResource(
236 SimulatorResource.Type.COLLECTION, name, uri, resType);
237 collectionResource.addChildResource(childResource);
238 result = collectionResource.getChildResource();
239 } catch (InvalidArgsException e1) {
240 e1.printStackTrace();
241 } catch (SimulatorException e1) {
242 e1.printStackTrace();
245 if (null == childResource || null == result)
248 int count = result.size();
250 collectionResource.removeChildResourceByUri(childResource.getURI());
251 result = collectionResource.getChildResource();
252 } catch (InvalidArgsException e) {
254 } catch (SimulatorException e) {
258 assertTrue((count == 1 && result == null)
259 || (count > 1 && result.size() == count - 1));
262 public void testRemoveChildResourceByUri_N01() {
263 ExceptionType exType = ExceptionType.UNKNOWN;
266 collectionResource.removeChildResourceByUri(null);
267 } catch (InvalidArgsException e) {
268 exType = ExceptionType.INVALID_ARGS;
269 } catch (SimulatorException e) {
270 exType = ExceptionType.SIMULATOR;
273 assertTrue(exType == ExceptionType.INVALID_ARGS);
276 public void testRemoveChildResourceByUri_N02() {
277 ExceptionType exType = ExceptionType.UNKNOWN;
281 collectionResource.removeChildResourceByUri(uri);
282 } catch (InvalidArgsException e) {
283 exType = ExceptionType.INVALID_ARGS;
284 } catch (SimulatorException e) {
285 exType = ExceptionType.SIMULATOR;
288 assertTrue(exType == ExceptionType.INVALID_ARGS);