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