replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / test / java / org / iotivity / cloud / testrdserver / ResourcePresenceResourceTest.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
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
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
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.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.testrdserver;
23
24 import static java.util.concurrent.TimeUnit.SECONDS;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27
28 import java.util.concurrent.CountDownLatch;
29
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;
45
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;
52
53     @Before
54     public void setUp() throws Exception {
55         mRDResource = new ResourceDirectoryResource();
56         mResPresenceResource = new ResPresenceResource();
57         mResponse = null;
58         mockDevice = mock(CoapDevice.class);
59         mLatch = new CountDownLatch(1);
60         // callback mock
61         Mockito.doAnswer(new Answer<Object>() {
62             @Override
63             public CoapResponse answer(InvocationOnMock invocation)
64                     throws Throwable {
65                 CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
66                 mLatch.countDown();
67                 mResponse = resp;
68                 return resp;
69             }
70         }).when(mockDevice).sendResponse(Mockito.anyObject());
71     }
72
73     @After
74     public void tearDown() throws Exception {
75         RDServerTestUtils.resetRDDatabase();
76     }
77
78     @Test
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));
86     }
87
88     @Test
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>() {
95             @Override
96             public CoapResponse answer(InvocationOnMock invocation)
97                     throws Throwable {
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));
103                 }
104                 // assertion for observer device (resource presence response
105                 // when the
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"));
115                 }
116                 return null;
117             }
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));
130     }
131
132     private boolean methodCheck(IResponse response,
133             ResponseStatus responseStatus) {
134         if (responseStatus == response.getStatus())
135             return true;
136         else
137             return false;
138     }
139 }