Imported Upstream version 1.2.0
[platform/upstream/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.device.CoapDevice;
34 import org.iotivity.cloud.base.device.IRequestChannel;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
38 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
39 import org.iotivity.cloud.ciserver.DeviceServerSystem;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.invocation.InvocationOnMock;
47 import org.mockito.stubbing.Answer;
48
49 public class CertificateTest {
50     private static final String TEST_RESOURCE_CERTI_URI = "/"
51             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
52             + OICConstants.CERT_URI;
53     private CoapDevice          mMockDevice             = mock(
54             CoapDevice.class);
55     private IRequest            mReq                    = null;
56     private DeviceServerSystem  mDeviceServerSystem     = new DeviceServerSystem();
57     final CountDownLatch        mLatch                  = new CountDownLatch(1);
58
59     @Mock
60     private IRequestChannel     mRequestChannel;
61
62     @InjectMocks
63     private Certificate         mCertHandler            = new Certificate();
64
65     @Before
66     public void setUp() throws Exception {
67         mReq = null;
68         MockitoAnnotations.initMocks(this);
69         mDeviceServerSystem.addResource(mCertHandler);
70         // callback mock
71         Mockito.doAnswer(new Answer<Object>() {
72             @Override
73             public CoapRequest answer(InvocationOnMock invocation)
74                     throws Throwable {
75                 Object[] args = invocation.getArguments();
76                 CoapRequest request = (CoapRequest) args[0];
77                 System.out.println(
78                         "\t----------payload : " + request.getPayloadString());
79                 System.out.println(
80                         "\t----------uripath : " + request.getUriPath());
81                 System.out.println(
82                         "\t---------uriquery : " + request.getUriQuery());
83                 mReq = request;
84                 mLatch.countDown();
85                 return null;
86             }
87         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
88                 Mockito.any(CoapDevice.class));
89     }
90
91     @Test
92     public void testOnDefaultRequestReceived() throws Exception {
93         System.out.println(
94                 "\t--------------OnRequestReceived(AS) Test------------");
95
96         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
97                 TEST_RESOURCE_CERTI_URI, null, null, null);
98         mCertHandler.onRequestReceived(mMockDevice, request);
99
100         assertTrue(mLatch.await(1L, SECONDS));
101         assertEquals(mReq.getUriPath(), TEST_RESOURCE_CERTI_URI);
102     }
103
104 }