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