[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[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.protocols.IRequest;
35 import org.iotivity.cloud.base.protocols.IResponse;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
40 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
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.MockitoAnnotations;
47 import org.mockito.invocation.InvocationOnMock;
48 import org.mockito.stubbing.Answer;
49
50 public class KeepAliveResourceTest {
51
52     private KeepAliveResource                         keepAliveResource;
53     private CoapDevice                                mockDevice;
54     private Cbor<HashMap<String, ArrayList<Integer>>> mCbor     = new Cbor<>();
55     private final CountDownLatch                      mLatch    = new CountDownLatch(
56             1);
57     private IResponse                                 mRes;
58     private int[]                                     mInterval = { 1, 2, 4,
59             8 };
60
61     @Before
62     public void setUp() throws Exception {
63         keepAliveResource = new KeepAliveResource(mInterval);
64         MockitoAnnotations.initMocks(this);
65         mockDevice = mock(CoapDevice.class);
66         mRes = null;
67         // callback mock
68         Mockito.doAnswer(new Answer<Object>() {
69             @Override
70             public CoapResponse answer(InvocationOnMock invocation)
71                     throws Throwable {
72                 Object[] args = invocation.getArguments();
73                 CoapResponse resp = (CoapResponse) args[0];
74                 System.out
75                         .println("\t----payload : " + resp.getPayloadString());
76                 System.out
77                         .println("\t----responsestatus : " + resp.getStatus());
78                 mRes = resp;
79                 return null;
80             }
81         }).when(mockDevice).sendResponse(Mockito.anyObject());
82         keepAliveResource.startSessionChecker(0, 3000);
83     }
84
85     @After
86     public void Endup() throws Exception {
87         keepAliveResource.stopSessionChecker();
88     }
89
90     @Test
91     public void testOnDefaultGetRequestReceived() {
92         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
93                 "/oic/ping", null);
94         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
95         HashMap<String, ArrayList<Integer>> payloadData = mCbor
96                 .parsePayloadFromCbor(mRes.getPayload(), HashMap.class);
97         HashMap<String, ArrayList<Integer>> comparisonData = new HashMap<>();
98         ArrayList<Integer> comparisonArray = new ArrayList<>();
99         comparisonArray.add(1);
100         comparisonArray.add(2);
101         comparisonArray.add(4);
102         comparisonArray.add(8);
103         comparisonData.put("inarray", comparisonArray);
104         assertEquals(payloadData, comparisonData);
105         assertTrue(methodCheck(mRes, ResponseStatus.CONTENT));
106     }
107
108     @Test
109     public void testOnDefaultPutRequestReceived() {
110         HashMap<String, Integer> payloadData = new HashMap<>();
111         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
112         payloadData.put("in", 8);
113         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
114                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
115                 cbor.encodingPayloadToCbor(payloadData));
116         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
117         assertTrue(methodCheck(mRes, ResponseStatus.VALID));
118     }
119
120     private boolean methodCheck(IResponse response,
121             ResponseStatus responseStatus) {
122         if (responseStatus == response.getStatus())
123             return true;
124         else
125             return false;
126     }
127 }