fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / KeepAliveResourceTest.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
23 package org.iotivity.cloud.ciserver.resources;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.Mockito.mock;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.concurrent.CountDownLatch;
32
33 import org.iotivity.cloud.base.device.CoapDevice;
34 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.IResponse;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
39 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
40 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
41 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
42 import org.iotivity.cloud.util.Cbor;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mockito;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.invocation.InvocationOnMock;
49 import org.mockito.stubbing.Answer;
50
51 public class KeepAliveResourceTest {
52
53     private KeepAliveResource                         keepAliveResource;
54     private CoapDevice                                mockDevice;
55     private Cbor<HashMap<String, ArrayList<Integer>>> mCbor     = new Cbor<>();
56     private final CountDownLatch                      mLatch    = new CountDownLatch(
57             1);
58     private IResponse                                 mRes;
59     private int[]                                     mInterval = { 1, 2, 4,
60             8 };
61
62     @Before
63     public void setUp() throws Exception {
64         keepAliveResource = new KeepAliveResource(mInterval);
65         MockitoAnnotations.initMocks(this);
66         mockDevice = mock(CoapDevice.class);
67         mRes = null;
68         // callback mock
69         Mockito.doAnswer(new Answer<Object>() {
70             @Override
71             public CoapResponse answer(InvocationOnMock invocation)
72                     throws Throwable {
73                 Object[] args = invocation.getArguments();
74                 CoapResponse resp = (CoapResponse) args[0];
75                 System.out
76                         .println("\t----payload : " + resp.getPayloadString());
77                 System.out
78                         .println("\t----responsestatus : " + resp.getStatus());
79                 mRes = resp;
80                 return null;
81             }
82         }).when(mockDevice).sendResponse(Mockito.anyObject());
83         keepAliveResource.startSessionChecker(0, 3000);
84     }
85
86     @After
87     public void Endup() throws Exception {
88         keepAliveResource.stopSessionChecker();
89     }
90
91     @Test
92     public void testOnDefaultGetRequestReceived() {
93         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
94                 "/oic/ping", null);
95         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
96         HashMap<String, ArrayList<Integer>> payloadData = mCbor
97                 .parsePayloadFromCbor(mRes.getPayload(), HashMap.class);
98         HashMap<String, ArrayList<Integer>> comparisonData = new HashMap<>();
99         ArrayList<Integer> comparisonArray = new ArrayList<>();
100         comparisonArray.add(1);
101         comparisonArray.add(2);
102         comparisonArray.add(4);
103         comparisonArray.add(8);
104         comparisonData.put("inarray", comparisonArray);
105         assertEquals(payloadData, comparisonData);
106         assertTrue(methodCheck(mRes, ResponseStatus.CONTENT));
107     }
108
109     @Test
110     public void testOnDefaultPutRequestReceived() {
111         HashMap<String, Integer> payloadData = new HashMap<>();
112         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
113         payloadData.put("in", 8);
114         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
115                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
116                 cbor.encodingPayloadToCbor(payloadData));
117         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
118         assertTrue(methodCheck(mRes, ResponseStatus.VALID));
119     }
120
121     @Test(expected = BadRequestException.class)
122     public void testOnDefaultExceptionRequestReceived() {
123         HashMap<String, Integer> payloadData = new HashMap<>();
124         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
125         payloadData.put("in", 8);
126         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
127                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
128                 cbor.encodingPayloadToCbor(payloadData));
129         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
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 }