06071ea231e98107263be33aedf8b8f08513bab2
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AclGroupTest.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.Arrays;
9 import java.util.HashMap;
10 import java.util.concurrent.CountDownLatch;
11
12 import org.iotivity.cloud.base.OCFConstants;
13 import org.iotivity.cloud.base.connector.ConnectorPool;
14 import org.iotivity.cloud.base.device.CoapDevice;
15 import org.iotivity.cloud.base.device.IRequestChannel;
16 import org.iotivity.cloud.base.protocols.IRequest;
17 import org.iotivity.cloud.base.protocols.MessageBuilder;
18 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
19 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
20 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
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 AclGroupTest {
33     private static final String TEST_RESOURCE_GROUP_URI = "/"
34             + OCFConstants.PREFIX_WELL_KNOWN + "/" + OCFConstants.PREFIX_OCF
35             + "/" + OCFConstants.ACL_URI + "/" + OCFConstants.GROUP_URI;
36     private CoapDevice          mockDevice              = mock(
37             CoapDevice.class);
38     IRequest                    req                     = null;
39     ConnectorPool               connectorPool           = null;
40     DeviceServerSystem          deviceServerSystem      = new DeviceServerSystem();
41     final CountDownLatch        latch                   = new CountDownLatch(1);
42
43     @Mock
44     IRequestChannel             requestChannel;
45
46     @InjectMocks
47     AclGroup                    aclGroupHandler         = new AclGroup();
48
49     @Before
50     public void setUp() throws Exception {
51         req = null;
52         Mockito.doReturn("mockUserId").when(mockDevice).getUserId();
53         MockitoAnnotations.initMocks(this);
54         deviceServerSystem.addResource(aclGroupHandler);
55         // callback mock
56         Mockito.doAnswer(new Answer<Object>() {
57             @Override
58             public CoapRequest answer(InvocationOnMock invocation)
59                     throws Throwable {
60                 Object[] args = invocation.getArguments();
61                 CoapRequest request = (CoapRequest) args[0];
62                 System.out.println(
63                         "\t----------payload : " + request.getPayloadString());
64                 System.out.println(
65                         "\t----------uripath : " + request.getUriPath());
66                 System.out.println(
67                         "\t---------uriquery : " + request.getUriQuery());
68                 req = request;
69                 latch.countDown();
70                 return null;
71             }
72         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
73                 Mockito.any(CoapDevice.class));
74     }
75
76     @Test
77     public void testCreateGroupRequestReceived() throws Exception {
78         System.out.println(
79                 "\t--------------OnRequestReceived(AS) Create Group Test------------");
80
81         HashMap<String, Object> payloadData = new HashMap<>();
82         payloadData.put("gtype", "public");
83         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
84         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
85                 TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR,
86                 cbor.encodingPayloadToCbor(payloadData));
87         aclGroupHandler.onRequestReceived(mockDevice, request);
88
89         assertTrue(latch.await(1L, SECONDS));
90         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
91                 .containsKey("gtype"));
92         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
93                 .containsKey("gmid"));
94         assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
95     }
96
97     @Test
98     public void testAddMemberRequestReceived() throws Exception {
99         System.out.println(
100                 "\t--------------OnRequestReceived(AS) Add Member Test------------");
101
102         HashMap<String, Object> payloadData = new HashMap<>();
103         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
104         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
105                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
106                 ContentFormat.APPLICATION_CBOR,
107                 cbor.encodingPayloadToCbor(payloadData));
108         aclGroupHandler.onRequestReceived(mockDevice, request);
109
110         assertTrue(latch.await(1L, SECONDS));
111         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
112                 .containsKey("midlist"));
113         assertEquals(req.getUriPath(),
114                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
115     }
116
117     @Test
118     public void testAddDeviceRequestReceived() throws Exception {
119         System.out.println(
120                 "\t--------------OnRequestReceived(AS) Add Device Test------------");
121
122         HashMap<String, Object> payloadData = new HashMap<>();
123         payloadData.put("dilist", Arrays.asList("sampleDevice"));
124         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
125         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
126                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
127                 ContentFormat.APPLICATION_CBOR,
128                 cbor.encodingPayloadToCbor(payloadData));
129         aclGroupHandler.onRequestReceived(mockDevice, request);
130
131         assertTrue(latch.await(1L, SECONDS));
132         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
133                 .containsKey("dilist"));
134         assertEquals(req.getUriPath(),
135                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
136     }
137
138     @Test
139     public void testGetGroupListAndGetGroupInfoRequestReceived()
140             throws Exception {
141         System.out.println(
142                 "\t--------------OnRequestReceived(AS) Get Group List & Get Group Info Test------------");
143
144         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
145                 TEST_RESOURCE_GROUP_URI, null, null, null);
146         aclGroupHandler.onRequestReceived(mockDevice, request);
147
148         assertTrue(latch.await(1L, SECONDS));
149         assertTrue(req.getUriQueryMap().containsKey("mid"));
150         assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
151     }
152
153     @Test
154     public void testDeleteGroupRequestReceived() throws Exception {
155         System.out.println(
156                 "\t--------------OnRequestReceived(AS) Delete Group Test------------");
157
158         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
159                 TEST_RESOURCE_GROUP_URI, "gid=samplegid", null, null);
160         aclGroupHandler.onRequestReceived(mockDevice, request);
161
162         assertTrue(latch.await(1L, SECONDS));
163         assertTrue(req.getUriQueryMap().containsKey("gid"));
164         assertTrue(req.getUriQueryMap().containsKey("gmid"));
165         assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
166     }
167
168     @Test
169     public void testDeleteMemberRequestReceived() throws Exception {
170         System.out.println(
171                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
172
173         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
174                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, null,
175                 null);
176         aclGroupHandler.onRequestReceived(mockDevice, request);
177
178         assertTrue(latch.await(1L, SECONDS));
179         assertTrue(req.getUriQueryMap().containsKey("midlist"));
180         assertEquals(req.getUriPath(),
181                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
182     }
183
184     @Test
185     public void testDeleteDeviceRequestReceived() throws Exception {
186         System.out.println(
187                 "\t--------------OnRequestReceived(AS) Delete Member Test------------");
188
189         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
190                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
191                 "dilist=sampledi", null, null);
192         aclGroupHandler.onRequestReceived(mockDevice, request);
193
194         assertTrue(latch.await(1L, SECONDS));
195         assertTrue(req.getUriQueryMap().containsKey("dilist"));
196         assertEquals(req.getUriPath(),
197                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
198     }
199 }