Implement reconnection and ping-pong between cloud services. Make
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / CertificateTest.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.proxy.account;
24
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29
30 import java.util.concurrent.CountDownLatch;
31
32 import org.iotivity.cloud.base.OICConstants;
33 import org.iotivity.cloud.base.connector.ConnectorPool;
34 import org.iotivity.cloud.base.device.CoapDevice;
35 import org.iotivity.cloud.base.device.IRequestChannel;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
39 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
40 import org.iotivity.cloud.ciserver.DeviceServerSystem;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.invocation.InvocationOnMock;
49 import org.mockito.stubbing.Answer;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
53
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest(ConnectorPool.class)
56 public class CertificateTest {
57     private static final String TEST_RESOURCE_CERTI_URI = "/"
58             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
59             + OICConstants.CERT_URI;
60     private CoapDevice          mMockDevice             = mock(
61             CoapDevice.class);
62     private IRequest            mReq                    = null;
63     private DeviceServerSystem  mDeviceServerSystem     = new DeviceServerSystem();
64     final CountDownLatch        mLatch                  = new CountDownLatch(1);
65
66     @Mock
67     private IRequestChannel     mRequestChannel;
68
69     @InjectMocks
70     private Certificate         mCertHandler            = new Certificate();
71
72     @Before
73     public void setUp() throws Exception {
74         mReq = null;
75         MockitoAnnotations.initMocks(this);
76         mDeviceServerSystem.addResource(mCertHandler);
77         // callback mock
78         Mockito.doAnswer(new Answer<Object>() {
79             @Override
80             public CoapRequest answer(InvocationOnMock invocation)
81                     throws Throwable {
82                 Object[] args = invocation.getArguments();
83                 CoapRequest request = (CoapRequest) args[0];
84                 System.out.println(
85                         "\t----------payload : " + request.getPayloadString());
86                 System.out.println(
87                         "\t----------uripath : " + request.getUriPath());
88                 System.out.println(
89                         "\t---------uriquery : " + request.getUriQuery());
90                 mReq = request;
91                 mLatch.countDown();
92                 return null;
93             }
94         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
95                 Mockito.any(CoapDevice.class));
96
97         PowerMockito.mockStatic(ConnectorPool.class);
98         PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
99     }
100
101     @Test
102     public void testOnDefaultRequestReceived() throws Exception {
103         System.out.println(
104                 "\t--------------OnRequestReceived(AS) Test------------");
105
106         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
107                 TEST_RESOURCE_CERTI_URI, null, null, null);
108         mCertHandler.onRequestReceived(mMockDevice, request);
109
110         assertTrue(mLatch.await(1L, SECONDS));
111         assertEquals(mReq.getUriPath(), TEST_RESOURCE_CERTI_URI);
112     }
113
114 }