f0045ffe4d7e7e0f72f535d2813fbcdd017d1c30
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / CertificateTest.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.DeviceServerSystem;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.InjectMocks;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27
28 public class CertificateTest {
29     private static final String TEST_RESOURCE_CERTI_URI = "/"
30             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
31             + OICConstants.CERT_URI;
32     private CoapDevice          mockDevice              = mock(
33             CoapDevice.class);
34     IRequest                    req                     = null;
35     ConnectorPool               connectorPool           = null;
36     DeviceServerSystem          deviceServerSystem      = new DeviceServerSystem();
37     final CountDownLatch        latch                   = new CountDownLatch(1);
38
39     @Mock
40     IRequestChannel             requestChannel;
41
42     @InjectMocks
43     Certificate                 certHandler             = new Certificate();
44
45     @Before
46     public void setUp() throws Exception {
47         req = null;
48         MockitoAnnotations.initMocks(this);
49         deviceServerSystem.addResource(certHandler);
50         // callback mock
51         Mockito.doAnswer(new Answer<Object>() {
52             @Override
53             public CoapRequest answer(InvocationOnMock invocation)
54                     throws Throwable {
55                 Object[] args = invocation.getArguments();
56                 CoapRequest request = (CoapRequest) args[0];
57                 System.out.println(
58                         "\t----------payload : " + request.getPayloadString());
59                 System.out.println(
60                         "\t----------uripath : " + request.getUriPath());
61                 System.out.println(
62                         "\t---------uriquery : " + request.getUriQuery());
63                 req = request;
64                 latch.countDown();
65                 return null;
66             }
67         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
68                 Mockito.any(CoapDevice.class));
69     }
70
71     @Test
72     public void testOnDefaultRequestReceived() throws Exception {
73         System.out.println(
74                 "\t--------------OnRequestReceived(AS) Test------------");
75
76         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
77                 TEST_RESOURCE_CERTI_URI, null, null, null);
78         certHandler.onRequestReceived(mockDevice, request);
79
80         assertTrue(latch.await(1L, SECONDS));
81         assertEquals(req.getUriPath(), TEST_RESOURCE_CERTI_URI);
82     }
83
84 }