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