Implement reconnection and ping-pong between cloud services. Make
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / CrlTest.java
1 package org.iotivity.cloud.ciserver.resources.proxy.account;
2
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
7
8 import java.util.concurrent.CountDownLatch;
9
10 import org.iotivity.cloud.base.OICConstants;
11 import org.iotivity.cloud.base.connector.ConnectorPool;
12 import org.iotivity.cloud.base.device.CoapDevice;
13 import org.iotivity.cloud.base.device.IRequestChannel;
14 import org.iotivity.cloud.base.protocols.IRequest;
15 import org.iotivity.cloud.base.protocols.MessageBuilder;
16 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
17 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
18 import org.iotivity.cloud.ciserver.Constants;
19 import org.iotivity.cloud.ciserver.DeviceServerSystem;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29 import org.powermock.api.mockito.PowerMockito;
30 import org.powermock.core.classloader.annotations.PrepareForTest;
31 import org.powermock.modules.junit4.PowerMockRunner;
32
33 @RunWith(PowerMockRunner.class)
34 @PrepareForTest(ConnectorPool.class)
35 public class CrlTest {
36
37     private static final String TEST_RESOURCE_CRL_URI = "/"
38             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
39             + Constants.REQ_CRL;
40     private CoapDevice          mMockDevice           = mock(CoapDevice.class);
41     private IRequest            mReq                  = null;
42     private DeviceServerSystem  mDeviceServerSystem   = new DeviceServerSystem();
43     final CountDownLatch        mLatch                = new CountDownLatch(1);
44
45     @Mock
46     private IRequestChannel     mRequestChannel;
47
48     @InjectMocks
49     private Crl                 mCertHandler          = new Crl();
50
51     @Before
52     public void setUp() throws Exception {
53         mReq = null;
54         MockitoAnnotations.initMocks(this);
55         mDeviceServerSystem.addResource(mCertHandler);
56         // callback mock
57         Mockito.doAnswer(new Answer<Object>() {
58             @Override
59             public CoapRequest answer(InvocationOnMock invocation)
60                     throws Throwable {
61                 Object[] args = invocation.getArguments();
62                 CoapRequest request = (CoapRequest) args[0];
63                 System.out.println(
64                         "\t----------payload : " + request.getPayloadString());
65                 System.out.println(
66                         "\t----------uripath : " + request.getUriPath());
67                 System.out.println(
68                         "\t---------uriquery : " + request.getUriQuery());
69                 mReq = request;
70                 mLatch.countDown();
71                 return null;
72             }
73         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
74                 Mockito.any(CoapDevice.class));
75
76         PowerMockito.mockStatic(ConnectorPool.class);
77         PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
78     }
79
80     @Test
81     public void testOnDefaultRequestReceived() throws Exception {
82         System.out.println(
83                 "\t--------------OnRequestReceived(AS) Test------------");
84
85         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
86                 TEST_RESOURCE_CRL_URI, null, null, null);
87         mCertHandler.onRequestReceived(mMockDevice, request);
88
89         assertTrue(mLatch.await(1L, SECONDS));
90         assertEquals(mReq.getUriPath(), TEST_RESOURCE_CRL_URI);
91     }
92
93 }