Merge remote-tracking branch 'origin/master' into notification-service
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / test / java / org / iotivity / cloud / testrdserver / DiscoveryResourceTest.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 import java.util.ArrayList;
28 import java.util.HashMap;
29 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.directory.res.DiscoveryResource;
39 import org.iotivity.cloud.util.Cbor;
40 import org.junit.After;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.mockito.invocation.InvocationOnMock;
45 import org.mockito.stubbing.Answer;
46
47 public class DiscoveryResourceTest {
48     private Cbor<ArrayList<Object>>   mCbor              = new Cbor<>();
49     private ResourceDirectoryResource mRDResource        = null;
50     private DiscoveryResource         mDiscoveryResource = null;
51     private CoapDevice                mockDevice         = null;
52     CountDownLatch                    latch              = null;
53     IResponse                         res;
54
55     @Before
56     public void setUp() throws Exception {
57         res = null;
58         mockDevice = mock(CoapDevice.class);
59         latch = new CountDownLatch(1);
60         mRDResource = new ResourceDirectoryResource();
61         mDiscoveryResource = new DiscoveryResource();
62         // callback mock
63         Mockito.doAnswer(new Answer<Object>() {
64             @Override
65             public CoapResponse answer(InvocationOnMock invocation)
66                     throws Throwable {
67                 CoapResponse resp = (CoapResponse) invocation.getArguments()[0];
68                 latch.countDown();
69                 res = resp;
70                 return resp;
71             }
72         }).when(mockDevice).sendResponse(Mockito.anyObject());
73     }
74
75     @After
76     public void tearDown() throws Exception {
77         RDServerTestUtils.resetRDDatabase();
78     }
79
80     @Test
81     public void testHandleGetRequest_notExistVaule() throws Exception {
82         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
83                 RDServerTestUtils.DISCOVERY_REQ_URI,
84                 "rt=core.light&di=" + RDServerTestUtils.DI);
85         mDiscoveryResource.onDefaultRequestReceived(mockDevice, request);
86         // assertion: if the response status is "CONTENT"
87         // assertion : if the payload is null
88         assertTrue(latch.await(2L, SECONDS));
89         assertTrue(methodCheck(res, ResponseStatus.CONTENT));
90         assertTrue(nullPayloadCheck(res));
91     }
92
93     @Test
94     public void testHandleGetRequest_existValue() throws Exception {
95         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
96                 RDServerTestUtils.DISCOVERY_REQ_URI,
97                 "rt=core.light&di=" + RDServerTestUtils.DI);
98         mRDResource.onDefaultRequestReceived(mockDevice,
99                 RDServerTestUtils.makePublishRequest());
100         mDiscoveryResource.onDefaultRequestReceived(mockDevice, request);
101         // assertion: if the response status is "CONTENT"
102         // assertion : if the payload contains resource info
103         assertTrue(latch.await(2L, SECONDS));
104         assertTrue(methodCheck(res, ResponseStatus.CONTENT));
105         assertTrue(discoverHashmapCheck(res, "di"));
106         assertTrue(discoveredResourceCheck(res, "href"));
107         assertTrue(discoveredResourceCheck(res, "rt"));
108         assertTrue(discoveredResourceCheck(res, "if"));
109     }
110
111     private boolean discoverHashmapCheck(IResponse response,
112             String propertyName) {
113         ArrayList<Object> resourceList = mCbor
114                 .parsePayloadFromCbor(response.getPayload(), ArrayList.class);
115         HashMap<Object, Object> firstArray = (HashMap<Object, Object>) resourceList
116                 .get(0);
117         if (firstArray.get(propertyName) != null)
118             return true;
119         else
120             return false;
121     }
122
123     private boolean discoveredResourceCheck(IResponse response,
124             String propertyName) {
125         ArrayList<Object> resourceList = mCbor
126                 .parsePayloadFromCbor(response.getPayload(), ArrayList.class);
127         HashMap<Object, Object> firstArray = (HashMap<Object, Object>) resourceList
128                 .get(0);
129         ArrayList<HashMap<Object, Object>> linkData = (ArrayList<HashMap<Object, Object>>) firstArray
130                 .get("links");
131         HashMap<Object, Object> linkMap = linkData.get(0);
132         if (linkMap.get(propertyName) != null)
133             return true;
134         else
135             return false;
136     }
137
138     private boolean methodCheck(IResponse response,
139             ResponseStatus responseStatus) {
140         if (responseStatus == response.getStatus())
141             return true;
142         else
143             return false;
144     }
145
146     private boolean nullPayloadCheck(IResponse response) {
147         ArrayList<Object> payloadData = mCbor
148                 .parsePayloadFromCbor(res.getPayload(), ArrayList.class);
149         return (payloadData.isEmpty());
150     }
151 }