2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.testrdserver;
24 import static java.util.concurrent.TimeUnit.SECONDS;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
28 import java.util.concurrent.CountDownLatch;
30 import org.iotivity.cloud.base.device.CoapDevice;
31 import org.iotivity.cloud.base.protocols.IRequest;
32 import org.iotivity.cloud.base.protocols.IResponse;
33 import org.iotivity.cloud.base.protocols.MessageBuilder;
34 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
35 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
36 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
37 import org.iotivity.cloud.rdserver.resources.directory.rd.ResourceDirectoryResource;
38 import org.iotivity.cloud.rdserver.resources.presence.resource.ResPresenceResource;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.stubbing.Answer;
46 public class ResourcePresenceResourceTest {
47 private ResourceDirectoryResource mRDResource = null;
48 private ResPresenceResource mResPresenceResource = null;
49 private CoapDevice mockDevice = null;
50 private CountDownLatch mLatch = null;
51 private IResponse mResponse;
54 public void setUp() throws Exception {
55 mRDResource = new ResourceDirectoryResource();
56 mResPresenceResource = new ResPresenceResource();
58 mockDevice = mock(CoapDevice.class);
59 mLatch = new CountDownLatch(1);
61 Mockito.doAnswer(new Answer<Object>() {
63 public CoapResponse answer(InvocationOnMock invocation)
65 CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
70 }).when(mockDevice).sendResponse(Mockito.anyObject());
74 public void tearDown() throws Exception {
75 RDServerTestUtils.resetRDDatabase();
79 public void testHandleGetObserveRequest_notExistValue() throws Exception {
80 System.out.println("\t------testHandleGetObserveRequest_notExistValue");
81 IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
82 RDServerTestUtils.RES_PRS_URI, "di=" + RDServerTestUtils.DI);
83 mResPresenceResource.onDefaultRequestReceived(mockDevice, request);
84 assertTrue(mLatch.await(2L, SECONDS));
85 assertTrue(methodCheck(mResponse, ResponseStatus.CONTENT));
89 public void testHandleGetObserveRequest_ExistValue() throws Exception {
90 System.out.println("\t------testHandleGetObserveRequest_ExistValue");
91 CoapDevice observerDevice = mock(CoapDevice.class);
92 CountDownLatch observerLatch = new CountDownLatch(2);
93 // callback mock for observer Device
94 Mockito.doAnswer(new Answer<Object>() {
96 public CoapResponse answer(InvocationOnMock invocation)
98 CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
99 observerLatch.countDown();
100 // assertion for observer device (resource presence response)
101 if (observerLatch.getCount() == 1) {
102 assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
104 // assertion for observer device (resource presence response
106 // resource is published)
107 if (observerLatch.getCount() == 0) {
108 String payload = new String(resp.getPayload());
109 assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
110 assertTrue(payload.contains("non"));
111 assertTrue(payload.contains("ttl"));
112 assertTrue(payload.contains("trg"));
113 assertTrue(payload.contains("rt"));
114 assertTrue(payload.contains("href"));
118 }).when(observerDevice).sendResponse(Mockito.anyObject());
119 IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
120 RDServerTestUtils.RES_PRS_URI, "di=" + RDServerTestUtils.DI);
121 // observer : resource presence request (from CoapDevice observeDevice)
122 // observe specific device
123 mResPresenceResource.onDefaultRequestReceived(observerDevice, request);
124 // resource publish (from CoapDevice mockDevice)
125 mRDResource.onDefaultRequestReceived(mockDevice,
126 RDServerTestUtils.makePublishRequest());
127 // assertion: if the response status is "CHANGED"
128 assertTrue(mLatch.await(2L, SECONDS));
129 assertTrue(methodCheck(mResponse, ResponseStatus.CHANGED));
132 private boolean methodCheck(IResponse response,
133 ResponseStatus responseStatus) {
134 if (responseStatus == response.getStatus())