<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
+ <dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-all</artifactId>
+ <version>1.10.19</version>
+ </dependency>
+
</dependencies>
<build>
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * // http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package org.iotivity.cloud.testrdserver;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.mock;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
+import org.iotivity.cloud.base.device.CoapDevice;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.coap.CoapRequest;
+import org.iotivity.cloud.base.protocols.coap.CoapResponse;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.protocols.enums.Observe;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.rdserver.Constants;
+import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
+import org.iotivity.cloud.rdserver.resources.presence.device.DevicePresenceResource;
+import org.iotivity.cloud.util.Cbor;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class DevicePresenceResourceTest {
+ private Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ private ResourceDirectoryResource mRDResource = null;
+ private DevicePresenceResource mockDevicePresenceResource = null;
+ private CoapDevice mockDevice = null;
+ CountDownLatch latch = null;
+ IResponse res;
+
+ @Before
+ public void setUp() throws Exception {
+ res = null;
+ mockDevice = mock(CoapDevice.class);
+ latch = new CountDownLatch(1);
+ mRDResource = new ResourceDirectoryResource();
+ mockDevicePresenceResource = new DevicePresenceResource();
+ // callback mock
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ latch.countDown();
+ res = resp;
+ return resp;
+ }
+ }).when(mockDevice).sendResponse(Mockito.anyObject());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ RDServerTestUtils.resetRDDatabase();
+ }
+
+ private IRequest makePresenceGetRequest(Observe obs) {
+ String query = "di=" + RDServerTestUtils.DI;
+ IRequest request = null;
+ if (obs.compareTo(Observe.SUBSCRIBE) == 0) {
+ request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.DEVICE_PRS_REQ_URI, query);
+ } else if (obs.compareTo(Observe.UNSUBSCRIBE) == 0) {
+ ArrayList<String> devices = new ArrayList<>();
+ devices.add(RDServerTestUtils.DI);
+ HashMap<String, ArrayList<String>> payload = new HashMap<>();
+ payload.put(Constants.DEVICE_LIST_KEY, devices);
+ Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+ request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.DEVICE_PRS_REQ_URI, query,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(payload));
+ }
+ ((CoapRequest) request).setObserve(obs);
+ return request;
+ }
+
+ @Test
+ public void testHandleGetSubscribeRequest_notExistVaule() throws Exception {
+ System.out
+ .println("\t------testHandleGetSubscribeRequest_notExistVaule");
+ IRequest request = makePresenceGetRequest(Observe.SUBSCRIBE);
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ request);
+ // assertion: if the response status is "CONTENT"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ // assertion : if the payload has "di" and "state"
+ assertTrue(arrayHashmapCheck(res, "di"));
+ assertTrue(arrayHashmapCheck(res, "state"));
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
+ HashMap<String, Object> mapData = (HashMap<String, Object>) payloadData
+ .get(0);
+ assertNull(mapData.get("state"));
+ }
+
+ @Test
+ public void testHandleGetSubscribeRequest() throws Exception {
+ System.out.println("\t------testHandleGetSubscribeRequest");
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ IRequest request = makePresenceGetRequest(Observe.SUBSCRIBE);
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ request);
+ // assertion: if the response status is "CONTENT"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ // assertion : if the payload has "di" and "state"
+ assertTrue(arrayHashmapCheck(res, "di"));
+ assertTrue(arrayHashmapCheck(res, "state"));
+ Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
+ HashMap<String, Object> mapData = (HashMap<String, Object>) payloadData
+ .get(0);
+ assertNull(mapData.get("state"));
+ }
+
+ @Test
+ public void testHandleGetUnsubscribeRequest() throws Exception {
+ System.out.println("\t------testHandleGetUnsubscribeRequest");
+ IRequest request = makePresenceGetRequest(Observe.UNSUBSCRIBE);
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ request);
+ // assertion: if the response status is "CONTENT"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ // assertion : if the payload has "di" and "state"
+ assertTrue(arrayHashmapCheck(res, "di"));
+ assertTrue(arrayHashmapCheck(res, "state"));
+ Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
+ HashMap<String, Object> mapData = (HashMap<String, Object>) payloadData
+ .get(0);
+ assertNull(mapData.get("state"));
+ }
+
+ @Test
+ public void testHandlePostRequest_presenceOn() throws Exception {
+ System.out.println("\t------testHandlePostRequest_presenceOn");
+ // POST device presence on
+ HashMap<String, Object> payload = new HashMap<>();
+ Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+ payload.put(Constants.DEVICE_ID, RDServerTestUtils.DI);
+ payload.put(Constants.PRESENCE_STATE, "on");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+ RDServerTestUtils.DEVICE_PRS_REQ_URI, null,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(payload));
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ request);
+ // subscribe request (specific device)
+ IRequest subRequest = makePresenceGetRequest(Observe.SUBSCRIBE);
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ subRequest);
+ // assertion: if the response status is "CONTENT"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ // assertion: if the device status is "on"
+ assertTrue(arrayHashmapCheck(res, "di"));
+ assertTrue(arrayHashmapCheck(res, "state"));
+ Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
+ HashMap<String, Object> mapData = (HashMap<String, Object>) payloadData
+ .get(0);
+ assertTrue(mapData.get("state").equals("on"));
+ }
+
+ @Test
+ public void testHandlePostRequest_presenceOff() throws Exception {
+ System.out.println("\t------testHandlePostRequest_presenceOff");
+ CoapDevice observerDevice = mock(CoapDevice.class);
+ CountDownLatch observerLatch = new CountDownLatch(2);
+ // callback mock for observer Device
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ observerLatch.countDown();
+ // assertion for observer device (subscribe response)
+ if (observerLatch.getCount() == 1) {
+ assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
+ }
+ // assertion for observer device (prs off response)
+ if (observerLatch.getCount() == 0) {
+ assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
+ Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
+ HashMap<String, Object> payloadData = mCbor
+ .parsePayloadFromCbor(resp.getPayload(),
+ HashMap.class);
+ assertTrue(payloadData.get("state").equals("off"));
+ }
+
+ return null;
+ }
+
+ }).when(observerDevice).sendResponse(Mockito.anyObject());
+ // subscribe request (specific device)
+ IRequest subRequest = makePresenceGetRequest(Observe.SUBSCRIBE);
+ mockDevicePresenceResource.onDefaultRequestReceived(observerDevice,
+ subRequest);
+ // POST device presence off
+ HashMap<String, Object> payload = new HashMap<>();
+ Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+ payload.put(Constants.DEVICE_ID, RDServerTestUtils.DI);
+ payload.put(Constants.PRESENCE_STATE, "off");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+ RDServerTestUtils.DEVICE_PRS_REQ_URI, null,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(payload));
+ mockDevicePresenceResource.onDefaultRequestReceived(mockDevice,
+ request);
+ // assertion for resource server device : responseStatus is "CREATED"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(observerLatch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CREATED));
+ }
+
+ private boolean arrayHashmapCheck(IResponse response, String propertyName) {
+ Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(response.getPayload(), ArrayList.class);
+ HashMap<String, Object> mapData = (HashMap<String, Object>) payloadData
+ .get(0);
+ if (mapData.containsKey(propertyName))
+ return true;
+ else
+ return false;
+ }
+
+ private boolean methodCheck(IResponse response,
+ ResponseStatus responseStatus) {
+ if (responseStatus == response.getStatus())
+ return true;
+ else
+ return false;
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * // http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package org.iotivity.cloud.testrdserver;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
+import org.iotivity.cloud.base.device.CoapDevice;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.coap.CoapResponse;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
+import org.iotivity.cloud.rdserver.resources.directory.res.DiscoveryResource;
+import org.iotivity.cloud.util.Cbor;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class DiscoveryResourceTest {
+ private Cbor<ArrayList<Object>> mCbor = new Cbor<>();
+ private ResourceDirectoryResource mRDResource = null;
+ private DiscoveryResource mDiscoveryResource = null;
+ private CoapDevice mockDevice = null;
+ CountDownLatch latch = null;
+ IResponse res;
+
+ @Before
+ public void setUp() throws Exception {
+ res = null;
+ mockDevice = mock(CoapDevice.class);
+ latch = new CountDownLatch(1);
+ mRDResource = new ResourceDirectoryResource();
+ mDiscoveryResource = new DiscoveryResource();
+ // callback mock
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ latch.countDown();
+ res = resp;
+ return resp;
+ }
+ }).when(mockDevice).sendResponse(Mockito.anyObject());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ RDServerTestUtils.resetRDDatabase();
+ }
+
+ @Test
+ public void testHandleGetRequest_notExistVaule() throws Exception {
+ IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.DISCOVERY_REQ_URI,
+ "rt=core.light&di=" + RDServerTestUtils.DI);
+ mDiscoveryResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "CONTENT"
+ // assertion : if the payload is null
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ assertTrue(nullPayloadCheck(res));
+ }
+
+ @Test
+ public void testHandleGetRequest_existValue() throws Exception {
+ IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.DISCOVERY_REQ_URI,
+ "rt=core.light&di=" + RDServerTestUtils.DI);
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ mDiscoveryResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "CONTENT"
+ // assertion : if the payload contains resource info
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ assertTrue(discoverHashmapCheck(res, "di"));
+ assertTrue(discoveredResourceCheck(res, "href"));
+ assertTrue(discoveredResourceCheck(res, "rt"));
+ assertTrue(discoveredResourceCheck(res, "if"));
+ }
+
+ private boolean discoverHashmapCheck(IResponse response,
+ String propertyName) {
+ ArrayList<Object> resourceList = mCbor
+ .parsePayloadFromCbor(response.getPayload(), ArrayList.class);
+ HashMap<Object, Object> firstArray = (HashMap<Object, Object>) resourceList
+ .get(0);
+ if (firstArray.get(propertyName) != null)
+ return true;
+ else
+ return false;
+ }
+
+ private boolean discoveredResourceCheck(IResponse response,
+ String propertyName) {
+ ArrayList<Object> resourceList = mCbor
+ .parsePayloadFromCbor(response.getPayload(), ArrayList.class);
+ HashMap<Object, Object> firstArray = (HashMap<Object, Object>) resourceList
+ .get(0);
+ ArrayList<HashMap<Object, Object>> linkData = (ArrayList<HashMap<Object, Object>>) firstArray
+ .get("links");
+ HashMap<Object, Object> linkMap = linkData.get(0);
+ if (linkMap.get(propertyName) != null)
+ return true;
+ else
+ return false;
+ }
+
+ private boolean methodCheck(IResponse response,
+ ResponseStatus responseStatus) {
+ if (responseStatus == response.getStatus())
+ return true;
+ else
+ return false;
+ }
+
+ private boolean nullPayloadCheck(IResponse response) {
+ ArrayList<Object> payloadData = mCbor
+ .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
+ return (payloadData.isEmpty());
+ }
+}
\ No newline at end of file
--- /dev/null
+package org.iotivity.cloud.testrdserver;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.enums.ContentFormat;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.rdserver.Constants;
+import org.iotivity.cloud.rdserver.db.MongoDB;
+import org.iotivity.cloud.util.Cbor;
+
+public class RDServerTestUtils {
+ public static final String DI = "98f7483c-5a31-4161-ba7e-9c13e0d";
+ public static final String RD_REQ_URI = "/oic/rd";
+ public static final String DISCOVERY_REQ_URI = "/oic/res";
+ public static final String DEVICE_PRS_REQ_URI = "/.well-known/ocf/prs";
+ public static final String RES_PRS_URI = "/oic/ad";
+
+ public static IRequest makePublishRequest() throws Exception {
+ HashMap<Object, Object> payload = new HashMap<>();
+ payload.put(Constants.DEVICE_ID, DI);
+ ArrayList<HashMap<Object, Object>> publishLinks = new ArrayList<>();
+ HashMap<Object, Object> link = new HashMap<>();
+ link.put(Constants.HREF, "/a/light");
+ ArrayList<String> rt = new ArrayList<String>();
+ rt.add("core.light");
+ ArrayList<String> itf = new ArrayList<String>();
+ itf.add("oic.if.baseline");
+ HashMap<String, Object> policy = new HashMap<>();
+ policy.put(Constants.BITMAP, 5);
+ link.put(Constants.RESOURCE_TYPE, rt);
+ link.put(Constants.INTERFACE, itf);
+ link.put(Constants.POLICY, policy);
+ publishLinks.add(link);
+ payload.put(Constants.LINKS, publishLinks);
+ Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
+ IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+ Constants.RD_URI,
+ Constants.RESOURCE_TYPE + "="
+ + Constants.RESOURCE_TYPE_RDPUBLISH,
+ ContentFormat.APPLICATION_CBOR,
+ cbor.encodingPayloadToCbor(payload));
+ return request;
+ }
+
+ public static void resetRDDatabase() throws Exception {
+ MongoDB mongoDB = new MongoDB(Constants.RD_DB_NAME);
+ mongoDB.createTable(Constants.RD_TABLE);
+ mongoDB.createTable(Constants.PRESENCE_TABLE);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * // http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package org.iotivity.cloud.testrdserver;
+
+import org.iotivity.cloud.base.device.CoapDevice;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.coap.CoapResponse;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
+import org.iotivity.cloud.util.Cbor;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
+
+public class ResourceDirectoryResourceTest {
+ private Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
+ private ResourceDirectoryResource mRDResource = null;
+ private CoapDevice mockDevice = null;
+ CountDownLatch latch = null;
+ IResponse res;
+
+ @Before
+ public void setUp() throws Exception {
+ res = null;
+ mockDevice = mock(CoapDevice.class);
+ latch = new CountDownLatch(1);
+ mRDResource = new ResourceDirectoryResource();
+ // callback mock
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ latch.countDown();
+ res = resp;
+ return resp;
+ }
+ }).when(mockDevice).sendResponse(Mockito.anyObject());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ RDServerTestUtils.resetRDDatabase();
+ }
+
+ @Test
+ public void testPostRequest() throws Exception {
+ System.out.println("\t------testPostRequest");
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ // assertion: if the response status is "CHANGED" according to
+ // the resource publication
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CHANGED));
+ // assertion : if the mandatory properties are received in the
+ // response
+ assertTrue(linkCheck(res, "href"));
+ assertTrue(linkCheck(res, "rt"));
+ assertTrue(linkCheck(res, "if"));
+ assertTrue(linkCheck(res, "ins"));
+ assertTrue(hashmapCheck(res, "di"));
+ }
+
+ @Test
+ public void testHandleDeleteRequestByDi_notExistVaule() throws Exception {
+ System.out.println("\t------testHandleDeleteRequestByDi_notExistVaule");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+ RDServerTestUtils.RD_REQ_URI, "di=" + RDServerTestUtils.DI);
+ mRDResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "DELETED" according to the
+ // resource publication
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.DELETED));
+ }
+
+ @Test
+ public void testHandleDeleteRequestByDi_existVaule() throws Exception {
+ System.out.println("\t------testHandleDeleteRequestByDi_existVaule");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+ RDServerTestUtils.RD_REQ_URI, "di=" + RDServerTestUtils.DI);
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ mRDResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "DELETED" according to the
+ // resource publication
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.DELETED));
+ }
+
+ @Test
+ public void testHandleDeleteRequestByIns_notExistVaule() throws Exception {
+ System.out
+ .println("\t------testHandleDeleteRequestByIns_notExistVaule");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+ RDServerTestUtils.RD_REQ_URI,
+ "di=" + RDServerTestUtils.DI + "&ins=1");
+ mRDResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "DELETED" according to the
+ // resource publication
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.DELETED));
+ }
+
+ @Test
+ public void testHandleDeleteRequestByIns_existVaule() throws Exception {
+ System.out.println("\t------testHandleDeleteRequestByIns_existVaule");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+ RDServerTestUtils.RD_REQ_URI,
+ "di=" + RDServerTestUtils.DI + "&ins=1");
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ mRDResource.onDefaultRequestReceived(mockDevice, request);
+ // assertion: if the response status is "DELETED" according to the
+ // resource publication
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.DELETED));
+ }
+
+ private boolean hashmapCheck(IResponse response, String propertyName) {
+ HashMap<String, Object> payloadData = mCbor
+ .parsePayloadFromCbor(response.getPayload(), HashMap.class);
+ if (payloadData.get(propertyName) != null)
+ return true;
+ else
+ return false;
+ }
+
+ private boolean linkCheck(IResponse response, String propertyName) {
+ HashMap<String, Object> payloadData = mCbor
+ .parsePayloadFromCbor(response.getPayload(), HashMap.class);
+ ArrayList<HashMap<Object, Object>> linkData = (ArrayList<HashMap<Object, Object>>) payloadData
+ .get("links");
+ HashMap<Object, Object> linkMap = linkData.get(0);
+ if (linkMap.get(propertyName) != null)
+ return true;
+ else
+ return false;
+ }
+
+ private boolean methodCheck(IResponse response,
+ ResponseStatus responseStatus) {
+ if (responseStatus == response.getStatus())
+ return true;
+ else
+ return false;
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * // http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+package org.iotivity.cloud.testrdserver;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+import java.util.concurrent.CountDownLatch;
+
+import org.iotivity.cloud.base.device.CoapDevice;
+import org.iotivity.cloud.base.protocols.IRequest;
+import org.iotivity.cloud.base.protocols.IResponse;
+import org.iotivity.cloud.base.protocols.MessageBuilder;
+import org.iotivity.cloud.base.protocols.coap.CoapResponse;
+import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
+import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
+import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresenceResource;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+public class ResourcePresenceResourceTest {
+ private ResourceDirectoryResource mRDResource = null;
+ private ResPresenceResource mResPresenceResource = null;
+ private CoapDevice mockDevice = null;
+ CountDownLatch latch = null;
+ IResponse res;
+
+ @Before
+ public void setUp() throws Exception {
+ mRDResource = new ResourceDirectoryResource();
+ mResPresenceResource = new ResPresenceResource();
+ res = null;
+ mockDevice = mock(CoapDevice.class);
+ latch = new CountDownLatch(1);
+ // callback mock
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ latch.countDown();
+ res = resp;
+ return resp;
+ }
+ }).when(mockDevice).sendResponse(Mockito.anyObject());
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ RDServerTestUtils.resetRDDatabase();
+ }
+
+ @Test
+ public void testHandleGetObserveRequest_notExistValue() throws Exception {
+ System.out.println("\t------testHandleGetObserveRequest_notExistValue");
+ IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.RES_PRS_URI, "di=" + RDServerTestUtils.DI);
+ mResPresenceResource.onDefaultRequestReceived(mockDevice, request);
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+ }
+
+ @Test
+ public void testHandleGetObserveRequest_ExistValue() throws Exception {
+ System.out.println("\t------testHandleGetObserveRequest_notExistValue");
+ CoapDevice observerDevice = mock(CoapDevice.class);
+ CountDownLatch observerLatch = new CountDownLatch(2);
+ // callback mock for observer Device
+ Mockito.doAnswer(new Answer<Object>() {
+ @Override
+ public CoapResponse answer(InvocationOnMock invocation)
+ throws Throwable {
+ CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
+ observerLatch.countDown();
+ // assertion for observer device (resource presence response)
+ if (observerLatch.getCount() == 1) {
+ assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
+ }
+ // assertion for observer device (resource presence response
+ // when the
+ // resource is published)
+ if (observerLatch.getCount() == 0) {
+ String payload = new String(resp.getPayload());
+ assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
+ assertTrue(payload.contains("non"));
+ assertTrue(payload.contains("ttl"));
+ assertTrue(payload.contains("trg"));
+ assertTrue(payload.contains("rt"));
+ assertTrue(payload.contains("di"));
+ assertTrue(payload.contains("href"));
+ }
+ return null;
+ }
+ }).when(observerDevice).sendResponse(Mockito.anyObject());
+ IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
+ RDServerTestUtils.RES_PRS_URI, "di=" + RDServerTestUtils.DI);
+ // observer : resource presence request (from CoapDevice observeDevice)
+ // observe specific device
+ mResPresenceResource.onDefaultRequestReceived(observerDevice, request);
+ // resource publish (from CoapDevice mockDevice)
+ mRDResource.onDefaultRequestReceived(mockDevice,
+ RDServerTestUtils.makePublishRequest());
+ // assertion: if the response status is "CHANGED"
+ assertTrue(latch.await(2L, SECONDS));
+ assertTrue(methodCheck(res, ResponseStatus.CHANGED));
+ }
+
+ private boolean methodCheck(IResponse response,
+ ResponseStatus responseStatus) {
+ if (responseStatus == response.getStatus())
+ return true;
+ else
+ return false;
+ }
+}
\ No newline at end of file