33747ece3c305c3493caeb85d3c3f83b3174b95f
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / KeepAliveResourceTest.java
1 package org.iotivity.cloud.ciserver.resources;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Mockito.mock;
6
7 import java.util.ArrayList;
8 import java.util.HashMap;
9
10 import org.iotivity.cloud.base.device.CoapDevice;
11 import org.iotivity.cloud.base.protocols.IRequest;
12 import org.iotivity.cloud.base.protocols.IResponse;
13 import org.iotivity.cloud.base.protocols.MessageBuilder;
14 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
15 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
16 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
17 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
18 import org.iotivity.cloud.util.Cbor;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.mockito.MockitoAnnotations;
24 import org.mockito.invocation.InvocationOnMock;
25 import org.mockito.stubbing.Answer;
26
27 public class KeepAliveResourceTest {
28
29     KeepAliveResource                                 keepAliveResource;
30     CoapDevice                                        mockDevice;
31     private Cbor<HashMap<String, ArrayList<Integer>>> mCbor    = new Cbor<>();
32     IResponse                                         res;
33     int[]                                             interval = { 1, 2, 4, 8 };
34
35     @Before
36     public void setUp() throws Exception {
37         keepAliveResource = new KeepAliveResource(interval);
38         MockitoAnnotations.initMocks(this);
39         mockDevice = mock(CoapDevice.class);
40         res = null;
41         // callback mock
42         Mockito.doAnswer(new Answer<Object>() {
43             @Override
44             public CoapResponse answer(InvocationOnMock invocation)
45                     throws Throwable {
46                 Object[] args = invocation.getArguments();
47                 CoapResponse resp = (CoapResponse) args[0];
48                 System.out
49                         .println("\t----payload : " + resp.getPayloadString());
50                 System.out
51                         .println("\t----responsestatus : " + resp.getStatus());
52                 res = resp;
53                 return null;
54             }
55         }).when(mockDevice).sendResponse(Mockito.anyObject());
56         keepAliveResource.startSessionChecker();
57     }
58
59     @After
60     public void Endup() throws Exception {
61         keepAliveResource.stopSessionChecker();
62     }
63
64     @Test
65     public void testOnDefaultGetRequestReceived() {
66         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
67                 "/oic/ping", null);
68         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
69         HashMap<String, ArrayList<Integer>> payloadData = mCbor
70                 .parsePayloadFromCbor(res.getPayload(), HashMap.class);
71         HashMap<String, ArrayList<Integer>> comparisonData = new HashMap<>();
72         ArrayList<Integer> comparisonArray = new ArrayList<>();
73         comparisonArray.add(1);
74         comparisonArray.add(2);
75         comparisonArray.add(4);
76         comparisonArray.add(8);
77         comparisonData.put("inarray", comparisonArray);
78         assertEquals(payloadData, comparisonData);
79         assertTrue(methodCheck(res, ResponseStatus.CONTENT));
80     }
81
82     @Test
83     public void testOnDefaultPutRequestReceived() {
84         HashMap<String, Integer> payloadData = new HashMap<>();
85         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
86         payloadData.put("in", 8);
87         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
88                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
89                 cbor.encodingPayloadToCbor(payloadData));
90         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
91         assertTrue(methodCheck(res, ResponseStatus.VALID));
92     }
93
94     // TODO : exception response assertion
95     // @Test
96     public void testOnDefaultPutEmptyPayloadRequestReceived() {
97         HashMap<String, Integer> payloadData = new HashMap<>();
98         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
99         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
100                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
101                 cbor.encodingPayloadToCbor(payloadData));
102         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
103         assertTrue(methodCheck(res, ResponseStatus.VALID));
104     }
105
106     // @Test
107     public void testOnDefaultPutNullPayloadRequestReceived() {
108         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
109                 "/oic/ping", null);
110         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
111         assertTrue(methodCheck(res, ResponseStatus.BAD_REQUEST));
112     }
113
114     private boolean methodCheck(IResponse response,
115             ResponseStatus responseStatus) {
116         if (responseStatus == response.getStatus())
117             return true;
118         else
119             return false;
120     }
121 }