From: Jung Seungho Date: Wed, 10 Aug 2016 02:15:16 +0000 (+0900) Subject: CI unit test included X-Git-Tag: 1.2.0+RC1~70^2~15 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b5c14e4a037df1f8877c4c7bfafbcdf1dbbaaff8;p=platform%2Fupstream%2Fiotivity.git CI unit test included Change-Id: I68b655f2a07db01ea4fca4c580702eb4a10ae66f Signed-off-by: yeonghun.nam Signed-off-by: Jung Seungho Reviewed-on: https://gerrit.iotivity.org/gerrit/9913 Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim --- diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/DeviceServerSystemTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/DeviceServerSystemTest.java new file mode 100644 index 000000000..19b2c9d63 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/DeviceServerSystemTest.java @@ -0,0 +1,246 @@ +package org.iotivity.cloud.ciserver; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.Device; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.server.CoapServer; +import org.iotivity.cloud.base.server.HttpServer; +import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool; +import org.iotivity.cloud.ciserver.resources.proxy.account.Account; +import org.iotivity.cloud.ciserver.resources.proxy.mq.MessageQueue; +import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceDirectory; +import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourceFind; +import org.iotivity.cloud.ciserver.resources.proxy.rd.ResourcePresence; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.Attribute; + +public class DeviceServerSystemTest { + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + String userId = "testuser"; + String accessToken = "1689c70ffa245effc563017fee36d250"; + private CoapDevice mockDevice = mock( + CoapDevice.class); + private Device device = mock( + Device.class); + IResponse res = null; + IRequest req = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch( + 1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + DeviceServerSystem.CoapLifecycleHandler coapLifecycleHandler = deviceServerSystem.new CoapLifecycleHandler(); + @InjectMocks + DeviceServerSystem.CoapAuthHandler coapAuthHandler = deviceServerSystem.new CoapAuthHandler(); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Before + public void testAddHttpServer() throws Exception { + HttpServer httpServer = new HttpServer(null); + deviceServerSystem.addServer(httpServer); + } + + @Before + public void testAddCoapServer() throws Exception { + CoapServer coapServer = new CoapServer(null); + deviceServerSystem.addServer(coapServer); + } + + @Test + public void testGetDevicePool() throws Exception { + CoapDevicePool devicePool = deviceServerSystem.getDevicePool(); + if (devicePool != null) { + System.out.println("devicePool returned :" + devicePool); + } + } + + @Test + public void testAddDevice() throws Exception { + CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken); + CoapDevicePool devicePool = deviceServerSystem.getDevicePool(); + devicePool.addDevice(coapDevice); + } + + @Test + public void testRemoveNotRegisteredDevice() throws Exception { + CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken); + CoapDevicePool devicePool = deviceServerSystem.getDevicePool(); + devicePool.removeDevice(coapDevice); + } + + @Test + public void testRemoveDevice() throws Exception { + CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken); + CoapDevicePool devicePool = deviceServerSystem.getDevicePool(); + devicePool.addDevice(coapDevice); + devicePool.removeDevice(coapDevice); + } + + @Test + public void testQueryDevice() throws Exception { + CoapDevice coapDevice = new CoapDevice(null, di, userId, accessToken); + CoapDevicePool devicePool = deviceServerSystem.getDevicePool(); + devicePool.addDevice(coapDevice); + devicePool.queryDevice(di); + } + + @Test + public void testStopSystem() throws Exception { + deviceServerSystem.stopSystem(); + } + + @Test + public void testAddAccountResource() { + Account acHandler = new Account(); + ResourceDirectory rdHandler = new ResourceDirectory(); + ResourceFind resHandler = new ResourceFind(); + ResourcePresence adHandler = new ResourcePresence(); + MessageQueue mqHandler = new MessageQueue(); + deviceServerSystem.addResource(acHandler); + deviceServerSystem.addResource(rdHandler); + deviceServerSystem.addResource(resHandler); + deviceServerSystem.addResource(adHandler); + deviceServerSystem.addResource(mqHandler); + } + + @Test + public void testChannelRead() throws InterruptedException { + ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); + + // inject mocked coapDevice into the api + Channel channel = mock(Channel.class); + Attribute attribute = mock(Attribute.class); + Mockito.doReturn(channel).when(ctx).channel(); + Mockito.doReturn(attribute).when(channel).attr(Mockito.any()); + Mockito.doReturn(mockDevice).when(attribute).get(); + + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null, + null); + Mockito.doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + + Object[] args = invocation.getArguments(); + IRequest req = (IRequest) args[0]; + + assertEquals(req, request); + + latch.countDown(); + return null; + } + + }).when(ctx).fireChannelRead(Mockito.anyObject()); + coapLifecycleHandler.channelRead(ctx, request); + assertTrue(latch.await(1L, SECONDS)); + + } + + @Test + public void coapAuthHandlerAccountChannelReadRequest() + throws InterruptedException { + System.out.println( + "\t--------------coapAuthHandler Account ChannelReadRequest Test------------"); + ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); + Cbor> cbor = new Cbor<>(); + Channel channel = mock(Channel.class); + Attribute attribute = mock(Attribute.class); + Mockito.doReturn(channel).when(ctx).channel(); + Mockito.doReturn(attribute).when(channel).attr(Mockito.any()); + + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest req = (CoapRequest) args[0]; + assertEquals(req.getUriPath(), + "/" + OCFConstants.PREFIX_WELL_KNOWN + "/" + + OCFConstants.PREFIX_OCF + "/" + + OCFConstants.ACCOUNT_URI); + assertTrue(cbor + .parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("di")); + latch.countDown(); + return null; + } + }).when(ctx).fireChannelRead(Mockito.any()); + + HashMap payloadData = new HashMap<>(); + payloadData.put("di", "sampleDevice"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + "/" + OCFConstants.PREFIX_WELL_KNOWN + "/" + + OCFConstants.PREFIX_OCF + "/" + + OCFConstants.ACCOUNT_URI, + null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + coapAuthHandler.channelRead(ctx, request); + } + + @Test + public void coapAuthHandlerPingChannelReadRequest() + throws InterruptedException { + System.out.println( + "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------"); + ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); + Cbor> cbor = new Cbor<>(); + Channel channel = mock(Channel.class); + Attribute attribute = mock(Attribute.class); + Mockito.doReturn(channel).when(ctx).channel(); + Mockito.doReturn(attribute).when(channel).attr(Mockito.any()); + + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest req = (CoapRequest) args[0]; + assertEquals(req.getUriPath(), "/" + OCFConstants.PREFIX_OIC + + "/" + OCFConstants.KEEP_ALIVE_URI); + + latch.countDown(); + return null; + } + }).when(ctx).fireChannelRead(Mockito.any()); + + IRequest request = MessageBuilder + .createRequest(RequestMethod.POST, + "/" + OCFConstants.PREFIX_OIC + "/" + + OCFConstants.KEEP_ALIVE_URI, + null, null, null); + coapAuthHandler.channelRead(ctx, request); + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java new file mode 100644 index 000000000..6bffe5548 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java @@ -0,0 +1,159 @@ +package org.iotivity.cloud.ciserver.resources; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.coap.CoapResponse; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool; +import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler; +import org.iotivity.cloud.ciserver.resources.DiResource.LinkInterfaceHandler; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class DiResourceTest { + private static final String RELAY_URI = "/di"; + private static final String RESOURCE_URI = "/a/light/0"; + private String diServer = "resourceServerId"; + private CoapDevice sourceDevice = mock(CoapDevice.class); + private CoapDevice targetDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + IRequestChannel targetChannel = mock( + IRequestChannel.class); + + @Mock + CoapDevicePool coapDevicePool; + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + DiResource diHandler = new DiResource( + coapDevicePool); + + @InjectMocks + LinkInterfaceHandler linkInterfaceHandler = diHandler.new LinkInterfaceHandler( + "targetDeviceId", sourceDevice); + + @InjectMocks + DefaultResponseHandler defaultResponseHandler = diHandler.new DefaultResponseHandler( + "targetDeviceId", sourceDevice); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + Mockito.doReturn("sourceDeviceId").when(sourceDevice).getDeviceId(); + Mockito.doReturn("targetDeviceId").when(targetDevice).getDeviceId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(diHandler); + Mockito.doReturn(targetDevice).when(coapDevicePool) + .queryDevice(Mockito.anyString()); + Mockito.doReturn(targetChannel).when(targetDevice).getRequestChannel(); + + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapResponse answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapResponse resp = (CoapResponse) args[0]; + res = resp; + latch.countDown(); + return null; + } + }).when(sourceDevice).sendResponse(Mockito.anyObject()); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(targetChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testOnDefaultRequestReceived() throws InterruptedException { + IRequest request = makePutRequest(); + diHandler.onDefaultRequestReceived(sourceDevice, request); + assertTrue(req.getMethod().equals(RequestMethod.PUT)); + assertTrue(req.getUriPath().equals(RESOURCE_URI)); + assertTrue(latch.await(1L, SECONDS)); + } + + // TODO treat oic.if.ll resource response (on LinkInterfaceHandler) + + @Test + public void testOnDefaultResponseHandleronResponseReceived() + throws InterruptedException { + IResponse response = makeContentResponse(); + defaultResponseHandler.onResponseReceived(response); + assertEquals(res, response); + assertTrue(latch.await(1L, SECONDS)); + } + + private IRequest makePutRequest() { + Cbor> cbor = new Cbor<>(); + HashMap payloadData = new HashMap<>(); + payloadData.put("state", true); + payloadData.put("power", 6); + IRequest request = MessageBuilder.createRequest(RequestMethod.PUT, + RELAY_URI + "/" + diServer + RESOURCE_URI, null, + ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + return request; + } + + private IResponse makeContentResponse() { + Cbor> cbor = new Cbor<>(); + HashMap payloadData = new HashMap<>(); + payloadData.put("state", true); + payloadData.put("power", 6); + IResponse response = MessageBuilder.createResponse(makeGetRequest(), + ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + return response; + } + + private IRequest makeGetRequest() { + IRequest request = MessageBuilder.createRequest(RequestMethod.PUT, + RELAY_URI + "/" + diServer + RESOURCE_URI, null); + return request; + } + +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java new file mode 100644 index 000000000..33747ece3 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java @@ -0,0 +1,121 @@ +package org.iotivity.cloud.ciserver.resources; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapResponse; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.util.Cbor; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class KeepAliveResourceTest { + + KeepAliveResource keepAliveResource; + CoapDevice mockDevice; + private Cbor>> mCbor = new Cbor<>(); + IResponse res; + int[] interval = { 1, 2, 4, 8 }; + + @Before + public void setUp() throws Exception { + keepAliveResource = new KeepAliveResource(interval); + MockitoAnnotations.initMocks(this); + mockDevice = mock(CoapDevice.class); + res = null; + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapResponse answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapResponse resp = (CoapResponse) args[0]; + System.out + .println("\t----payload : " + resp.getPayloadString()); + System.out + .println("\t----responsestatus : " + resp.getStatus()); + res = resp; + return null; + } + }).when(mockDevice).sendResponse(Mockito.anyObject()); + keepAliveResource.startSessionChecker(); + } + + @After + public void Endup() throws Exception { + keepAliveResource.stopSessionChecker(); + } + + @Test + public void testOnDefaultGetRequestReceived() { + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + "/oic/ping", null); + keepAliveResource.onDefaultRequestReceived(mockDevice, request); + HashMap> payloadData = mCbor + .parsePayloadFromCbor(res.getPayload(), HashMap.class); + HashMap> comparisonData = new HashMap<>(); + ArrayList comparisonArray = new ArrayList<>(); + comparisonArray.add(1); + comparisonArray.add(2); + comparisonArray.add(4); + comparisonArray.add(8); + comparisonData.put("inarray", comparisonArray); + assertEquals(payloadData, comparisonData); + assertTrue(methodCheck(res, ResponseStatus.CONTENT)); + } + + @Test + public void testOnDefaultPutRequestReceived() { + HashMap payloadData = new HashMap<>(); + Cbor> cbor = new Cbor<>(); + payloadData.put("in", 8); + IRequest request = MessageBuilder.createRequest(RequestMethod.PUT, + "/oic/ping", null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + keepAliveResource.onDefaultRequestReceived(mockDevice, request); + assertTrue(methodCheck(res, ResponseStatus.VALID)); + } + + // TODO : exception response assertion + // @Test + public void testOnDefaultPutEmptyPayloadRequestReceived() { + HashMap payloadData = new HashMap<>(); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.PUT, + "/oic/ping", null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + keepAliveResource.onDefaultRequestReceived(mockDevice, request); + assertTrue(methodCheck(res, ResponseStatus.VALID)); + } + + // @Test + public void testOnDefaultPutNullPayloadRequestReceived() { + IRequest request = MessageBuilder.createRequest(RequestMethod.PUT, + "/oic/ping", null); + keepAliveResource.onDefaultRequestReceived(mockDevice, request); + assertTrue(methodCheck(res, ResponseStatus.BAD_REQUEST)); + } + + private boolean methodCheck(IResponse response, + ResponseStatus responseStatus) { + if (responseStatus == response.getStatus()) + return true; + else + return false; + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java new file mode 100644 index 000000000..25c54350b --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java @@ -0,0 +1,136 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AccountSessionTest { + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + public static final String SESSION_URI = "/.well-known/ocf/account/session"; + private CoapDevice mockDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + + @InjectMocks + AccountSession acSessionHandler = new AccountSession(); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + Mockito.doReturn(di).when(mockDevice).getDeviceId(); + Mockito.doReturn("mockDeviceUser").when(mockDevice).getUserId(); + Mockito.doReturn("1689c70ffa245effc563017fee36d250").when(mockDevice) + .getAccessToken(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(acSessionHandler); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + req = request; + latch.countDown(); + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testAccountSignInOnRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived Sign In Test------------"); + IRequest request = makeSignInRequest(); + deviceServerSystem.onRequestReceived(mockDevice, request); + assertTrue(latch.await(1L, SECONDS)); + assertEquals(request, request); + } + + @Test + public void testAccountResourceOnRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived Sign Out Test------------"); + // sign up request from the client + IRequest request = makeSignOutRequest(); + deviceServerSystem.onRequestReceived(mockDevice, request); + // assertion : request msg to the AS is identical to the request msg + // from the client + assertTrue(latch.await(1L, SECONDS)); + assertTrue(hashmapCheck(req, "uid")); + assertTrue(hashmapCheck(req, "di")); + assertTrue(hashmapCheck(req, "accesstoken")); + assertTrue(hashmapCheck(req, "login")); + } + + private IRequest makeSignInRequest() { + Cbor> cbor = new Cbor>(); + IRequest request = null; + HashMap payloadData = new HashMap<>(); + payloadData.put("uid", "u0001"); + payloadData.put("di", di); + payloadData.put("accesstoken", "1689c70ffa245effc563017fee36d250"); + payloadData.put("login", true); + request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI, + null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + return request; + } + + private IRequest makeSignOutRequest() { + Cbor> cbor = new Cbor>(); + IRequest request = null; + HashMap payloadData = new HashMap<>(); + payloadData.put("login", false); + request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI, + null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + return request; + } + + private boolean hashmapCheck(IRequest request, String propertyName) { + Cbor> mCbor = new Cbor<>(); + HashMap payloadData = mCbor + .parsePayloadFromCbor(request.getPayload(), HashMap.class); + if (payloadData.get(propertyName) != null) + return true; + else + return false; + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountTest.java new file mode 100644 index 000000000..279a98fef --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountTest.java @@ -0,0 +1,111 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AccountTest { + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + public static final String ACCOUNT_URI = "/.well-known/ocf/account"; + private String authProvider = "github"; + private String authCode = "f6b55d8d8a27e0d32ab8"; + String userId = "testuser"; + private CoapDevice mockDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + + @InjectMocks + Account acHandler = new Account(); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + Mockito.doReturn("mockDeviceUser").when(mockDevice).getUserId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(acHandler); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + req = request; + latch.countDown(); + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testAccountDeviceDeleteOnRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived Device Delete Test------------"); + // sign up request from the client + IRequest request = null; + request = MessageBuilder.createRequest(RequestMethod.DELETE, + ACCOUNT_URI, "di=device1"); + deviceServerSystem.onRequestReceived(mockDevice, request); + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().get("uid").contains("mockDeviceUser")); + assertTrue(req.getUriQueryMap().get("di").contains("device1")); + assertTrue(req.getUriPath().equals(ACCOUNT_URI)); + } + + @Test + public void testAccountResourceOnRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived Sign Up Test------------"); + // sign up request from the client + IRequest request = null; + HashMap payloadData = new HashMap(); + payloadData.put("authcode", authCode); + payloadData.put("authprovider", authProvider); + payloadData.put("di", di); + Cbor> cbor = new Cbor>(); + request = MessageBuilder.createRequest(RequestMethod.POST, ACCOUNT_URI, + null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + deviceServerSystem.onRequestReceived(mockDevice, request); + // assertion : request msg to the AS is identical to the request msg + // from the client + assertTrue(latch.await(1L, SECONDS)); + assertEquals(request, req); + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java new file mode 100644 index 000000000..06071ea23 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java @@ -0,0 +1,199 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AclGroupTest { + private static final String TEST_RESOURCE_GROUP_URI = "/" + + OCFConstants.PREFIX_WELL_KNOWN + "/" + OCFConstants.PREFIX_OCF + + "/" + OCFConstants.ACL_URI + "/" + OCFConstants.GROUP_URI; + private CoapDevice mockDevice = mock( + CoapDevice.class); + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + AclGroup aclGroupHandler = new AclGroup(); + + @Before + public void setUp() throws Exception { + req = null; + Mockito.doReturn("mockUserId").when(mockDevice).getUserId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(aclGroupHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testCreateGroupRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Create Group Test------------"); + + HashMap payloadData = new HashMap<>(); + payloadData.put("gtype", "public"); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("gtype")); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("gmid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI); + } + + @Test + public void testAddMemberRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Add Member Test------------"); + + HashMap payloadData = new HashMap<>(); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, + ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("midlist")); + assertEquals(req.getUriPath(), + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); + } + + @Test + public void testAddDeviceRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Add Device Test------------"); + + HashMap payloadData = new HashMap<>(); + payloadData.put("dilist", Arrays.asList("sampleDevice")); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, + ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("dilist")); + assertEquals(req.getUriPath(), + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); + } + + @Test + public void testGetGroupListAndGetGroupInfoRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Get Group List & Get Group Info Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + TEST_RESOURCE_GROUP_URI, null, null, null); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("mid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI); + } + + @Test + public void testDeleteGroupRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Delete Group Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, + TEST_RESOURCE_GROUP_URI, "gid=samplegid", null, null); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("gid")); + assertTrue(req.getUriQueryMap().containsKey("gmid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI); + } + + @Test + public void testDeleteMemberRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Delete Member Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, null, + null); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("midlist")); + assertEquals(req.getUriPath(), + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); + } + + @Test + public void testDeleteDeviceRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Delete Member Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", + "dilist=sampledi", null, null); + aclGroupHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("dilist")); + assertEquals(req.getUriPath(), + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java new file mode 100644 index 000000000..5fe9c3272 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java @@ -0,0 +1,135 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AclInviteTest { + private static final String TEST_RESOURCE_INVITE_URI = "/" + + OCFConstants.PREFIX_WELL_KNOWN + "/" + OCFConstants.PREFIX_OCF + + "/" + OCFConstants.ACL_URI + "/" + OCFConstants.INVITE_URI; + private CoapDevice mockDevice = mock( + CoapDevice.class); + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch( + 1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + AclInvite aclInviteHandler = new AclInvite(); + + @Before + public void setUp() throws Exception { + req = null; + Mockito.doReturn("mockUserId").when(mockDevice).getUserId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(aclInviteHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testPostRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Post Message Test------------"); + + HashMap payloadData = new HashMap<>(); + HashMap invite = new HashMap<>(); + invite.put("gid", "g0001"); + invite.put("mid", "u0001"); + payloadData.put("invite", Arrays.asList(invite)); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_INVITE_URI, null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payloadData)); + aclInviteHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("uid")); + assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class) + .containsKey("invite")); + ArrayList> getinvite = (ArrayList>) cbor + .parsePayloadFromCbor(req.getPayload(), HashMap.class) + .get("invite"); + assertTrue(getinvite.get(0).containsKey("gid")); + assertTrue(getinvite.get(0).containsKey("mid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI); + } + + @Test + public void testGetRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Get Message Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + TEST_RESOURCE_INVITE_URI, null, null, null); + aclInviteHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("uid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI); + } + + @Test + public void testDeleteRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Delete Message Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, + TEST_RESOURCE_INVITE_URI, "gid=g0001", null, null); + aclInviteHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriQueryMap().containsKey("gid")); + assertTrue(req.getUriQueryMap().containsKey("uid")); + assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI); + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclTest.java new file mode 100644 index 000000000..8e5779517 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclTest.java @@ -0,0 +1,82 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class AclTest { + private static final String TEST_RESOURCE_ACI_URI = "/" + + OCFConstants.PREFIX_WELL_KNOWN + "/" + OCFConstants.PREFIX_OCF + + "/" + OCFConstants.ACL_URI; + private CoapDevice mockDevice = mock(CoapDevice.class); + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + Acl aclHandler = new Acl(); + + @Before + public void setUp() throws Exception { + req = null; + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(aclHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testOnDefaultRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS)Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_ACI_URI, null, null, null); + aclHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertEquals(req.getUriPath(), TEST_RESOURCE_ACI_URI); + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CertificateTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CertificateTest.java new file mode 100644 index 000000000..81c5f8648 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CertificateTest.java @@ -0,0 +1,84 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class CertificateTest { + private static final String TEST_RESOURCE_CERTI_URI = "/" + + OCFConstants.PREFIX_WELL_KNOWN + "/" + OCFConstants.PREFIX_OCF + + "/" + OCFConstants.CREDPROV_URI + "/" + OCFConstants.CERT_URI; + private CoapDevice mockDevice = mock( + CoapDevice.class); + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + Certificate certHandler = new Certificate(); + + @Before + public void setUp() throws Exception { + req = null; + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(certHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testOnDefaultRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_CERTI_URI, null, null, null); + certHandler.onRequestReceived(mockDevice, request); + + assertTrue(latch.await(1L, SECONDS)); + assertEquals(req.getUriPath(), TEST_RESOURCE_CERTI_URI); + } + +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueueTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueueTest.java new file mode 100644 index 000000000..b2f0f8534 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueueTest.java @@ -0,0 +1,84 @@ +package org.iotivity.cloud.ciserver.resources.proxy.mq; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class MessageQueueTest { + private static final String TEST_MQ_BROKER_URI = "/.well-known/ocf/ps/"; + String userId = "testuser"; + private CoapDevice mockDevice = null; + IResponse res = null; + IRequest req = null; + DeviceServerSystem deviceServerSystem; + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + @InjectMocks + MessageQueue mqHandler = new MessageQueue(); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + MockitoAnnotations.initMocks(this); + deviceServerSystem = new DeviceServerSystem(); + deviceServerSystem.addResource(mqHandler); + mockDevice = mock(CoapDevice.class); + // callback requestmsg mock from (IRequestChannel) server + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testMessageQueueResourceOnRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(MQ) Topic Creation Test------------"); + IRequest request = null; + HashMap tags = new HashMap(); + tags.put("topic", "DeviceServerSystemtestTopic"); + Cbor> cbor = new Cbor>(); + request = MessageBuilder.createRequest(RequestMethod.PUT, + TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(tags)); + deviceServerSystem.onRequestReceived(mockDevice, request); + // assertion : request msg to the AS is identical to the request msg + // from the client + assertTrue(latch.await(1L, SECONDS)); + assertEquals(request, req); + } + +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java new file mode 100644 index 000000000..758e61d6b --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java @@ -0,0 +1,215 @@ +package org.iotivity.cloud.ciserver.resources.proxy.rd; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.exception.ClientException; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.Observe; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class DevicePresenceTest { + public static final String DEVICE_PRS_REQ_URI = "/.well-known/ocf/prs"; + public static final String DEVICE_LIST_KEY = "devices"; + public static final String RES_PRS_URI = "/oic/ad"; + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + String userId = "testuser"; + private CoapDevice mockDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + @InjectMocks + DevicePresence prsHandler = new DevicePresence(); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(prsHandler); + Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId(); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return request; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + // @InjectMocks for testSpecificDeviceonResponseReceived + IRequest requestSpecificDevice = makePresenceSpecificDevice( + "device1"); + @InjectMocks + DevicePresence.AccountReceiveHandler SpecificDeviceHandler = prsHandler.new AccountReceiveHandler( + requestSpecificDevice, mockDevice); + + @Test + public void testSpecificDeviceonResponseReceived() throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Device Presence (specific deivce) Test------------"); + IResponse response = responseFromAccountServer(); + SpecificDeviceHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getObserve() == Observe.SUBSCRIBE); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(req.getMethod() == RequestMethod.GET); + } + + // @InjectMocks for testEntireDeviceonResponseReceived + IRequest requestEntireDevices = makePresenceEntireDevice(); + @InjectMocks + DevicePresence.AccountReceiveHandler EntireDeviceHandler = prsHandler.new AccountReceiveHandler( + requestEntireDevices, mockDevice); + + @Test + public void testEntireDeviceonResponseReceived() throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Device Presence (entire deivces) Test------------"); + IResponse response = responseFromAccountServer(); + EntireDeviceHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(queryMap.get("di").contains("device2")); + assertTrue(queryMap.get("di").contains("device3")); + assertTrue(req.getObserve() == Observe.SUBSCRIBE); + } + + @Test + public void testDevicePresenceObserverSpecificDeviceOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Device Presence (observe specific device) Test------------"); + IRequest request = makePresenceSpecificDevice(di); + deviceServerSystem.onRequestReceived(mockDevice, request); + assertTrue(latch.await(1L, SECONDS)); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(queryMap.containsKey("mid")); + assertTrue(req.getMethod().equals(RequestMethod.GET)); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived + IRequest requestDeregisterSpecificDevice = makeDeregisterSpecificDevice( + "device1"); + @InjectMocks + DevicePresence.AccountReceiveHandler DeregisterHandler = prsHandler.new AccountReceiveHandler( + requestDeregisterSpecificDevice, mockDevice); + + @Test + public void testPresenceDeregisterSpecificDeviceOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Device Presence (deregister specific device) Test------------"); + IResponse response = responseFromAccountServer(); + DeregisterHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(req.getObserve() == Observe.UNSUBSCRIBE); + } + + @Test + public void testDevicePresenceObserverEntireDevicesOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Device Presence (observe entire devices) Test------------"); + IRequest request = makePresenceEntireDevice(); + deviceServerSystem.onRequestReceived(mockDevice, request); + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getMethod().equals(RequestMethod.GET)); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(queryMap.containsKey("mid")); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + public IRequest makePresenceEntireDevice() { + IRequest request = null; + request = MessageBuilder.createRequest(RequestMethod.GET, + DEVICE_PRS_REQ_URI, null); + ((CoapRequest) request).setObserve(Observe.SUBSCRIBE); + return request; + } + + private IRequest makePresenceSpecificDevice(String di) { + String query = "di=" + di; + IRequest request = null; + request = MessageBuilder.createRequest(RequestMethod.GET, + DEVICE_PRS_REQ_URI, query); + ((CoapRequest) request).setObserve(Observe.SUBSCRIBE); + return request; + } + + private IRequest makeDeregisterSpecificDevice(String di) { + String query = "di=" + di; + IRequest request = null; + request = MessageBuilder.createRequest(RequestMethod.GET, + DEVICE_PRS_REQ_URI, query); + ((CoapRequest) request).setObserve(Observe.UNSUBSCRIBE); + return request; + } + + private IResponse responseFromAccountServer() { + // make response which has "CONTENT" status + Cbor> cbor = new Cbor<>(); + HashMap responsePayload = new HashMap(); + ArrayList deviceList = new ArrayList(); + // assuming that there are three devices in the response msg from + // the AS + deviceList.add("device1"); + deviceList.add("device2"); + deviceList.add("device3"); + responsePayload.put("dilist", deviceList); + responsePayload.put("gid", "g0001"); + responsePayload.put("gmid", "u0001"); + ArrayList midList = new ArrayList(); + midList.add("u0001"); + responsePayload.put("midlist", midList); + IRequest requestFromCitoAs = MessageBuilder.createRequest( + RequestMethod.GET, "/.well-known/ocf/acl/group/g0001", + "mid=null"); + IResponse response = MessageBuilder.createResponse(requestFromCitoAs, + ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(responsePayload)); + return response; + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java new file mode 100644 index 000000000..a7db30320 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java @@ -0,0 +1,137 @@ +package org.iotivity.cloud.ciserver.resources.proxy.rd; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.ciserver.Constants; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ResourceDirectoryTest { + private static final String TEST_RD_URI = OCFConstants.PREFIX_OIC + + "/" + OCFConstants.RD_URI; + public static final String DEVICE_PRS_REQ_URI = "/.well-known/ocf/prs"; + public static final String DEVICE_LIST_KEY = "devices"; + public static final String RES_PRS_URI = "/oic/ad"; + // private DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + String userId = "testuser"; + private CoapDevice mockDevice = null; + IResponse res = null; + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + + @InjectMocks + ResourceDirectory rdHandler = new ResourceDirectory(); + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(rdHandler); + mockDevice = mock(CoapDevice.class); + // callback requestmsg mock from (IRequestChannel) server + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return request; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + + } + + @Test + public void testRDResourceOnRequestReceived() throws Exception { + IRequest request = makeResourcePublishRequest(); + deviceServerSystem.onRequestReceived(mockDevice, request); + assertTrue(latch.await(1L, SECONDS)); + assertTrue(req.getUriPath() + .contains(Constants.PREFIX_WELL_KNOWN + "/" + + Constants.PREFIX_OCF + "/" + Constants.ACL_URI + "/" + + Constants.GROUP_URI + "/")); + } + + IRequest rdPublishRequest = makeResourcePublishRequest(); + + @InjectMocks + ResourceDirectory.AccountReceiveHandler accountReceiveHandler = rdHandler.new AccountReceiveHandler( + rdPublishRequest, mockDevice); + + @Test + public void testRDResourceOnResponseReceived() throws Exception { + IRequest request = makeResourcePublishRequest(); + deviceServerSystem.onRequestReceived(mockDevice, request); + // assertion : request msg to the AS is identical to the request msg + // from the client + accountReceiveHandler.onResponseReceived( + MessageBuilder.createResponse(request, ResponseStatus.CHANGED)); + assertEquals(req, rdPublishRequest); + assertTrue(latch.await(1L, SECONDS)); + } + + private IRequest makeResourcePublishRequest() { + HashMap payload = new HashMap<>(); + payload.put(Constants.DEVICE_ID, di); + ArrayList> publishLinks = new ArrayList<>(); + HashMap link = new HashMap<>(); + link.put("href", "/a/light"); + ArrayList rt = new ArrayList(); + rt.add("core.light"); + ArrayList itf = new ArrayList(); + itf.add("oic.if.baseline"); + HashMap policy = new HashMap<>(); + policy.put("bm", 5); + link.put("rt", rt); + link.put("if", itf); + link.put("p", policy); + publishLinks.add(link); + payload.put("links", publishLinks); + Cbor> cbor = new Cbor<>(); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RD_URI, "rt" + "=" + "oic.wk.rdpub", + ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(payload)); + return request; + } + +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java new file mode 100644 index 000000000..7fb9cf22d --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java @@ -0,0 +1,209 @@ +package org.iotivity.cloud.ciserver.resources.proxy.rd; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OCFConstants; +import org.iotivity.cloud.base.connector.ConnectorPool; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.exception.ClientException; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.coap.CoapResponse; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ResourceFindTest { + private static final String TEST_RESOURCE_FIND_URI = "/" + + OCFConstants.PREFIX_OIC + "/" + OCFConstants.WELL_KNOWN_URI; + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + private CoapDevice mockDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + ConnectorPool connectorPool = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + + @Mock + IRequestChannel requestChannel; + + @InjectMocks + ResourceFind resHandler = new ResourceFind(); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(resHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapResponse answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapResponse resp = (CoapResponse) args[0]; + res = resp; + return resp; + } + }).when(mockDevice).sendResponse(Mockito.anyObject()); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return null; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + // @InjectMocks for testSpecificDeviceonResponseReceived + IRequest requestSpecificDevice = MessageBuilder + .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, + "rt=core.light;di=" + "device1"); + @InjectMocks + ResourceFind.AccountReceiveHandler specificDeviceHandler = resHandler.new AccountReceiveHandler( + requestSpecificDevice, mockDevice); + + @Test + public void testSpecificDeviceonResponseReceived() + throws ClientException, InterruptedException { + System.out.println( + "\t--------------onResponseReceived(RD) Resource Find (specific deivce) Test------------"); + IResponse response = responseFromAccountServer(); + specificDeviceHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("rt").contains("core.light")); + assertTrue(queryMap.get("di").contains("device1")); + assertFalse(queryMap.get("di").contains("device2")); + assertFalse(queryMap.get("di").contains("device3")); + } + + // @InjectMocks for testEntireDeviceonResponseReceived + IRequest requestEntireDevices = MessageBuilder + .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, + "rt=core.light"); + @InjectMocks + ResourceFind.AccountReceiveHandler handler = resHandler.new AccountReceiveHandler( + requestEntireDevices, mockDevice); + + @Test + public void testEntireDeviceonResponseReceived() throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------"); + IResponse response = responseFromAccountServer(); + handler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("rt").contains("core.light")); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(queryMap.get("di").contains("device2")); + assertTrue(queryMap.get("di").contains("device3")); + } + + // @InjectMocks for testEntireDeviceonResponseReceived + IRequest requestEntireDevicesNoQuery = MessageBuilder + .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null); + @InjectMocks + ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = resHandler.new AccountReceiveHandler( + requestEntireDevicesNoQuery, mockDevice); + + @Test + public void testEntireDeviceNoQueryonResponseReceived() + throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Resource Find (entire deivces, No Query) Test------------"); + IResponse response = responseFromAccountServer(); + entireDevicesNoQueryHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(queryMap.get("di").contains("device2")); + assertTrue(queryMap.get("di").contains("device3")); + } + + @Test + public void testResourceFindEntireDevicesOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Resource Find (entire deivces) Test------------"); + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di); + resHandler.onRequestReceived(mockDevice, request); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(latch.await(1L, SECONDS)); + assertTrue(queryMap.containsKey("mid")); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + @Test + public void testResourceFindSpecificDeviceOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Resource Find (specific deivce) Test------------"); + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di); + resHandler.onRequestReceived(mockDevice, request); + HashMap> queryMap = req.getUriQueryMap(); + // assertion: if the request packet from the CI contains the query + // which includes device ID and the accesstoken + assertTrue(latch.await(1L, SECONDS)); + assertTrue(queryMap.containsKey("mid")); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + private IResponse responseFromAccountServer() { + // make response which has "CONTENT" status + Cbor> cbor = new Cbor<>(); + HashMap responsePayload = new HashMap(); + ArrayList deviceList = new ArrayList(); + // assuming that there are three devices in the response msg from the AS + deviceList.add("device1"); + deviceList.add("device2"); + deviceList.add("device3"); + responsePayload.put("dilist", deviceList); + responsePayload.put("gid", "g0001"); + responsePayload.put("gmid", "u0001"); + ArrayList midList = new ArrayList(); + midList.add("u0001"); + responsePayload.put("midlist", midList); + IResponse response = MessageBuilder.createResponse(requestEntireDevices, + ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(responsePayload)); + return response; + } +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java new file mode 100644 index 000000000..d7f2ff9a8 --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java @@ -0,0 +1,165 @@ +package org.iotivity.cloud.ciserver.resources.proxy.rd; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.exception.ClientException; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.IResponse; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.ContentFormat; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.base.protocols.enums.ResponseStatus; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.iotivity.cloud.util.Cbor; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class ResourcePresenceTest { + public static final String DEVICE_PRS_REQ_URI = "/.well-known/ocf/prs"; + public static final String DEVICE_LIST_KEY = "devices"; + public static final String RES_PRS_URI = "/oic/ad"; + private String di = "B371C481-38E6-4D47-8320-7688D8A5B58C"; + private CoapDevice mockDevice = mock(CoapDevice.class); + IResponse res = null; + IRequest req = null; + DeviceServerSystem deviceServerSystem = new DeviceServerSystem(); + final CountDownLatch latch = new CountDownLatch(1); + @Mock + IRequestChannel requestChannel; + @InjectMocks + ResourcePresence adHandler = new ResourcePresence(); + + @Before + public void setUp() throws Exception { + res = null; + req = null; + Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId(); + MockitoAnnotations.initMocks(this); + deviceServerSystem.addResource(adHandler); + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + req = request; + latch.countDown(); + return request; + } + }).when(requestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testResourcePresenceEntireResourcesOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------"); + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + RES_PRS_URI, null); + deviceServerSystem.onRequestReceived(mockDevice, request); + HashMap> queryMap = req.getUriQueryMap(); + // assertion: if the request packet from the CI contains the query + // which includes the accesstoken and the di + assertTrue(latch.await(1L, SECONDS)); + assertTrue(queryMap.containsKey("mid")); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + @Test + public void testResourcePresenceSpecificResourceOnRequestReceived() + throws Exception { + System.out.println( + "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------"); + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + RES_PRS_URI, "di=" + di); + deviceServerSystem.onRequestReceived(mockDevice, request); + HashMap> queryMap = req.getUriQueryMap(); + assertTrue(latch.await(1L, SECONDS)); + assertTrue(queryMap.containsKey("mid")); + assertEquals(req.getUriPath(), "/.well-known/ocf/acl/group/null"); + } + + IRequest requestEntireDevices = MessageBuilder + .createRequest(RequestMethod.GET, RES_PRS_URI, null); + @InjectMocks + ResourcePresence.AccountReceiveHandler entireDeviceHandler = adHandler.new AccountReceiveHandler( + requestEntireDevices, mockDevice); + + @Test + public void testEntireDeviceonResponseReceived() throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------"); + IResponse responseFromAccountServer = responseFromAccountServer(); + entireDeviceHandler.onResponseReceived(responseFromAccountServer); + HashMap> queryMap = req.getUriQueryMap(); + // assertion : if query has pre-requested multiple di list given from + // the AS + assertTrue(req.getMethod() == RequestMethod.GET); + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(queryMap.get("di").contains("device2")); + assertTrue(queryMap.get("di").contains("device3")); + } + + IRequest requestSpecificDevice = MessageBuilder + .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1"); + @InjectMocks + ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler( + requestSpecificDevice, mockDevice); + + @Test + public void testSpecificDeviceonResponseReceived() throws ClientException { + System.out.println( + "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------"); + IResponse response = responseFromAccountServer(); + specificDeviceHandler.onResponseReceived(response); + HashMap> queryMap = req.getUriQueryMap(); + // assertion : if query has pre-requested di + assertTrue(queryMap.get("di").contains("device1")); + assertTrue(req.getMethod() == RequestMethod.GET); + } + + private IResponse responseFromAccountServer() { + // make response which has "CONTENT" status + Cbor> cbor = new Cbor<>(); + HashMap responsePayload = new HashMap(); + ArrayList deviceList = new ArrayList(); + // assuming that there are three devices in the response msg from the AS + deviceList.add("device1"); + deviceList.add("device2"); + deviceList.add("device3"); + responsePayload.put("dilist", deviceList); + responsePayload.put("gid", "g0001"); + responsePayload.put("gmid", "u0001"); + ArrayList midList = new ArrayList(); + midList.add("u0001"); + responsePayload.put("midlist", midList); + IResponse response = MessageBuilder.createResponse(requestEntireDevices, + ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, + cbor.encodingPayloadToCbor(responsePayload)); + return response; + } +}