remove prefix .well-known uri & change from 'ocf' to 'oic' uri
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AccountTest.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.HashMap;
9 import java.util.concurrent.CountDownLatch;
10
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.IResponse;
16 import org.iotivity.cloud.base.protocols.MessageBuilder;
17 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
18 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
19 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
20 import org.iotivity.cloud.ciserver.Constants;
21 import org.iotivity.cloud.ciserver.DeviceServerSystem;
22 import org.iotivity.cloud.util.Cbor;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31
32 public class AccountTest {
33     private String             di                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
34     public static final String ACCOUNT_URI        = Constants.ACCOUNT_FULL_URI;
35     private String             authProvider       = "github";
36     private String             authCode           = "f6b55d8d8a27e0d32ab8";
37     String                     userId             = "testuser";
38     private CoapDevice         mockDevice         = mock(CoapDevice.class);
39     IResponse                  res                = null;
40     IRequest                   req                = null;
41     ConnectorPool              connectorPool      = null;
42     DeviceServerSystem         deviceServerSystem = new DeviceServerSystem();
43     final CountDownLatch       latch              = new CountDownLatch(1);
44     @Mock
45     IRequestChannel            requestChannel;
46
47     @InjectMocks
48     Account                    acHandler          = new Account();
49
50     @Before
51     public void setUp() throws Exception {
52         res = null;
53         req = null;
54         Mockito.doReturn("mockDeviceUser").when(mockDevice).getUserId();
55         MockitoAnnotations.initMocks(this);
56         deviceServerSystem.addResource(acHandler);
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                 req = request;
64                 latch.countDown();
65                 System.out.println(
66                         "\t----------payload : " + request.getPayloadString());
67                 System.out.println(
68                         "\t----------uripath : " + request.getUriPath());
69                 System.out.println(
70                         "\t---------uriquery : " + request.getUriQuery());
71                 return null;
72             }
73         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
74                 Mockito.any(CoapDevice.class));
75     }
76
77     @Test
78     public void testAccountDeviceDeleteOnRequestReceived() throws Exception {
79         System.out.println(
80                 "\t--------------OnRequestReceived Device Delete Test------------");
81         // sign up request from the client
82         IRequest request = null;
83         request = MessageBuilder.createRequest(RequestMethod.DELETE,
84                 ACCOUNT_URI, "di=device1");
85         deviceServerSystem.onRequestReceived(mockDevice, request);
86         assertTrue(latch.await(1L, SECONDS));
87         assertTrue(req.getUriQueryMap().get("uid").contains("mockDeviceUser"));
88         assertTrue(req.getUriQueryMap().get("di").contains("device1"));
89         assertTrue(req.getUriPath().equals(ACCOUNT_URI));
90     }
91
92     @Test
93     public void testAccountResourceOnRequestReceived() throws Exception {
94         System.out.println(
95                 "\t--------------OnRequestReceived Sign Up Test------------");
96         // sign up request from the client
97         IRequest request = null;
98         HashMap<String, String> payloadData = new HashMap<String, String>();
99         payloadData.put("authcode", authCode);
100         payloadData.put("authprovider", authProvider);
101         payloadData.put("di", di);
102         Cbor<HashMap<String, String>> cbor = new Cbor<HashMap<String, String>>();
103         request = MessageBuilder.createRequest(RequestMethod.POST, ACCOUNT_URI,
104                 null, ContentFormat.APPLICATION_CBOR,
105                 cbor.encodingPayloadToCbor(payloadData));
106         deviceServerSystem.onRequestReceived(mockDevice, request);
107         // assertion : request msg to the AS is identical to the request msg
108         // from the client
109         assertTrue(latch.await(1L, SECONDS));
110         assertEquals(request, req);
111     }
112 }