fixed CI unit test. 35/10535/19
authorJung Seungho <shonest.jung@samsung.com>
Tue, 23 Aug 2016 09:20:28 +0000 (18:20 +0900)
committerJee Hyeok Kim <jihyeok13.kim@samsung.com>
Tue, 23 Aug 2016 22:00:01 +0000 (22:00 +0000)
1. Code Coverage up to 84.5%.
2. Fixed DeviceServerSystem and Constants.
3. Add the Apache License comment.

Change-Id: I2e70a87e7179daee227e2ec703e9720400eeb0d3
Signed-off-by: Jung Seungho <shonest.jung@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10535
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
28 files changed:
cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResourceTest.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/CloudInterfaceServer.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/DiResource.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/KeepAliveResource.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java
cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/DeviceServerSystemTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/DiResourceTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CertificateTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/mq/MessageQueueTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectoryTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java
cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java

index b214564..2f7600d 100644 (file)
@@ -52,7 +52,7 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 public class InviteResourceTest {
-    private static final String           INVITE_URI        = "/.well-known/ocf/acl/invite";
+    private static final String           INVITE_URI        = Constants.INVITE_FULL_URI;
 
     private static final String           TEST_INVITE_USER  = "u0001";
     private static final String           TEST_INVITED_USER = "u0002";
@@ -141,7 +141,8 @@ public class InviteResourceTest {
                 if (latchInviteUser.getCount() == 0) {
 
                     assertEquals(resp.getStatus(), ResponseStatus.CONTENT);
-                    assertTrue(checkResponsePayload(resp, Constants.RESP_INVITE));
+                    assertTrue(
+                            checkResponsePayload(resp, Constants.RESP_INVITE));
                     assertTrue(checkInviteInfo(resp.getPayload(),
                             Constants.RESP_INVITE, TEST_GROUP_ID,
                             TEST_INVITED_USER));
@@ -178,8 +179,8 @@ public class InviteResourceTest {
                 if (latchInvitedUser.getCount() == 0) {
 
                     assertEquals(resp.getStatus(), ResponseStatus.CONTENT);
-                    assertTrue(checkResponsePayload(resp,
-                            Constants.RESP_INVITED));
+                    assertTrue(
+                            checkResponsePayload(resp, Constants.RESP_INVITED));
                     assertTrue(checkInviteInfo(resp.getPayload(),
                             Constants.RESP_INVITED, TEST_GROUP_ID,
                             TEST_INVITE_USER));
@@ -277,8 +278,8 @@ public class InviteResourceTest {
     private void deleteInvitation(String gid, String uid, String mid) {
 
         String uriQuery = Constants.REQ_GROUP_ID + "=" + gid + ";"
-                + Constants.REQ_UUID_ID + "=" + uid + ";"
-                + Constants.REQ_MEMBER + "=" + mid;
+                + Constants.REQ_UUID_ID + "=" + uid + ";" + Constants.REQ_MEMBER
+                + "=" + mid;
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
                 INVITE_URI, uriQuery);
@@ -286,7 +287,8 @@ public class InviteResourceTest {
         mInviteResource.onDefaultRequestReceived(mMockDevice, request);
     }
 
-    private void getInvitation(String uid, CoapDevice device, Observe observeOpt) {
+    private void getInvitation(String uid, CoapDevice device,
+            Observe observeOpt) {
 
         String uriQuery = Constants.REQ_UUID_ID + "=" + uid;
         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
@@ -314,10 +316,11 @@ public class InviteResourceTest {
                 mCbor.encodingPayloadToCbor(payload));
     }
 
-    private boolean checkResponsePayload(IResponse response, String propertyName) {
+    private boolean checkResponsePayload(IResponse response,
+            String propertyName) {
 
-        HashMap<String, Object> payloadData = mCbor.parsePayloadFromCbor(
-                response.getPayload(), HashMap.class);
+        HashMap<String, Object> payloadData = mCbor
+                .parsePayloadFromCbor(response.getPayload(), HashMap.class);
 
         return payloadData.get(propertyName) != null ? true : false;
     }
@@ -325,8 +328,8 @@ public class InviteResourceTest {
     private boolean checkInviteInfo(byte[] payload, String propertyKey,
             String gid, String mid) {
 
-        HashMap<String, Object> paredPayload = mCbor.parsePayloadFromCbor(
-                payload, HashMap.class);
+        HashMap<String, Object> paredPayload = mCbor
+                .parsePayloadFromCbor(payload, HashMap.class);
 
         @SuppressWarnings("unchecked")
         ArrayList<HashMap<String, Object>> invitationList = (ArrayList<HashMap<String, Object>>) paredPayload
index 41f0e83..70d7f22 100755 (executable)
@@ -57,12 +57,15 @@ public class CloudInterfaceServer {
 
         boolean tlsMode = Integer.parseInt(args[7]) == 1;
 
-        ConnectorPool.addConnection("rd", new InetSocketAddress(args[1],
-                Integer.parseInt(args[2])), tlsMode);
-        ConnectorPool.addConnection("account", new InetSocketAddress(args[3],
-                Integer.parseInt(args[4])), tlsMode);
-        ConnectorPool.addConnection("mq", new InetSocketAddress(args[5],
-                Integer.parseInt(args[6])), tlsMode);
+        ConnectorPool.addConnection("rd",
+                new InetSocketAddress(args[1], Integer.parseInt(args[2])),
+                tlsMode);
+        ConnectorPool.addConnection("account",
+                new InetSocketAddress(args[3], Integer.parseInt(args[4])),
+                tlsMode);
+        ConnectorPool.addConnection("mq",
+                new InetSocketAddress(args[5], Integer.parseInt(args[6])),
+                tlsMode);
 
         DeviceServerSystem deviceServer = new DeviceServerSystem();
 
@@ -102,21 +105,21 @@ public class CloudInterfaceServer {
 
         deviceServer.addResource(aclInviteHandler);
 
-        KeepAliveResource resKeepAlive = new KeepAliveResource(new int[] { 1,
-                2, 4, 8 });
+        KeepAliveResource resKeepAlive = new KeepAliveResource(
+                new int[] { 1, 2, 4, 8 });
 
         deviceServer.addResource(resKeepAlive);
 
         deviceServer.addResource(new DiResource(devicePool));
 
-        deviceServer.addServer(new CoapServer(new InetSocketAddress(Integer
-                .parseInt(args[0]))));
+        deviceServer.addServer(new CoapServer(
+                new InetSocketAddress(Integer.parseInt(args[0]))));
 
         // deviceServer.addServer(new HttpServer(new InetSocketAddress(8080)));
 
         deviceServer.startSystem(tlsMode);
 
-        resKeepAlive.startSessionChecker();
+        resKeepAlive.startSessionChecker(3000, 6000);
 
         Scanner in = new Scanner(System.in);
 
index 4e0b584..005c700 100644 (file)
@@ -34,13 +34,18 @@ public class Constants extends OICConstants {
     public static final String REQ_LOGIN           = "login";
 
     public static final String ACCESS_TOKEN        = "accesstoken";
-    public static final Object REFRESH_TOKEN       = "refreshtoken";
-    public static final Object EXPIRES_IN          = "expiresin";
+    public static final String REFRESH_TOKEN       = "refreshtoken";
+    public static final String AUTH_CODE           = "authcode";
+    public static final String AUTH_PROVIDER       = "authprovider";
+    public static final String EXPIRES_IN          = "expiresin";
 
     public static final String REQ_GROUP_MASTER_ID = "gmid";
     public static final String REQ_MEMBER_ID       = "mid";
     public static final String REQ_MEMBER_LIST     = "midlist";
     public static final String REQ_DEVICE_ID       = "di";
     public static final String REQ_DEVICE_LIST     = "dilist";
+    public static final String REQ_INVITE          = "invite";
+    public static final String REQ_PING_ARRAY      = "inarray";
+    public static final String REQ_PING            = "in";
 
 }
index 20d7684..14e2479 100644 (file)
@@ -51,6 +51,12 @@ import io.netty.channel.ChannelPromise;
 
 public class DeviceServerSystem extends ServerSystem {
 
+    IRequestChannel mRDServer = null;
+
+    public DeviceServerSystem() {
+        mRDServer = ConnectorPool.getConnection("rd");
+    }
+
     public class CoapDevicePool {
         HashMap<String, Device> mMapDevice = new HashMap<>();
 
@@ -135,17 +141,16 @@ public class DeviceServerSystem extends ServerSystem {
             }
         }
 
-        private void sendDevicePresence(String deviceId, String state) {
+        public void sendDevicePresence(String deviceId, String state) {
 
             Cbor<HashMap<String, Object>> cbor = new Cbor<>();
-            IRequestChannel RDServer = ConnectorPool.getConnection("rd");
             HashMap<String, Object> payload = new HashMap<String, Object>();
             payload.put(Constants.REQ_DEVICE_ID, deviceId);
             payload.put(Constants.PRESENCE_STATE, state);
             StringBuffer uriPath = new StringBuffer();
             uriPath.append("/" + Constants.PREFIX_OIC);
             uriPath.append("/" + Constants.DEVICE_PRESENCE_URI);
-            RDServer.sendRequest(MessageBuilder.createRequest(
+            mRDServer.sendRequest(MessageBuilder.createRequest(
                     RequestMethod.POST, uriPath.toString(), null,
                     ContentFormat.APPLICATION_CBOR,
                     cbor.encodingPayloadToCbor(payload)), null);
@@ -180,14 +185,17 @@ public class DeviceServerSystem extends ServerSystem {
                 switch (response.getUriPath()) {
 
                     case OICConstants.ACCOUNT_SESSION_FULL_URI:
+                        HashMap<String, Object> payloadData = mCbor
+                                .parsePayloadFromCbor(response.getPayload(),
+                                        HashMap.class);
 
                         if (response.getStatus() != ResponseStatus.CHANGED) {
                             throw new UnAuthorizedException();
                         }
 
-                        HashMap<String, Object> payloadData = mCbor
-                                .parsePayloadFromCbor(response.getPayload(),
-                                        HashMap.class);
+                        if (payloadData == null) {
+                            throw new BadRequestException("payload is empty");
+                        }
                         int remainTime = (int) payloadData
                                 .get(Constants.EXPIRES_IN);
 
@@ -226,9 +234,6 @@ public class DeviceServerSystem extends ServerSystem {
                 // And check first response is VALID then add or cut
                 CoapRequest request = (CoapRequest) msg;
 
-                HashMap<String, Object> authPayload = null;
-                Device device = null;
-
                 switch (request.getUriPath()) {
                     // Check whether request is about account
                     case OICConstants.ACCOUNT_FULL_URI:
@@ -236,7 +241,7 @@ public class DeviceServerSystem extends ServerSystem {
 
                         if (ctx.channel().attr(keyDevice).get() == null) {
                             // Create device first and pass to upperlayer
-                            device = new CoapDevice(ctx);
+                            Device device = new CoapDevice(ctx);
                             ctx.channel().attr(keyDevice).set(device);
                         }
 
@@ -244,16 +249,21 @@ public class DeviceServerSystem extends ServerSystem {
 
                     case OICConstants.ACCOUNT_SESSION_FULL_URI:
 
-                        authPayload = mCbor.parsePayloadFromCbor(
-                                request.getPayload(), HashMap.class);
+                        HashMap<String, Object> authPayload = mCbor
+                                .parsePayloadFromCbor(request.getPayload(),
+                                        HashMap.class);
 
-                        device = ctx.channel().attr(keyDevice).get();
+                        Device device = ctx.channel().attr(keyDevice).get();
 
                         if (device == null) {
                             device = new CoapDevice(ctx);
                             ctx.channel().attr(keyDevice).set(device);
                         }
 
+                        if (authPayload == null) {
+                            throw new BadRequestException("payload is empty");
+                        }
+
                         ((CoapDevice) device).updateDevice(
                                 (String) authPayload.get(Constants.DEVICE_ID),
                                 (String) authPayload.get(Constants.USER_ID),
index ebca10f..ae83028 100644 (file)
@@ -31,6 +31,7 @@ import org.iotivity.cloud.base.device.Device;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.device.IResponseEventHandler;
 import org.iotivity.cloud.base.exception.ServerException;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
 import org.iotivity.cloud.base.exception.ServerException.NotFoundException;
 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
 import org.iotivity.cloud.base.protocols.IRequest;
@@ -129,6 +130,9 @@ public class DiResource extends Resource {
             if (response.getStatus() == ResponseStatus.CONTENT) {
                 linkPayload = mCbor.parsePayloadFromCbor(response.getPayload(),
                         ArrayList.class);
+                if (linkPayload == null) {
+                    throw new BadRequestException("payload is null");
+                }
                 convertHref(linkPayload);
             }
 
index 4790d9c..5432f9a 100644 (file)
@@ -49,10 +49,10 @@ import org.iotivity.cloud.util.Cbor;
  *
  */
 public class KeepAliveResource extends Resource {
-    private int[]                          mIntervals      = null;
-    private Timer                          mTimer          = new Timer();
-    private Cbor<HashMap<String, Integer>> mCbor           = new Cbor<>();
-    private HashMap<Device, Long>          mConnectionPool = new HashMap<>();
+    private int[]                         mIntervals      = null;
+    private Timer                         mTimer          = new Timer();
+    private Cbor<HashMap<String, Object>> mCbor           = new Cbor<>();
+    private HashMap<Device, Long>         mConnectionPool = new HashMap<>();
 
     public KeepAliveResource(int[] intervals) {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.KEEP_ALIVE_URI));
@@ -82,8 +82,8 @@ public class KeepAliveResource extends Resource {
         srcDevice.sendResponse(response);
     }
 
-    public void startSessionChecker() {
-        mTimer.schedule(new KeepAliveTask(), 30000, 60000);
+    public void startSessionChecker(int startTime, int intervalTime) {
+        mTimer.schedule(new KeepAliveTask(), startTime, intervalTime);
     }
 
     public void stopSessionChecker() {
@@ -99,7 +99,7 @@ public class KeepAliveResource extends Resource {
     private IResponse handleGetPingConfig(IRequest request) {
 
         HashMap<String, int[]> payloadData = new HashMap<>();
-        payloadData.put("inarray", mIntervals);
+        payloadData.put(Constants.REQ_PING_ARRAY, mIntervals);
 
         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
                 ContentFormat.APPLICATION_CBOR,
@@ -108,13 +108,15 @@ public class KeepAliveResource extends Resource {
 
     private IResponse handlePutPingConfig(Device srcDevice, IRequest request) {
 
-        HashMap<String, Integer> payloadData = mCbor
+        HashMap<String, Object> payloadData = mCbor
                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
 
-        if (payloadData.containsKey("in")) {
-            mConnectionPool.put(srcDevice, System.currentTimeMillis()
-                    + (payloadData.get("in") * (long) 60000));
-        }
+        checkPayloadException(Constants.REQ_PING, payloadData);
+
+        Long pingTime = Integer.valueOf(
+                payloadData.get(Constants.REQ_PING).toString()) * (long) 60000;
+        Long connectionTime = System.currentTimeMillis() + pingTime;
+        mConnectionPool.put(srcDevice, connectionTime);
 
         return MessageBuilder.createResponse(request, ResponseStatus.VALID);
     }
@@ -135,7 +137,7 @@ public class KeepAliveResource extends Resource {
                 Long currentTime = System.currentTimeMillis();
                 for (Device device : map.keySet()) {
                     Long lifeTime = (Long) map.get(device);
-                    if (lifeTime < currentTime) {
+                    if (lifeTime != null && lifeTime < currentTime) {
                         deleteList.add(device);
                     }
                 }
index 497d139..66c9018 100644 (file)
@@ -26,20 +26,56 @@ import java.util.Arrays;
 import org.iotivity.cloud.base.connector.ConnectorPool;
 import org.iotivity.cloud.base.device.Device;
 import org.iotivity.cloud.base.device.IRequestChannel;
+import org.iotivity.cloud.base.device.IResponseEventHandler;
+import org.iotivity.cloud.base.exception.ClientException;
 import org.iotivity.cloud.base.exception.ServerException;
 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.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
 import org.iotivity.cloud.base.resource.Resource;
 import org.iotivity.cloud.ciserver.Constants;
 
 public class Account extends Resource {
-    IRequestChannel mAuthServer = null;
+    IRequestChannel mASServer = null;
 
     public Account() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.ACCOUNT_URI));
 
-        mAuthServer = ConnectorPool.getConnection("account");
+        mASServer = ConnectorPool.getConnection("account");
+    }
+
+    class AccountReceiveHandler implements IResponseEventHandler {
+
+        IRequestChannel  mRDServer = null;
+        private Device   mSrcDevice;
+        private IRequest mRequest;
+
+        public AccountReceiveHandler(IRequest request, Device srcDevice) {
+            mRDServer = ConnectorPool.getConnection("rd");
+            mSrcDevice = srcDevice;
+            mRequest = request;
+        }
+
+        @Override
+        public void onResponseReceived(IResponse response)
+                throws ClientException {
+            switch (response.getStatus()) {
+                case DELETED:
+                    StringBuffer uriPath = new StringBuffer();
+                    uriPath.append(Constants.PREFIX_OIC + "/");
+                    uriPath.append(Constants.RD_URI);
+                    mRDServer.sendRequest(
+                            MessageBuilder.createRequest(RequestMethod.POST,
+                                    uriPath.toString(), mRequest.getUriQuery()),
+                            mSrcDevice);
+                    break;
+                default:
+                    mSrcDevice.sendResponse(MessageBuilder.createResponse(
+                            mRequest, ResponseStatus.BAD_REQUEST));
+            }
+        }
     }
 
     @Override
@@ -54,6 +90,7 @@ public class Account extends Resource {
             request = MessageBuilder.modifyRequest(request, null, uriQuery,
                     null, null);
         }
-        mAuthServer.sendRequest(request, srcDevice);
+        mASServer.sendRequest(request,
+                new AccountReceiveHandler(request, srcDevice));
     }
 }
index c46d609..2722bcb 100644 (file)
@@ -51,6 +51,9 @@ public class AccountSession extends Resource {
             throws ServerException {
         HashMap<String, Object> payloadData = mCbor
                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
+
+        checkPayloadException(Constants.REQ_LOGIN, payloadData);
+
         if (payloadData.get(Constants.REQ_LOGIN).toString().equals("false")) {
             payloadData.put(Constants.USER_ID, srcDevice.getUserId());
             payloadData.put(Constants.DEVICE_ID, srcDevice.getDeviceId());
index ad3e344..8a690d0 100644 (file)
@@ -57,12 +57,16 @@ public class AclGroup extends Resource {
                 HashMap<String, Object> payloadData = mCbor
                         .parsePayloadFromCbor(request.getPayload(),
                                 HashMap.class);
+                if (payloadData == null) {
+                    throw new BadRequestException("payload is null");
+                }
                 if (getUriPathSegments()
                         .containsAll(request.getUriPathSegments())) {
                     payloadData.put(Constants.REQ_GROUP_MASTER_ID,
                             srcDevice.getUserId());
                 } else {
                     if (payloadData.isEmpty()) {
+                        payloadData = new HashMap<>();
                         payloadData.put(Constants.REQ_MEMBER_LIST,
                                 Arrays.asList(srcDevice.getUserId()));
                     }
index 9f0437d..54e46af 100644 (file)
@@ -56,14 +56,16 @@ public class AclInvite extends Resource {
             case POST:
 
                 Cbor<HashMap<String, Object>> cbor = new Cbor<>();
-                HashMap<String, Object> payload = cbor.parsePayloadFromCbor(
+                HashMap<String, Object> payloadData = cbor.parsePayloadFromCbor(
                         request.getPayload(), HashMap.class);
 
-                payload.put(Constants.USER_ID, srcDevice.getUserId());
+                checkPayloadException(Constants.REQ_INVITE, payloadData);
+
+                payloadData.put(Constants.USER_ID, srcDevice.getUserId());
 
                 request = MessageBuilder.modifyRequest(request, null, null,
                         ContentFormat.APPLICATION_CBOR,
-                        cbor.encodingPayloadToCbor(payload));
+                        cbor.encodingPayloadToCbor(payloadData));
                 break;
 
             default:
index d5fb060..5f9e0e2 100644 (file)
@@ -72,6 +72,12 @@ public class DevicePresence extends Resource {
                             .parsePayloadFromCbor(response.getPayload(),
                                     HashMap.class);
 
+                    if (payloadData == null) {
+                        mSrcDevice.sendResponse(MessageBuilder.createResponse(
+                                mRequest, ResponseStatus.BAD_REQUEST));
+                        return;
+                    }
+
                     if (mRequest.getUriQuery() != null
                             && mRequest.getUriQueryMap()
                                     .containsKey(Constants.REQ_DEVICE_ID)) {
index b151e1f..97f2f1e 100644 (file)
@@ -29,35 +29,34 @@ import org.iotivity.cloud.base.device.Device;
 import org.iotivity.cloud.base.device.IRequestChannel;
 import org.iotivity.cloud.base.device.IResponseEventHandler;
 import org.iotivity.cloud.base.exception.ClientException;
-import org.iotivity.cloud.base.exception.ClientException.BadResponseException;
 import org.iotivity.cloud.base.exception.ServerException;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
 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.enums.ContentFormat;
 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
+import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
 import org.iotivity.cloud.base.resource.Resource;
 import org.iotivity.cloud.ciserver.Constants;
 import org.iotivity.cloud.util.Cbor;
 
 public class ResourceDirectory extends Resource {
     private Cbor<HashMap<String, Object>> mCbor     = new Cbor<>();
+    IRequestChannel                       mRDServer = null;
     IRequestChannel                       mASServer = null;
 
     public ResourceDirectory() {
         super(Arrays.asList(Constants.PREFIX_OIC, Constants.RD_URI));
-
+        mRDServer = ConnectorPool.getConnection("rd");
         mASServer = ConnectorPool.getConnection("account");
     }
 
     class AccountReceiveHandler implements IResponseEventHandler {
-
-        IRequestChannel  mRDServer = null;
         private Device   mSrcDevice;
         private IRequest mRequest;
 
         public AccountReceiveHandler(IRequest request, Device srcDevice) {
-            mRDServer = ConnectorPool.getConnection("rd");
             mSrcDevice = srcDevice;
             mRequest = request;
         }
@@ -68,14 +67,12 @@ public class ResourceDirectory extends Resource {
 
             switch (response.getStatus()) {
                 case CHANGED:
-
                     mRDServer.sendRequest(mRequest, mSrcDevice);
                     break;
 
                 default:
-                    throw new BadResponseException(
-                            response.getStatus().toString()
-                                    + " response type is not supported");
+                    mSrcDevice.sendResponse(MessageBuilder.createResponse(
+                            mRequest, ResponseStatus.BAD_REQUEST));
             }
         }
     }
@@ -83,26 +80,39 @@ public class ResourceDirectory extends Resource {
     @Override
     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
             throws ServerException {
-
-        StringBuffer uriPath = new StringBuffer();
-        uriPath.append(Constants.PREFIX_OIC + "/");
-        uriPath.append(Constants.ACL_URI + "/");
-        uriPath.append(Constants.GROUP_URI + "/");
-        uriPath.append(srcDevice.getUserId());
-
-        HashMap<String, Object> payloadData = mCbor
-                .parsePayloadFromCbor(request.getPayload(), HashMap.class);
-
-        String di = payloadData.get(Constants.REQ_DEVICE_ID).toString();
-
-        HashMap<String, Object> requestPayload = new HashMap<>();
-
-        requestPayload.put(Constants.REQ_DEVICE_LIST, Arrays.asList(di));
-        IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.POST,
-                uriPath.toString(), null, ContentFormat.APPLICATION_CBOR,
-                mCbor.encodingPayloadToCbor(requestPayload));
-
-        mASServer.sendRequest(requestToAS,
-                new AccountReceiveHandler(request, srcDevice));
+        switch (request.getMethod()) {
+            case POST:
+                HashMap<String, Object> payloadData = mCbor
+                        .parsePayloadFromCbor(request.getPayload(),
+                                HashMap.class);
+
+                StringBuffer uriPath = new StringBuffer();
+                uriPath.append(Constants.PREFIX_OIC + "/");
+                uriPath.append(Constants.ACL_URI + "/");
+                uriPath.append(Constants.GROUP_URI + "/");
+                uriPath.append(srcDevice.getUserId());
+
+                String di = payloadData.get(Constants.REQ_DEVICE_ID).toString();
+
+                HashMap<String, Object> requestPayload = new HashMap<>();
+
+                requestPayload.put(Constants.REQ_DEVICE_LIST,
+                        Arrays.asList(di));
+                IRequest requestToAS = MessageBuilder.createRequest(
+                        RequestMethod.POST, uriPath.toString(), null,
+                        ContentFormat.APPLICATION_CBOR,
+                        mCbor.encodingPayloadToCbor(requestPayload));
+
+                mASServer.sendRequest(requestToAS,
+                        new AccountReceiveHandler(request, srcDevice));
+                break;
+
+            case DELETE:
+                mRDServer.sendRequest(request, srcDevice);
+                break;
+            default:
+                throw new BadRequestException(
+                        request.getMethod() + " request type is not support");
+        }
     }
 }
index 091713a..f5e73ba 100644 (file)
@@ -71,6 +71,12 @@ public class ResourceFind extends Resource {
                             .parsePayloadFromCbor(response.getPayload(),
                                     HashMap.class);
 
+                    if (payloadData == null) {
+                        mSrcDevice.sendResponse(MessageBuilder.createResponse(
+                                mRequest, ResponseStatus.BAD_REQUEST));
+                        return;
+                    }
+
                     if (mRequest.getUriQuery() != null
                             && mRequest.getUriQueryMap()
                                     .containsKey(Constants.REQ_DEVICE_ID)) {
index ddf7a54..f8e232b 100644 (file)
@@ -71,6 +71,12 @@ public class ResourcePresence extends Resource {
                             .parsePayloadFromCbor(response.getPayload(),
                                     HashMap.class);
 
+                    if (payloadData == null) {
+                        mSrcDevice.sendResponse(MessageBuilder.createResponse(
+                                mRequest, ResponseStatus.BAD_REQUEST));
+                        return;
+                    }
+
                     if (mRequest.getUriQuery() != null
                             && mRequest.getUriQueryMap()
                                     .containsKey(Constants.REQ_DEVICE_ID)) {
index 484e172..cdfb1e5 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -16,8 +38,10 @@ 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.base.server.CoapServer;
 import org.iotivity.cloud.base.server.HttpServer;
 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
@@ -27,6 +51,7 @@ 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.iotivity.cloud.util.Log;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.InjectMocks;
@@ -38,50 +63,104 @@ import org.mockito.stubbing.Answer;
 
 import io.netty.channel.Channel;
 import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelId;
+import io.netty.channel.ChannelPromise;
 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(
+    private ChannelHandlerContext                   mCtx                  = null;
+    private String                                  mDi                   = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    private String                                  mUserId               = "testuser";
+    private String                                  mAccessToken          = "1689c70ffa245effc563017fee36d250";
+    private Device                                  mDevice               = mock(
             Device.class);
-    IResponse                               res                  = null;
-    IRequest                                req                  = null;
-    DeviceServerSystem                      deviceServerSystem   = new DeviceServerSystem();
-    final CountDownLatch                    latch                = new CountDownLatch(
+    private CoapDevice                              mMockDevice           = mock(
+            CoapDevice.class);
+    private IResponse                               mRes                  = null;
+    private IRequest                                mReq                  = null;
+    final CountDownLatch                            mLatch                = new CountDownLatch(
             1);
-
+    private Cbor<HashMap<Object, Object>>           mCbor                 = new Cbor<>();
     @Mock
-    IRequestChannel                         requestChannel;
+    private IRequestChannel                         mRequestChannel;
 
     @InjectMocks
-    DeviceServerSystem.CoapLifecycleHandler coapLifecycleHandler = deviceServerSystem.new CoapLifecycleHandler();
+    private DeviceServerSystem                      mDeviceServerSystem   = new DeviceServerSystem();
     @InjectMocks
-    DeviceServerSystem.CoapAuthHandler      coapAuthHandler      = deviceServerSystem.new CoapAuthHandler();
+    private DeviceServerSystem.CoapLifecycleHandler mCoapLifecycleHandler = mDeviceServerSystem.new CoapLifecycleHandler();
+    @InjectMocks
+    private DeviceServerSystem.CoapAuthHandler      mCoapAuthHandler      = mDeviceServerSystem.new CoapAuthHandler();
 
     @Before
     public void setUp() throws Exception {
+        Log.createfile();
         MockitoAnnotations.initMocks(this);
+        mRes = null;
+        mReq = null;
+        mCtx = mock(ChannelHandlerContext.class);
+        Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
+        Channel channel = mock(Channel.class);
+        Attribute<Device> attribute = mock(Attribute.class);
+        ChannelId channelId = mock(ChannelId.class);
+        Mockito.doReturn(channel).when(mCtx).channel();
+        Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
+        Mockito.doReturn(channelId).when(channel).id();
+        Mockito.doReturn(
+                "0000000141f3edcfc2c3000000000001d0000000000000000000000000000000000000")
+                .when(channelId).asLongText();
+        Mockito.doReturn(mMockDevice).when(attribute).get();
+        Mockito.doReturn("sampleDeviceID").when(mMockDevice).getDeviceId();
+        Mockito.doAnswer(new Answer<Object>() {
+            @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());
+                mReq = request;
+                mLatch.countDown();
+                return null;
+            }
+        }).when(mCtx).fireChannelRead(Mockito.any());
+
+        Mockito.doAnswer(new Answer<Object>() {
+            @Override
+            public CoapResponse answer(InvocationOnMock invocation)
+                    throws Throwable {
+                Object[] args = invocation.getArguments();
+                CoapResponse response = (CoapResponse) args[0];
+                System.out.println(
+                        "\t----------payload : " + response.getPayloadString());
+                System.out.println(
+                        "\t----------status : " + response.getStatus());
+                mRes = response;
+                mLatch.countDown();
+                return null;
+            }
+        }).when(mCtx).writeAndFlush(Mockito.any());
+
     }
 
-    @Before
+    @Test
     public void testAddHttpServer() throws Exception {
         HttpServer httpServer = new HttpServer(null);
-        deviceServerSystem.addServer(httpServer);
+        mDeviceServerSystem.addServer(httpServer);
     }
 
-    @Before
+    @Test
     public void testAddCoapServer() throws Exception {
         CoapServer coapServer = new CoapServer(null);
-        deviceServerSystem.addServer(coapServer);
+        mDeviceServerSystem.addServer(coapServer);
     }
 
     @Test
     public void testGetDevicePool() throws Exception {
-        CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
+        CoapDevicePool devicePool = mDeviceServerSystem.getDevicePool();
         if (devicePool != null) {
             System.out.println("devicePool returned :" + devicePool);
         }
@@ -90,24 +169,24 @@ public class DeviceServerSystemTest {
     @Test
     public void testAddDevice() throws Exception {
         CoapDevice coapDevice = new CoapDevice(null);
-        coapDevice.updateDevice(di, userId, accessToken);
-        CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
+        coapDevice.updateDevice(mDi, mUserId, mAccessToken);
+        CoapDevicePool devicePool = mDeviceServerSystem.getDevicePool();
         devicePool.addDevice(coapDevice);
     }
 
     @Test
     public void testRemoveNotRegisteredDevice() throws Exception {
         CoapDevice coapDevice = new CoapDevice(null);
-        coapDevice.updateDevice(di, userId, accessToken);
-        CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
+        coapDevice.updateDevice(mDi, mUserId, mAccessToken);
+        CoapDevicePool devicePool = mDeviceServerSystem.getDevicePool();
         devicePool.removeDevice(coapDevice);
     }
 
     @Test
     public void testRemoveDevice() throws Exception {
         CoapDevice coapDevice = new CoapDevice(null);
-        coapDevice.updateDevice(di, userId, accessToken);
-        CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
+        coapDevice.updateDevice(mDi, mUserId, mAccessToken);
+        CoapDevicePool devicePool = mDeviceServerSystem.getDevicePool();
         devicePool.addDevice(coapDevice);
         devicePool.removeDevice(coapDevice);
     }
@@ -115,15 +194,15 @@ public class DeviceServerSystemTest {
     @Test
     public void testQueryDevice() throws Exception {
         CoapDevice coapDevice = new CoapDevice(null);
-        coapDevice.updateDevice(di, userId, accessToken);
-        CoapDevicePool devicePool = deviceServerSystem.getDevicePool();
+        coapDevice.updateDevice(mDi, mUserId, mAccessToken);
+        CoapDevicePool devicePool = mDeviceServerSystem.getDevicePool();
         devicePool.addDevice(coapDevice);
-        devicePool.queryDevice(di);
+        devicePool.queryDevice(mDi);
     }
 
     @Test
     public void testStopSystem() throws Exception {
-        deviceServerSystem.stopSystem();
+        mDeviceServerSystem.stopSystem();
     }
 
     @Test
@@ -133,113 +212,241 @@ public class DeviceServerSystemTest {
         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);
+        mDeviceServerSystem.addResource(acHandler);
+        mDeviceServerSystem.addResource(rdHandler);
+        mDeviceServerSystem.addResource(resHandler);
+        mDeviceServerSystem.addResource(adHandler);
+        mDeviceServerSystem.addResource(mqHandler);
     }
 
     @Test
-    public void testChannelRead() throws InterruptedException {
-        ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
+    public void coapAuthHandlerAccountCtxNullChannelReadRequest()
+            throws InterruptedException {
+        System.out.println(
+                "\t--------------coapAuthHandler Account ctx is null ChannelReadRequest Test------------");
+        Channel channel = mock(Channel.class);
+        Attribute<Device> attribute = mock(Attribute.class);
+        Mockito.doReturn(channel).when(mCtx).channel();
+        Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
+        Mockito.doReturn(null).when(attribute).get();
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put(Constants.DEVICE_ID, "sampleDevice");
+
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_FULL_URI, null,
+                ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
+        mCoapAuthHandler.channelRead(mCtx, request);
+
+        assertEquals(mReq.getUriPath(), OICConstants.ACCOUNT_FULL_URI);
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.DEVICE_ID));
+    }
+
+    @Test
+    public void coapAuthHandlerPingChannelReadRequest()
+            throws InterruptedException {
+        System.out.println(
+                "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------");
+
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.KEEP_ALIVE_FULL_URI, null, null, null);
+        mCoapAuthHandler.channelRead(mCtx, request);
+
+        assertEquals(mReq.getUriPath(), OICConstants.KEEP_ALIVE_FULL_URI);
+    }
 
-        // inject mocked coapDevice into the api
+    @Test
+    public void coapAuthHandlerAccountSessionChannelReadRequest()
+            throws InterruptedException {
+        System.out.println(
+                "\t--------------coapAuthHandler AccountSession ChannelReadRequest Test------------");
+
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put(Constants.DEVICE_ID, mDi);
+        payloadData.put(Constants.USER_ID, mAccessToken);
+        payloadData.put(Constants.ACCESS_TOKEN, mAccessToken);
+
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_SESSION_FULL_URI, null,
+                ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
+        mCoapAuthHandler.channelRead(mCtx, request);
+
+        assertEquals(mReq.getUriPath(), OICConstants.ACCOUNT_SESSION_FULL_URI);
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.DEVICE_ID));
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.USER_ID));
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.ACCESS_TOKEN));
+    }
+
+    @Test
+    public void coapAuthHandlerAccountSessionCtxNullChannelReadRequest()
+            throws InterruptedException {
+        System.out.println(
+                "\t--------------coapAuthHandler AccountSession ctx is null ChannelReadRequest Test------------");
         Channel channel = mock(Channel.class);
         Attribute<Device> attribute = mock(Attribute.class);
-        Mockito.doReturn(channel).when(ctx).channel();
+        Mockito.doReturn(channel).when(mCtx).channel();
         Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
-        Mockito.doReturn(mockDevice).when(attribute).get();
+        Mockito.doReturn(null).when(attribute).get();
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put(Constants.DEVICE_ID, mDi);
+        payloadData.put(Constants.USER_ID, mAccessToken);
+        payloadData.put(Constants.ACCESS_TOKEN, mAccessToken);
 
-        IRequest request = MessageBuilder.createRequest(RequestMethod.GET, null,
-                null);
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_SESSION_FULL_URI, null,
+                ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
+        mCoapAuthHandler.channelRead(mCtx, request);
+
+        assertEquals(mReq.getUriPath(), OICConstants.ACCOUNT_SESSION_FULL_URI);
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.DEVICE_ID));
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.USER_ID));
+        assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
+                .containsKey(Constants.ACCESS_TOKEN));
+        mLatch.countDown();
+    }
 
-                Object[] args = invocation.getArguments();
-                IRequest req = (IRequest) args[0];
+    @Test
+    public void coapAuthHandlerUnAuthorizedExceptionChannelReadRequest()
+            throws Exception {
 
-                assertEquals(req, request);
+        System.out.println(
+                "\t--------------coapAuthHandler UnAuthorizedException ChannelReadRequest Test------------");
 
-                latch.countDown();
-                return null;
-            }
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_SESSION_FULL_URI + "/" + "errorURI", null,
+                null, null);
+        mCoapAuthHandler.channelRead(mCtx, request);
+        assertEquals(mRes.getStatus(), ResponseStatus.UNAUTHORIZED);
+    }
 
-        }).when(ctx).fireChannelRead(Mockito.anyObject());
-        coapLifecycleHandler.channelRead(ctx, request);
-        assertTrue(latch.await(1L, SECONDS));
+    @Test
+    public void coapAuthHandlerAccountSessionChannelReadResponse()
+            throws Exception {
+        System.out.println(
+                "\t--------------coapAuthHandler AccountSession ChannelReadResponse Test------------");
 
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_SESSION_FULL_URI, null, null, null);
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put(Constants.EXPIRES_IN, 3300);
+        IResponse response = MessageBuilder.createResponse(request,
+                ResponseStatus.CHANGED, ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
+        ChannelPromise channelPromise = null;
+        mCoapAuthHandler.write(mCtx, response, channelPromise);
+        assertEquals(((IRequest) mRes).getUriPath(),
+                OICConstants.ACCOUNT_SESSION_FULL_URI);
+        assertTrue(mCbor.parsePayloadFromCbor(mRes.getPayload(), HashMap.class)
+                .containsKey(Constants.EXPIRES_IN));
     }
 
     @Test
-    public void coapAuthHandlerAccountChannelReadRequest()
+    public void coapAuthHandlerUnAuthorizedExceptionChannelReadResponse()
+            throws Exception {
+        System.out.println(
+                "\t--------------coapAuthHandler UnAuthorizedException ChannelReadResponse Test------------");
+
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
+                OICConstants.ACCOUNT_SESSION_FULL_URI, null, null, null);
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put(Constants.EXPIRES_IN, -1);
+        IResponse response = MessageBuilder.createResponse(request,
+                ResponseStatus.UNAUTHORIZED, ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
+        ChannelPromise channelPromise = null;
+        mCoapAuthHandler.write(mCtx, response, channelPromise);
+        assertEquals(mRes.getStatus(), ResponseStatus.UNAUTHORIZED);
+    }
+
+    @Test
+    public void CoapLifecycleHandlerChannelReadRequest()
             throws InterruptedException {
         System.out.println(
-                "\t--------------coapAuthHandler Account ChannelReadRequest Test------------");
-        ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
-        Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
+                "\t--------------CoapLifecycleHandler ChannelReadCoapRequest Test------------");
+
+        mCoapLifecycleHandler.channelRead(mCtx,
+                MessageBuilder.createRequest(RequestMethod.GET, null, null));
+        assertTrue(mLatch.await(1L, SECONDS));
+    }
+
+    @Test
+    public void CoapLifecycleHandlerUnAuthorizedExceptionChannelReadRequest()
+            throws Exception {
+        System.out.println(
+                "\t--------------CoapLifecycleHandler ChannelReadCoapRequest Test------------");
         Channel channel = mock(Channel.class);
         Attribute<Device> attribute = mock(Attribute.class);
-        Mockito.doReturn(channel).when(ctx).channel();
+        Mockito.doReturn(channel).when(mCtx).channel();
         Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
+        Mockito.doReturn(mMockDevice).when(attribute).get();
+        Mockito.doReturn(true).when(mMockDevice).isExpiredTime();
+        ChannelId channelId = mock(ChannelId.class);
+        Mockito.doReturn(channelId).when(channel).id();
+        Mockito.doReturn(
+                "0000000141f3edcfc2c3000000000001d0000000000000000000000000000000000000")
+                .when(channelId).asLongText();
+        mCoapLifecycleHandler.channelRead(mCtx,
+                MessageBuilder.createRequest(RequestMethod.GET, null, null));
+        assertEquals(mRes.getStatus(), ResponseStatus.UNAUTHORIZED);
+    }
 
+    @Test
+    public void CoapLifecycleHandlerchannelActive() throws Exception {
+        System.out.println(
+                "\t--------------CoapLifecycleHandler ChannelReadCoapRequest    Test------------");
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
                     throws Throwable {
+
                 Object[] args = invocation.getArguments();
                 CoapRequest req = (CoapRequest) args[0];
-                assertEquals(req.getUriPath(), Constants.ACCOUNT_FULL_URI);
-                assertTrue(cbor
+                assertTrue(req.getUriPath().contains(Constants.PREFIX_OIC + "/"
+                        + Constants.DEVICE_PRESENCE_URI));
+                assertTrue(mCbor
                         .parsePayloadFromCbor(req.getPayload(), HashMap.class)
-                        .containsKey("di"));
-                latch.countDown();
+                        .containsKey(Constants.PRESENCE_STATE));
+
+                mLatch.countDown();
                 return null;
             }
-        }).when(ctx).fireChannelRead(Mockito.any());
 
-        HashMap<String, Object> payloadData = new HashMap<>();
-        payloadData.put("di", "sampleDevice");
-
-        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
-                Constants.ACCOUNT_FULL_URI, null,
-                ContentFormat.APPLICATION_CBOR,
-                cbor.encodingPayloadToCbor(payloadData));
-        coapAuthHandler.channelRead(ctx, request);
+        }).when(mRequestChannel).sendRequest(Mockito.any(), Mockito.any());
+        mCoapLifecycleHandler.channelActive(mCtx);
     }
 
     @Test
-    public void coapAuthHandlerPingChannelReadRequest()
-            throws InterruptedException {
+    public void CoapLifecycleHandlerchannelInActive() throws Exception {
         System.out.println(
-                "\t--------------coapAuthHandler Ping ChannelReadRequest Test------------");
-        ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
-        Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
-        Channel channel = mock(Channel.class);
-        Attribute<Device> attribute = mock(Attribute.class);
-        Mockito.doReturn(channel).when(ctx).channel();
-        Mockito.doReturn(attribute).when(channel).attr(Mockito.any());
+                "\t--------------CoapLifecycleHandler ChannelReadCoapRequest    Test------------");
 
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
                     throws Throwable {
+
                 Object[] args = invocation.getArguments();
                 CoapRequest req = (CoapRequest) args[0];
-                assertEquals(req.getUriPath(), "/" + OICConstants.PREFIX_OIC
-                        + "/" + OICConstants.KEEP_ALIVE_URI);
+                assertTrue(req.getUriPath().contains(Constants.PREFIX_OIC + "/"
+                        + Constants.DEVICE_PRESENCE_URI));
+                assertTrue(mCbor
+                        .parsePayloadFromCbor(req.getPayload(), HashMap.class)
+                        .containsKey(Constants.PRESENCE_STATE));
 
-                latch.countDown();
+                mLatch.countDown();
                 return null;
             }
-        }).when(ctx).fireChannelRead(Mockito.any());
 
-        IRequest request = MessageBuilder
-                .createRequest(RequestMethod.POST,
-                        "/" + OICConstants.PREFIX_OIC + "/"
-                                + OICConstants.KEEP_ALIVE_URI,
-                        null, null, null);
-        coapAuthHandler.channelRead(ctx, request);
+        }).when(mRequestChannel).sendRequest(Mockito.any(), Mockito.any());
+        mCoapLifecycleHandler.channelInactive(mCtx);
     }
+
 }
index 6bffe55..da98817 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -5,6 +27,7 @@ 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;
 
@@ -18,6 +41,7 @@ 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.Constants;
 import org.iotivity.cloud.ciserver.DeviceServerSystem;
 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
 import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler;
@@ -33,47 +57,52 @@ 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(
+    private static final String           RELAY_URI              = "/di";
+    private static final String           RESOURCE_URI           = "/a/light/0";
+    private String                        mDiServer              = "resourceServerId";
+    private CoapDevice                    mSourceDevice          = mock(
+            CoapDevice.class);
+    private CoapDevice                    mTargetDevice          = mock(
+            CoapDevice.class);
+    private IResponse                     mRes                   = null;
+    private IRequest                      mReq                   = null;
+    private DeviceServerSystem            mDeviceServerSystem    = new DeviceServerSystem();
+    private final CountDownLatch          mLatch                 = new CountDownLatch(
+            1);
+    private Cbor<HashMap<String, Object>> mCbor                  = new Cbor<>();
+    private IRequestChannel               mTargetChannel         = mock(
             IRequestChannel.class);
 
     @Mock
-    CoapDevicePool              coapDevicePool;
+    CoapDevicePool                        coapDevicePool;
 
     @Mock
-    IRequestChannel             requestChannel;
+    IRequestChannel                       requestChannel;
 
     @InjectMocks
-    DiResource                  diHandler              = new DiResource(
+    DiResource                            diHandler              = new DiResource(
             coapDevicePool);
 
     @InjectMocks
-    LinkInterfaceHandler        linkInterfaceHandler   = diHandler.new LinkInterfaceHandler(
-            "targetDeviceId", sourceDevice);
+    LinkInterfaceHandler                  linkInterfaceHandler   = diHandler.new LinkInterfaceHandler(
+            "targetDeviceId", mSourceDevice);
 
     @InjectMocks
-    DefaultResponseHandler      defaultResponseHandler = diHandler.new DefaultResponseHandler(
-            "targetDeviceId", sourceDevice);
+    DefaultResponseHandler                defaultResponseHandler = diHandler.new DefaultResponseHandler(
+            "targetDeviceId", mSourceDevice);
 
     @Before
     public void setUp() throws Exception {
-        res = null;
-        req = null;
-        Mockito.doReturn("sourceDeviceId").when(sourceDevice).getDeviceId();
-        Mockito.doReturn("targetDeviceId").when(targetDevice).getDeviceId();
+        mRes = null;
+        mReq = null;
+        Mockito.doReturn("sourceDeviceId").when(mSourceDevice).getDeviceId();
+        Mockito.doReturn("targetDeviceId").when(mTargetDevice).getDeviceId();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(diHandler);
-        Mockito.doReturn(targetDevice).when(coapDevicePool)
+        mDeviceServerSystem.addResource(diHandler);
+        Mockito.doReturn(mTargetDevice).when(coapDevicePool)
                 .queryDevice(Mockito.anyString());
-        Mockito.doReturn(targetChannel).when(targetDevice).getRequestChannel();
+        Mockito.doReturn(mTargetChannel).when(mTargetDevice)
+                .getRequestChannel();
 
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
@@ -82,11 +111,11 @@ public class DiResourceTest {
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapResponse resp = (CoapResponse) args[0];
-                res = resp;
-                latch.countDown();
+                mRes = resp;
+                mLatch.countDown();
                 return null;
             }
-        }).when(sourceDevice).sendResponse(Mockito.anyObject());
+        }).when(mSourceDevice).sendResponse(Mockito.anyObject());
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
@@ -99,60 +128,103 @@ public class DiResourceTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(targetChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mTargetChannel).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));
+        diHandler.onDefaultRequestReceived(mSourceDevice, request);
+        assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
+        assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
+        assertTrue(mLatch.await(1L, SECONDS));
     }
 
-    // TODO treat oic.if.ll resource response (on LinkInterfaceHandler)
+    @Test
+    public void testOnLinkInterfaceRequestReceived()
+            throws InterruptedException {
+        IRequest request = makePutLinkInterfaceRequest();
+        diHandler.onLinkInterfaceRequestReceived(mSourceDevice, request);
+        assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
+        assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
+        assertTrue(mLatch.await(1L, SECONDS));
+    }
 
     @Test
     public void testOnDefaultResponseHandleronResponseReceived()
             throws InterruptedException {
         IResponse response = makeContentResponse();
         defaultResponseHandler.onResponseReceived(response);
-        assertEquals(res, response);
-        assertTrue(latch.await(1L, SECONDS));
+        assertEquals(mRes, response);
+        assertTrue(mLatch.await(1L, SECONDS));
+    }
+
+    @Test
+    public void testOnLinkInterfaceResponseHandleronResponseReceived()
+            throws InterruptedException {
+        IResponse response = makeContentLinkResponse();
+        linkInterfaceHandler.onResponseReceived(response);
+        assertEquals(mRes, response);
+        assertTrue(mLatch.await(1L, SECONDS));
     }
 
     private IRequest makePutRequest() {
-        Cbor<HashMap<String, Object>> cbor = new Cbor<>();
         HashMap<String, Object> payloadData = new HashMap<>();
         payloadData.put("state", true);
         payloadData.put("power", 6);
         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
-                RELAY_URI + "/" + diServer + RESOURCE_URI, null,
+                RELAY_URI + "/" + mDiServer + RESOURCE_URI, null,
                 ContentFormat.APPLICATION_CBOR,
-                cbor.encodingPayloadToCbor(payloadData));
+                mCbor.encodingPayloadToCbor(payloadData));
+        return request;
+    }
+
+    private IRequest makePutLinkInterfaceRequest() {
+        HashMap<String, Object> payloadData = new HashMap<>();
+        payloadData.put("state", true);
+        payloadData.put("power", 6);
+        IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
+                RELAY_URI + "/" + mDiServer + RESOURCE_URI,
+                Constants.RS_INTERFACE + "=" + Constants.LINK_INTERFACE,
+                ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(payloadData));
         return request;
     }
 
     private IResponse makeContentResponse() {
-        Cbor<HashMap<String, Object>> cbor = new Cbor<>();
+
         HashMap<String, Object> payloadData = new HashMap<>();
         payloadData.put("state", true);
         payloadData.put("power", 6);
         IResponse response = MessageBuilder.createResponse(makeGetRequest(),
                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
-                cbor.encodingPayloadToCbor(payloadData));
+                mCbor.encodingPayloadToCbor(payloadData));
+        return response;
+    }
+
+    private IResponse makeContentLinkResponse() {
+        HashMap<String, Object> payloadData = new HashMap<>();
+        ArrayList<HashMap<String, Object>> linkPayload = new ArrayList<>();
+
+        payloadData.put("href", "hrefsample1");
+        linkPayload.add(payloadData);
+        linkPayload.add(payloadData);
+        linkPayload.add(payloadData);
+
+        IResponse response = MessageBuilder.createResponse(makeGetRequest(),
+                ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
+                mCbor.encodingPayloadToCbor(linkPayload));
         return response;
     }
 
     private IRequest makeGetRequest() {
         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
-                RELAY_URI + "/" + diServer + RESOURCE_URI, null);
+                RELAY_URI + "/" + mDiServer + RESOURCE_URI, null);
         return request;
     }
 
index 33747ec..1f29a41 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources;
 
 import static org.junit.Assert.assertEquals;
@@ -6,8 +28,10 @@ import static org.mockito.Mockito.mock;
 
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.device.CoapDevice;
+import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
 import org.iotivity.cloud.base.protocols.IRequest;
 import org.iotivity.cloud.base.protocols.IResponse;
 import org.iotivity.cloud.base.protocols.MessageBuilder;
@@ -26,18 +50,21 @@ import org.mockito.stubbing.Answer;
 
 public class KeepAliveResourceTest {
 
-    KeepAliveResource                                 keepAliveResource;
-    CoapDevice                                        mockDevice;
-    private Cbor<HashMap<String, ArrayList<Integer>>> mCbor    = new Cbor<>();
-    IResponse                                         res;
-    int[]                                             interval = { 1, 2, 4, 8 };
+    private KeepAliveResource                         keepAliveResource;
+    private CoapDevice                                mockDevice;
+    private Cbor<HashMap<String, ArrayList<Integer>>> mCbor     = new Cbor<>();
+    private final CountDownLatch                      mLatch    = new CountDownLatch(
+            1);
+    private IResponse                                 mRes;
+    private int[]                                     mInterval = { 1, 2, 4,
+            8 };
 
     @Before
     public void setUp() throws Exception {
-        keepAliveResource = new KeepAliveResource(interval);
+        keepAliveResource = new KeepAliveResource(mInterval);
         MockitoAnnotations.initMocks(this);
         mockDevice = mock(CoapDevice.class);
-        res = null;
+        mRes = null;
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -49,11 +76,11 @@ public class KeepAliveResourceTest {
                         .println("\t----payload : " + resp.getPayloadString());
                 System.out
                         .println("\t----responsestatus : " + resp.getStatus());
-                res = resp;
+                mRes = resp;
                 return null;
             }
         }).when(mockDevice).sendResponse(Mockito.anyObject());
-        keepAliveResource.startSessionChecker();
+        keepAliveResource.startSessionChecker(0, 3000);
     }
 
     @After
@@ -67,7 +94,7 @@ public class KeepAliveResourceTest {
                 "/oic/ping", null);
         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
         HashMap<String, ArrayList<Integer>> payloadData = mCbor
-                .parsePayloadFromCbor(res.getPayload(), HashMap.class);
+                .parsePayloadFromCbor(mRes.getPayload(), HashMap.class);
         HashMap<String, ArrayList<Integer>> comparisonData = new HashMap<>();
         ArrayList<Integer> comparisonArray = new ArrayList<>();
         comparisonArray.add(1);
@@ -76,7 +103,7 @@ public class KeepAliveResourceTest {
         comparisonArray.add(8);
         comparisonData.put("inarray", comparisonArray);
         assertEquals(payloadData, comparisonData);
-        assertTrue(methodCheck(res, ResponseStatus.CONTENT));
+        assertTrue(methodCheck(mRes, ResponseStatus.CONTENT));
     }
 
     @Test
@@ -88,27 +115,18 @@ public class KeepAliveResourceTest {
                 "/oic/ping", null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
         keepAliveResource.onDefaultRequestReceived(mockDevice, request);
-        assertTrue(methodCheck(res, ResponseStatus.VALID));
+        assertTrue(methodCheck(mRes, ResponseStatus.VALID));
     }
 
-    // TODO : exception response assertion
-    // @Test
-    public void testOnDefaultPutEmptyPayloadRequestReceived() {
+    @Test(expected = BadRequestException.class)
+    public void testOnDefaultExceptionRequestReceived() {
         HashMap<String, Integer> payloadData = new HashMap<>();
         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
-        IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
+        payloadData.put("in", 8);
+        IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
                 "/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,
index a86b560..d80d24a 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -30,38 +52,38 @@ 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        = Constants.ACCOUNT_SESSION_FULL_URI;
-    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);
+    private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    public static final String   SESSION_URI         = Constants.ACCOUNT_SESSION_FULL_URI;
+    private CoapDevice           mMockDevice         = mock(CoapDevice.class);
+    private IResponse            mRes                = null;
+    private IRequest             mReq                = null;
+    private ConnectorPool        mConnectorPool      = null;
+    private DeviceServerSystem   mDeviceServerSystem = new DeviceServerSystem();
+    private final CountDownLatch mLatch              = new CountDownLatch(1);
     @Mock
-    IRequestChannel            requestChannel;
+    private IRequestChannel      mRequestChannel;
 
     @InjectMocks
-    AccountSession             acSessionHandler   = new AccountSession();
+    private AccountSession       mAcSessionHandler   = 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)
+        mRes = null;
+        mReq = null;
+        Mockito.doReturn(mDi).when(mMockDevice).getDeviceId();
+        Mockito.doReturn("mockDeviceUser").when(mMockDevice).getUserId();
+        Mockito.doReturn("1689c70ffa245effc563017fee36d250").when(mMockDevice)
                 .getAccessToken();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(acSessionHandler);
+        mDeviceServerSystem.addResource(mAcSessionHandler);
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapRequest request = (CoapRequest) args[0];
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 System.out.println(
                         "\t----------payload : " + request.getPayloadString());
                 System.out.println(
@@ -70,7 +92,7 @@ public class AccountSessionTest {
                         "\t---------uriquery : " + request.getUriQuery());
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -79,8 +101,8 @@ public class AccountSessionTest {
         System.out.println(
                 "\t--------------OnRequestReceived Sign In Test------------");
         IRequest request = makeSignInRequest();
-        deviceServerSystem.onRequestReceived(mockDevice, request);
-        assertTrue(latch.await(1L, SECONDS));
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
         assertEquals(request, request);
     }
 
@@ -90,24 +112,25 @@ public class AccountSessionTest {
                 "\t--------------OnRequestReceived Sign Out Test------------");
         // sign up request from the client
         IRequest request = makeSignOutRequest();
-        deviceServerSystem.onRequestReceived(mockDevice, request);
+        mDeviceServerSystem.onRequestReceived(mMockDevice, 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"));
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(hashmapCheck(mReq, Constants.USER_ID));
+        assertTrue(hashmapCheck(mReq, Constants.DEVICE_ID));
+        assertTrue(hashmapCheck(mReq, Constants.ACCESS_TOKEN));
+        assertTrue(hashmapCheck(mReq, Constants.REQ_LOGIN));
     }
 
     private IRequest makeSignInRequest() {
         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
         IRequest request = null;
         HashMap<String, Object> payloadData = new HashMap<>();
-        payloadData.put("uid", "u0001");
-        payloadData.put("di", di);
-        payloadData.put("accesstoken", "1689c70ffa245effc563017fee36d250");
-        payloadData.put("login", true);
+        payloadData.put(Constants.USER_ID, "u0001");
+        payloadData.put(Constants.DEVICE_ID, mDi);
+        payloadData.put(Constants.ACCESS_TOKEN,
+                "1689c70ffa245effc563017fee36d250");
+        payloadData.put(Constants.REQ_LOGIN, true);
         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
                 null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
@@ -118,7 +141,7 @@ public class AccountSessionTest {
         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
         IRequest request = null;
         HashMap<String, Object> payloadData = new HashMap<>();
-        payloadData.put("login", false);
+        payloadData.put(Constants.REQ_LOGIN, false);
         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
                 null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
index 193898e..b73c34d 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -12,11 +34,11 @@ 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;
@@ -30,38 +52,38 @@ 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        = Constants.ACCOUNT_FULL_URI;
-    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;
+    private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    public static final String   ACCOUNT_URI         = Constants.ACCOUNT_FULL_URI;
+    private String               mAuthProvider       = "github";
+    private String               mAuthCode           = "f6b55d8d8a27e0d32ab8";
+    private CoapDevice           mMockDevice         = mock(CoapDevice.class);
+    private IRequest             mReqRDServer        = null;
+    private IRequest             mReqASServer        = null;
+    ConnectorPool                connectorPool       = null;
+    private DeviceServerSystem   mDeviceServerSystem = new DeviceServerSystem();
+    private final CountDownLatch mLatch              = new CountDownLatch(1);
+    @Mock(name = "mRDServer")
+    IRequestChannel              mRequestChannelRDServer;
+    @Mock(name = "mASServer")
+    IRequestChannel              mRequestChannelASServer;
 
     @InjectMocks
-    Account                    acHandler          = new Account();
+    private Account              mAcHandler          = new Account();
 
     @Before
     public void setUp() throws Exception {
-        res = null;
-        req = null;
-        Mockito.doReturn("mockDeviceUser").when(mockDevice).getUserId();
+        Mockito.doReturn("mockDeviceUser").when(mMockDevice).getUserId();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(acHandler);
+        mDeviceServerSystem.addResource(mAcHandler);
+
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapRequest request = (CoapRequest) args[0];
-                req = request;
-                latch.countDown();
+                mReqRDServer = request;
+                mLatch.countDown();
                 System.out.println(
                         "\t----------payload : " + request.getPayloadString());
                 System.out.println(
@@ -70,23 +92,64 @@ public class AccountTest {
                         "\t---------uriquery : " + request.getUriQuery());
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
-                Mockito.any(CoapDevice.class));
+        }).when(mRequestChannelRDServer).sendRequest(
+                Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
+
+        Mockito.doAnswer(new Answer<Object>() {
+            @Override
+            public CoapRequest answer(InvocationOnMock invocation)
+                    throws Throwable {
+                Object[] args = invocation.getArguments();
+                CoapRequest request = (CoapRequest) args[0];
+                mReqASServer = request;
+                mLatch.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(mRequestChannelASServer).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));
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReqASServer.getUriQueryMap().get("uid")
+                .contains("mockDeviceUser"));
+        assertTrue(mReqASServer.getUriQueryMap().get("di").contains("device1"));
+        assertTrue(mReqASServer.getUriPath().equals(ACCOUNT_URI));
+    }
+
+    IRequest                      asRequest             = MessageBuilder
+            .createRequest(RequestMethod.DELETE, ACCOUNT_URI, "di=device1");
+
+    @InjectMocks
+    Account.AccountReceiveHandler accountReceiveHandler = mAcHandler.new AccountReceiveHandler(
+            asRequest, mMockDevice);
+
+    @Test
+    public void testAccountDeviceDeleteOnResponseReceived() throws Exception {
+        System.out.println(
+                "\t--------------OnResponseReceived Device Delete Test------------");
+        IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+                ACCOUNT_URI, "di=device1");
+
+        accountReceiveHandler.onResponseReceived(
+                MessageBuilder.createResponse(request, ResponseStatus.DELETED));
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReqRDServer.getUriQueryMap().get("di").contains("device1"));
+        assertTrue(mReqRDServer.getUriPath()
+                .contains(Constants.PREFIX_OIC + "/" + Constants.RD_URI));
     }
 
     @Test
@@ -96,17 +159,17 @@ public class AccountTest {
         // sign up request from the client
         IRequest request = null;
         HashMap<String, String> payloadData = new HashMap<String, String>();
-        payloadData.put("authcode", authCode);
-        payloadData.put("authprovider", authProvider);
-        payloadData.put("di", di);
+        payloadData.put(Constants.AUTH_CODE, mAuthCode);
+        payloadData.put(Constants.AUTH_PROVIDER, mAuthProvider);
+        payloadData.put(Constants.DEVICE_ID, mDi);
         Cbor<HashMap<String, String>> cbor = new Cbor<HashMap<String, String>>();
         request = MessageBuilder.createRequest(RequestMethod.POST, ACCOUNT_URI,
                 null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
-        deviceServerSystem.onRequestReceived(mockDevice, request);
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
         // assertion : request msg to the AS is identical to the request msg
         // from the client
-        assertTrue(latch.await(1L, SECONDS));
-        assertEquals(request, req);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertEquals(request, mReqASServer);
     }
 }
index 47fd821..30b3a44 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -9,9 +31,9 @@ import java.util.Arrays;
 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.exception.ServerException.BadRequestException;
 import org.iotivity.cloud.base.protocols.IRequest;
 import org.iotivity.cloud.base.protocols.MessageBuilder;
 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
@@ -30,27 +52,27 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 public class AclGroupTest {
-    private static final String TEST_RESOURCE_GROUP_URI = Constants.GROUP_FULL_URI;
+    private static final String  TEST_RESOURCE_GROUP_URI = Constants.GROUP_FULL_URI;
 
-    private CoapDevice          mockDevice              = mock(
+    private CoapDevice           mMockDevice             = mock(
             CoapDevice.class);
-    IRequest                    req                     = null;
-    ConnectorPool               connectorPool           = null;
-    DeviceServerSystem          deviceServerSystem      = new DeviceServerSystem();
-    final CountDownLatch        latch                   = new CountDownLatch(1);
+    private IRequest             mReq                    = null;
+    private DeviceServerSystem   mDeviceServerSystem     = new DeviceServerSystem();
+    private final CountDownLatch mLatch                  = new CountDownLatch(
+            1);
 
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel      mRequestChannel;
 
     @InjectMocks
-    AclGroup                    aclGroupHandler         = new AclGroup();
+    private AclGroup             mAclGroupHandler        = new AclGroup();
 
     @Before
     public void setUp() throws Exception {
-        req = null;
-        Mockito.doReturn("mockUserId").when(mockDevice).getUserId();
+        mReq = null;
+        Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(aclGroupHandler);
+        mDeviceServerSystem.addResource(mAclGroupHandler);
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -64,11 +86,11 @@ public class AclGroupTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -83,14 +105,14 @@ public class AclGroupTest {
         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
                 TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("gtype"));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("gmid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
     }
 
     @Test
@@ -104,12 +126,12 @@ public class AclGroupTest {
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
                 ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("midlist"));
-        assertEquals(req.getUriPath(),
+        assertEquals(mReq.getUriPath(),
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
     }
 
@@ -125,12 +147,12 @@ public class AclGroupTest {
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null,
                 ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("dilist"));
-        assertEquals(req.getUriPath(),
+        assertEquals(mReq.getUriPath(),
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
     }
 
@@ -142,11 +164,11 @@ public class AclGroupTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
                 TEST_RESOURCE_GROUP_URI, null, null, null);
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("mid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("mid"));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
     }
 
     @Test
@@ -156,12 +178,12 @@ public class AclGroupTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
                 TEST_RESOURCE_GROUP_URI, "gid=samplegid", null, null);
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("gid"));
-        assertTrue(req.getUriQueryMap().containsKey("gmid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_GROUP_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("gid"));
+        assertTrue(mReq.getUriQueryMap().containsKey("gmid"));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI);
     }
 
     @Test
@@ -172,11 +194,11 @@ public class AclGroupTest {
         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, null,
                 null);
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("midlist"));
-        assertEquals(req.getUriPath(),
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("midlist"));
+        assertEquals(mReq.getUriPath(),
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
     }
 
@@ -188,11 +210,22 @@ public class AclGroupTest {
         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
                 "dilist=sampledi", null, null);
-        aclGroupHandler.onRequestReceived(mockDevice, request);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("dilist"));
-        assertEquals(req.getUriPath(),
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("dilist"));
+        assertEquals(mReq.getUriPath(),
                 TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup");
     }
+
+    @Test(expected = BadRequestException.class)
+    public void testExceptionDeviceRequestReceived() throws Exception {
+        System.out.println(
+                "\t--------------OnRequestReceived(AS) Delete Member Test------------");
+
+        IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
+                TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup",
+                "dilist=sampledi", null, null);
+        mAclGroupHandler.onRequestReceived(mMockDevice, request);
+    }
 }
index 34eb0e2..443eb68 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -10,7 +32,6 @@ import java.util.Arrays;
 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;
@@ -31,28 +52,27 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 public class AclInviteTest {
-    private static final String TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
+    private static final String  TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
 
-    private CoapDevice          mockDevice               = mock(
+    private CoapDevice           mMockDevice              = mock(
             CoapDevice.class);
-    IRequest                    req                      = null;
-    ConnectorPool               connectorPool            = null;
-    DeviceServerSystem          deviceServerSystem       = new DeviceServerSystem();
-    final CountDownLatch        latch                    = new CountDownLatch(
+    private IRequest             mReq                     = null;
+    private DeviceServerSystem   mDeviceServerSystem      = new DeviceServerSystem();
+    private final CountDownLatch mLatch                   = new CountDownLatch(
             1);
 
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel      mRequestChannel;
 
     @InjectMocks
-    AclInvite                   aclInviteHandler         = new AclInvite();
+    private AclInvite            mAclInviteHandler        = new AclInvite();
 
     @Before
     public void setUp() throws Exception {
-        req = null;
-        Mockito.doReturn("mockUserId").when(mockDevice).getUserId();
+        mReq = null;
+        Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(aclInviteHandler);
+        mDeviceServerSystem.addResource(mAclInviteHandler);
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -66,11 +86,11 @@ public class AclInviteTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -88,19 +108,19 @@ public class AclInviteTest {
         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
                 TEST_RESOURCE_INVITE_URI, null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(payloadData));
-        aclInviteHandler.onRequestReceived(mockDevice, request);
+        mAclInviteHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("uid"));
-        assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
+        assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .containsKey("invite"));
         ArrayList<HashMap<String, Object>> getinvite = (ArrayList<HashMap<String, Object>>) cbor
-                .parsePayloadFromCbor(req.getPayload(), HashMap.class)
+                .parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
                 .get("invite");
         assertTrue(getinvite.get(0).containsKey("gid"));
         assertTrue(getinvite.get(0).containsKey("mid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
     }
 
     @Test
@@ -110,11 +130,11 @@ public class AclInviteTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
                 TEST_RESOURCE_INVITE_URI, null, null, null);
-        aclInviteHandler.onRequestReceived(mockDevice, request);
+        mAclInviteHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("uid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("uid"));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
     }
 
     @Test
@@ -124,11 +144,11 @@ public class AclInviteTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
                 TEST_RESOURCE_INVITE_URI, "gid=g0001", null, null);
-        aclInviteHandler.onRequestReceived(mockDevice, request);
+        mAclInviteHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriQueryMap().containsKey("gid"));
-        assertTrue(req.getUriQueryMap().containsKey("uid"));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getUriQueryMap().containsKey("gid"));
+        assertTrue(mReq.getUriQueryMap().containsKey("uid"));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
     }
 }
index 20cf362..a8b1921 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -8,7 +30,6 @@ import static org.mockito.Mockito.mock;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
-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;
@@ -28,23 +49,22 @@ import org.mockito.stubbing.Answer;
 public class AclTest {
     private static final String TEST_RESOURCE_ACI_URI = "/"
             + OICConstants.PREFIX_OIC + "/" + OICConstants.ACL_URI;
-    private CoapDevice          mockDevice            = mock(CoapDevice.class);
-    IRequest                    req                   = null;
-    ConnectorPool               connectorPool         = null;
-    DeviceServerSystem          deviceServerSystem    = new DeviceServerSystem();
-    final CountDownLatch        latch                 = new CountDownLatch(1);
+    private CoapDevice          mMockDevice           = mock(CoapDevice.class);
+    private IRequest            mReq                  = null;
+    private DeviceServerSystem  mDeviceServerSystem   = new DeviceServerSystem();
+    final CountDownLatch        mLatch                = new CountDownLatch(1);
 
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel     mRequestChannel;
 
     @InjectMocks
-    Acl                         aclHandler            = new Acl();
+    private Acl                 mAclHandler           = new Acl();
 
     @Before
     public void setUp() throws Exception {
-        req = null;
+        mReq = null;
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(aclHandler);
+        mDeviceServerSystem.addResource(mAclHandler);
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -58,11 +78,11 @@ public class AclTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -73,9 +93,9 @@ public class AclTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
                 TEST_RESOURCE_ACI_URI, null, null, null);
-        aclHandler.onRequestReceived(mockDevice, request);
+        mAclHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_ACI_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_ACI_URI);
     }
 }
index f0045ff..2fa8bc5 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.account;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -8,7 +30,6 @@ import static org.mockito.Mockito.mock;
 import java.util.concurrent.CountDownLatch;
 
 import org.iotivity.cloud.base.OICConstants;
-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;
@@ -29,24 +50,23 @@ public class CertificateTest {
     private static final String TEST_RESOURCE_CERTI_URI = "/"
             + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/"
             + OICConstants.CERT_URI;
-    private CoapDevice          mockDevice              = mock(
+    private CoapDevice          mMockDevice             = mock(
             CoapDevice.class);
-    IRequest                    req                     = null;
-    ConnectorPool               connectorPool           = null;
-    DeviceServerSystem          deviceServerSystem      = new DeviceServerSystem();
-    final CountDownLatch        latch                   = new CountDownLatch(1);
+    private IRequest            mReq                    = null;
+    private DeviceServerSystem  mDeviceServerSystem     = new DeviceServerSystem();
+    final CountDownLatch        mLatch                  = new CountDownLatch(1);
 
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel     mRequestChannel;
 
     @InjectMocks
-    Certificate                 certHandler             = new Certificate();
+    private Certificate         mCertHandler            = new Certificate();
 
     @Before
     public void setUp() throws Exception {
-        req = null;
+        mReq = null;
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(certHandler);
+        mDeviceServerSystem.addResource(mCertHandler);
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -60,11 +80,11 @@ public class CertificateTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -75,10 +95,10 @@ public class CertificateTest {
 
         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
                 TEST_RESOURCE_CERTI_URI, null, null, null);
-        certHandler.onRequestReceived(mockDevice, request);
+        mCertHandler.onRequestReceived(mMockDevice, request);
 
-        assertTrue(latch.await(1L, SECONDS));
-        assertEquals(req.getUriPath(), TEST_RESOURCE_CERTI_URI);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertEquals(mReq.getUriPath(), TEST_RESOURCE_CERTI_URI);
     }
 
 }
index e70303f..db46b22 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.mq;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -11,7 +33,6 @@ 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;
@@ -30,25 +51,22 @@ import org.mockito.stubbing.Answer;
 
 public class MessageQueueTest {
     private static final String TEST_MQ_BROKER_URI = Constants.MQ_BROKER_FULL_URI;
-    String                      userId             = "testuser";
-    private CoapDevice          mockDevice         = null;
-    IResponse                   res                = null;
-    IRequest                    req                = null;
-    DeviceServerSystem          deviceServerSystem;
-    final CountDownLatch        latch              = new CountDownLatch(1);
+    private CoapDevice           mMockDevice        = null;
+    private IRequest             mReq               = null;
+    private DeviceServerSystem   mDeviceServerSystem;
+    private final CountDownLatch mLatch             = new CountDownLatch(1);
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel      mRequestChannel;
     @InjectMocks
-    MessageQueue                mqHandler          = new MessageQueue();
+    private MessageQueue         mMqHandler         = new MessageQueue();
 
     @Before
     public void setUp() throws Exception {
-        res = null;
-        req = null;
+        mReq = null;
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem = new DeviceServerSystem();
-        deviceServerSystem.addResource(mqHandler);
-        mockDevice = mock(CoapDevice.class);
+        mDeviceServerSystem = new DeviceServerSystem();
+        mDeviceServerSystem.addResource(mMqHandler);
+        mMockDevice = mock(CoapDevice.class);
         // callback requestmsg mock from (IRequestChannel) server
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -56,11 +74,11 @@ public class MessageQueueTest {
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapRequest request = (CoapRequest) args[0];
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -75,11 +93,11 @@ public class MessageQueueTest {
         request = MessageBuilder.createRequest(RequestMethod.PUT,
                 TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR,
                 cbor.encodingPayloadToCbor(tags));
-        deviceServerSystem.onRequestReceived(mockDevice, request);
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
         // assertion : request msg to the AS is identical to the request msg
         // from the client
-        assertTrue(latch.await(1L, SECONDS));
-        assertEquals(request, req);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertEquals(request, mReq);
     }
 
 }
index 9e8f3bc..42943f6 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.rd;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -10,7 +32,6 @@ 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;
@@ -35,27 +56,24 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 public class DevicePresenceTest {
-    public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
-    public static final String DEVICE_LIST_KEY    = "devices";
-    public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
-    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);
+    public static final String DEVICE_PRS_REQ_URI  = Constants.DEVICE_PRESENCE_FULL_URI;
+    public static final String DEVICE_LIST_KEY     = "devices";
+    public static final String RES_PRS_URI         = Constants.RESOURCE_PRESENCE_FULL_URI;
+    private String             mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    private CoapDevice         mMockDevice         = mock(CoapDevice.class);
+    private IRequest           mReq                = null;
+    private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem();
+    final CountDownLatch       mLatch              = new CountDownLatch(1);
     @Mock
-    IRequestChannel            requestChannel;
+    private IRequestChannel    mRequestChannel;
     @InjectMocks
-    DevicePresence             prsHandler         = new DevicePresence();
+    private DevicePresence     mPrsHandler         = new DevicePresence();
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(prsHandler);
-        Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
+        mDeviceServerSystem.addResource(mPrsHandler);
+        Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
@@ -68,11 +86,11 @@ public class DevicePresenceTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return request;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -80,8 +98,8 @@ public class DevicePresenceTest {
     IRequest                             requestSpecificDevice = makePresenceSpecificDevice(
             "device1");
     @InjectMocks
-    DevicePresence.AccountReceiveHandler SpecificDeviceHandler = prsHandler.new AccountReceiveHandler(
-            requestSpecificDevice, mockDevice);
+    DevicePresence.AccountReceiveHandler SpecificDeviceHandler = mPrsHandler.new AccountReceiveHandler(
+            requestSpecificDevice, mMockDevice);
 
     @Test
     public void testSpecificDeviceonResponseReceived() throws ClientException {
@@ -89,17 +107,17 @@ public class DevicePresenceTest {
                 "\t--------------onResponseReceived(RD) Device Presence (specific deivce) Test------------");
         IResponse response = responseFromAccountServer();
         SpecificDeviceHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getObserve() == Observe.SUBSCRIBE);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
         assertTrue(queryMap.get("di").contains("device1"));
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
     }
 
     // @InjectMocks for testEntireDeviceonResponseReceived
     IRequest                             requestEntireDevices = makePresenceEntireDevice();
     @InjectMocks
-    DevicePresence.AccountReceiveHandler EntireDeviceHandler  = prsHandler.new AccountReceiveHandler(
-            requestEntireDevices, mockDevice);
+    DevicePresence.AccountReceiveHandler EntireDeviceHandler  = mPrsHandler.new AccountReceiveHandler(
+            requestEntireDevices, mMockDevice);
 
     @Test
     public void testEntireDeviceonResponseReceived() throws ClientException {
@@ -107,12 +125,12 @@ public class DevicePresenceTest {
                 "\t--------------onResponseReceived(RD) Device Presence (entire deivces) Test------------");
         IResponse response = responseFromAccountServer();
         EntireDeviceHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.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);
+        assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
     }
 
     @Test
@@ -120,21 +138,21 @@ public class DevicePresenceTest {
             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<String, List<String>> queryMap = req.getUriQueryMap();
+        IRequest request = makePresenceSpecificDevice(mDi);
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
         assertTrue(queryMap.containsKey("mid"));
-        assertTrue(req.getMethod().equals(RequestMethod.GET));
-        assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
+        assertTrue(mReq.getMethod().equals(RequestMethod.GET));
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived
     IRequest                             requestDeregisterSpecificDevice = makeDeregisterSpecificDevice(
             "device1");
     @InjectMocks
-    DevicePresence.AccountReceiveHandler DeregisterHandler               = prsHandler.new AccountReceiveHandler(
-            requestDeregisterSpecificDevice, mockDevice);
+    DevicePresence.AccountReceiveHandler DeregisterHandler               = mPrsHandler.new AccountReceiveHandler(
+            requestDeregisterSpecificDevice, mMockDevice);
 
     @Test
     public void testPresenceDeregisterSpecificDeviceOnRequestReceived()
@@ -143,10 +161,10 @@ public class DevicePresenceTest {
                 "\t--------------OnRequestReceived(RD) Device Presence (deregister specific device) Test------------");
         IResponse response = responseFromAccountServer();
         DeregisterHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
         assertTrue(queryMap.get("di").contains("device1"));
-        assertTrue(req.getObserve() == Observe.UNSUBSCRIBE);
+        assertTrue(mReq.getObserve() == Observe.UNSUBSCRIBE);
     }
 
     @Test
@@ -155,12 +173,12 @@ public class DevicePresenceTest {
         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<String, List<String>> queryMap = req.getUriQueryMap();
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReq.getMethod().equals(RequestMethod.GET));
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
         assertTrue(queryMap.containsKey("mid"));
-        assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     public IRequest makePresenceEntireDevice() {
index d2ae9ab..be5b77b 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.rd;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -9,11 +31,9 @@ import java.util.ArrayList;
 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;
@@ -32,81 +52,112 @@ import org.mockito.invocation.InvocationOnMock;
 import org.mockito.stubbing.Answer;
 
 public class ResourceDirectoryTest {
-    private static final String TEST_RD_URI        = Constants.RD_FULL_URI;
-    public static final String  DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
-    public static final String  DEVICE_LIST_KEY    = "devices";
-    public static final String  RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
+    private static final String TEST_RD_URI         = Constants.RD_FULL_URI;
+    public static final String  DEVICE_PRS_REQ_URI  = Constants.DEVICE_PRESENCE_FULL_URI;
+    public static final String  DEVICE_LIST_KEY     = "devices";
+    public static final String  RES_PRS_URI         = Constants.RESOURCE_PRESENCE_FULL_URI;
     // 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;
+    private String              mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    String                      mUserId             = "testuser";
+    private CoapDevice          mMockDevice         = null;
+    IRequest                    mReqRDServer        = null;
+    IRequest                    mReqASServer        = null;
+    DeviceServerSystem          mDeviceServerSystem = new DeviceServerSystem();
+    final CountDownLatch        mLatch              = new CountDownLatch(1);
+    @Mock(name = "mRDServer")
+    IRequestChannel             mRequestChannelRDServer;
+    @Mock(name = "mASServer")
+    IRequestChannel             mRequestChannelASServer;
 
     @InjectMocks
-    ResourceDirectory           rdHandler          = new ResourceDirectory();
+    ResourceDirectory           mRdHandler          = new ResourceDirectory();
 
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(rdHandler);
-        mockDevice = mock(CoapDevice.class);
-        // callback requestmsg mock from (IRequestChannel) server
+        mDeviceServerSystem.addResource(mRdHandler);
+        mMockDevice = mock(CoapDevice.class);
+        Mockito.doReturn(mUserId).when(mMockDevice).getUserId();
+
         Mockito.doAnswer(new Answer<Object>() {
             @Override
             public CoapRequest answer(InvocationOnMock invocation)
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapRequest request = (CoapRequest) args[0];
+                System.out.println("\t----------mRDServer --------------");
                 System.out.println(
                         "\t----------payload : " + request.getPayloadString());
+                System.out.println("\t----------uripath : "
+                        + request.getMethod() + " " + request.getUriPath());
+                System.out.println(
+                        "\t---------uriquery : " + request.getUriQuery());
+                mReqRDServer = request;
+                mLatch.countDown();
+                return request;
+            }
+        }).when(mRequestChannelRDServer).sendRequest(
+                Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
+
+        Mockito.doAnswer(new Answer<Object>() {
+            @Override
+            public CoapRequest answer(InvocationOnMock invocation)
+                    throws Throwable {
+                Object[] args = invocation.getArguments();
+                CoapRequest request = (CoapRequest) args[0];
+                System.out.println("\t----------mASServer --------------");
                 System.out.println(
-                        "\t----------uripath : " + request.getUriPath());
+                        "\t----------payload : " + request.getPayloadString());
+                System.out.println("\t----------uripath : "
+                        + request.getMethod() + " " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReqASServer = request;
+                mLatch.countDown();
                 return request;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
-                Mockito.any(CoapDevice.class));
+        }).when(mRequestChannelASServer).sendRequest(
+                Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
 
     }
 
     @Test
-    public void testRDResourceOnRequestReceived() throws Exception {
+    public void testRDResourcePublishOnRequestReceived() throws Exception {
         IRequest request = makeResourcePublishRequest();
-        deviceServerSystem.onRequestReceived(mockDevice, request);
-        assertTrue(latch.await(1L, SECONDS));
-        assertTrue(req.getUriPath().contains(Constants.GROUP_FULL_URI + "/"));
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReqASServer.getMethod().equals(RequestMethod.POST));
+        assertTrue(mReqASServer.getUriPath()
+                .contains(Constants.GROUP_FULL_URI + "/"));
+    }
+
+    @Test
+    public void testRDResourceDeleteOnRequestReceived() throws Exception {
+        IRequest request = makeResourceDeleteRequest();
+        mDeviceServerSystem.onRequestReceived(mMockDevice, request);
+        assertTrue(mLatch.await(1L, SECONDS));
+        assertTrue(mReqRDServer.getMethod().equals(RequestMethod.DELETE));
+        assertTrue(mReqRDServer.getUriPath().contains(TEST_RD_URI));
     }
 
     IRequest                                rdPublishRequest      = makeResourcePublishRequest();
 
     @InjectMocks
-    ResourceDirectory.AccountReceiveHandler accountReceiveHandler = rdHandler.new AccountReceiveHandler(
-            rdPublishRequest, mockDevice);
+    ResourceDirectory.AccountReceiveHandler accountReceiveHandler = mRdHandler.new AccountReceiveHandler(
+            rdPublishRequest, mMockDevice);
 
     @Test
-    public void testRDResourceOnResponseReceived() throws Exception {
+    public void testRDResourcePublishOnResponseReceived() 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));
+        assertEquals(mReqRDServer, rdPublishRequest);
+        assertTrue(mLatch.await(1L, SECONDS));
     }
 
     private IRequest makeResourcePublishRequest() {
         HashMap<Object, Object> payload = new HashMap<>();
-        payload.put(Constants.DEVICE_ID, di);
+        payload.put(Constants.DEVICE_ID, mDi);
         ArrayList<HashMap<Object, Object>> publishLinks = new ArrayList<>();
         HashMap<Object, Object> link = new HashMap<>();
         link.put("href", "/a/light");
@@ -129,4 +180,11 @@ public class ResourceDirectoryTest {
         return request;
     }
 
+    private IRequest makeResourceDeleteRequest() {
+        IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
+                TEST_RD_URI, "di" + "=" + mDi + ";" + "ins" + "=" + "1234",
+                null, null);
+        return request;
+    }
+
 }
index acdf9a4..708edd2 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.rd;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -11,7 +33,6 @@ 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;
@@ -39,25 +60,24 @@ public class ResourceFindTest {
     private static final String TEST_RESOURCE_FIND_URI = Constants.WELL_KNOWN_FULL_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);
+    private IResponse           mRes                   = null;
+    private IRequest            mReq                   = null;
+    private DeviceServerSystem  mDeviceServerSystem    = new DeviceServerSystem();
+    final CountDownLatch        mLatch                 = new CountDownLatch(1);
 
     @Mock
-    IRequestChannel             requestChannel;
+    private IRequestChannel     mRequestChannel;
 
     @InjectMocks
-    ResourceFind                resHandler             = new ResourceFind();
+    private ResourceFind        mResHandler            = new ResourceFind();
 
     @Before
     public void setUp() throws Exception {
-        res = null;
-        req = null;
+        mRes = null;
+        mReq = null;
         Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
         MockitoAnnotations.initMocks(this);
-        deviceServerSystem.addResource(resHandler);
+        mDeviceServerSystem.addResource(mResHandler);
         // callback mock
         Mockito.doAnswer(new Answer<Object>() {
             @Override
@@ -65,7 +85,7 @@ public class ResourceFindTest {
                     throws Throwable {
                 Object[] args = invocation.getArguments();
                 CoapResponse resp = (CoapResponse) args[0];
-                res = resp;
+                mRes = resp;
                 return resp;
             }
         }).when(mockDevice).sendResponse(Mockito.anyObject());
@@ -81,11 +101,11 @@ public class ResourceFindTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
-                latch.countDown();
+                mReq = request;
+                mLatch.countDown();
                 return null;
             }
-        }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
+        }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
                 Mockito.any(CoapDevice.class));
     }
 
@@ -94,7 +114,7 @@ public class ResourceFindTest {
             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
                     "rt=core.light;di=" + "device1");
     @InjectMocks
-    ResourceFind.AccountReceiveHandler specificDeviceHandler = resHandler.new AccountReceiveHandler(
+    ResourceFind.AccountReceiveHandler specificDeviceHandler = mResHandler.new AccountReceiveHandler(
             requestSpecificDevice, mockDevice);
 
     @Test
@@ -104,8 +124,8 @@ public class ResourceFindTest {
                 "\t--------------onResponseReceived(RD) Resource Find (specific deivce) Test------------");
         IResponse response = responseFromAccountServer();
         specificDeviceHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
         assertTrue(queryMap.get("rt").contains("core.light"));
         assertTrue(queryMap.get("di").contains("device1"));
         assertFalse(queryMap.get("di").contains("device2"));
@@ -117,7 +137,7 @@ public class ResourceFindTest {
             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
                     "rt=core.light");
     @InjectMocks
-    ResourceFind.AccountReceiveHandler handler              = resHandler.new AccountReceiveHandler(
+    ResourceFind.AccountReceiveHandler handler              = mResHandler.new AccountReceiveHandler(
             requestEntireDevices, mockDevice);
 
     @Test
@@ -126,8 +146,8 @@ public class ResourceFindTest {
                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------");
         IResponse response = responseFromAccountServer();
         handler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
         assertTrue(queryMap.get("rt").contains("core.light"));
         assertTrue(queryMap.get("di").contains("device1"));
         assertTrue(queryMap.get("di").contains("device2"));
@@ -138,7 +158,7 @@ public class ResourceFindTest {
     IRequest                           requestEntireDevicesNoQuery = MessageBuilder
             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null);
     @InjectMocks
-    ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = resHandler.new AccountReceiveHandler(
+    ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = mResHandler.new AccountReceiveHandler(
             requestEntireDevicesNoQuery, mockDevice);
 
     @Test
@@ -148,8 +168,8 @@ public class ResourceFindTest {
                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces, No Query) Test------------");
         IResponse response = responseFromAccountServer();
         entireDevicesNoQueryHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
         assertTrue(queryMap.get("di").contains("device1"));
         assertTrue(queryMap.get("di").contains("device2"));
         assertTrue(queryMap.get("di").contains("device3"));
@@ -162,11 +182,11 @@ public class ResourceFindTest {
                 "\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<String, List<String>> queryMap = req.getUriQueryMap();
-        assertTrue(latch.await(1L, SECONDS));
+        mResHandler.onRequestReceived(mockDevice, request);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
+        assertTrue(mLatch.await(1L, SECONDS));
         assertTrue(queryMap.containsKey("mid"));
-        assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     @Test
@@ -176,13 +196,13 @@ public class ResourceFindTest {
                 "\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<String, List<String>> queryMap = req.getUriQueryMap();
+        mResHandler.onRequestReceived(mockDevice, request);
+        HashMap<String, List<String>> queryMap = mReq.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(mLatch.await(1L, SECONDS));
         assertTrue(queryMap.containsKey("mid"));
-        assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     private IResponse responseFromAccountServer() {
index 9072d42..c28a978 100644 (file)
@@ -1,3 +1,25 @@
+/*
+ * //******************************************************************
+ * //
+ * // Copyright 2016 Samsung Electronics All Rights Reserved.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ * //
+ * // Licensed under the Apache License, Version 2.0 (the "License");
+ * // you may not use this file except in compliance with the License.
+ * // You may obtain a copy of the License at
+ * //
+ * //      http://www.apache.org/licenses/LICENSE-2.0
+ * //
+ * // Unless required by applicable law or agreed to in writing, software
+ * // distributed under the License is distributed on an "AS IS" BASIS,
+ * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * // See the License for the specific language governing permissions and
+ * // limitations under the License.
+ * //
+ * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ */
+
 package org.iotivity.cloud.ciserver.resources.proxy.rd;
 
 import static java.util.concurrent.TimeUnit.SECONDS;
@@ -36,22 +58,22 @@ public class ResourcePresenceTest {
     public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
     public static final String DEVICE_LIST_KEY    = "devices";
     public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
-    private String             di                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
-    private CoapDevice         mockDevice         = mock(CoapDevice.class);
-    IResponse                  res                = null;
-    IRequest                   req                = null;
-    DeviceServerSystem         deviceServerSystem = new DeviceServerSystem();
+    private String             mDi                = "B371C481-38E6-4D47-8320-7688D8A5B58C";
+    private CoapDevice         mMockDevice        = mock(CoapDevice.class);
+    private IResponse          mRes               = null;
+    private IRequest           mReq               = null;
+    private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
     final CountDownLatch       latch              = new CountDownLatch(1);
     @Mock
-    IRequestChannel            requestChannel;
+    private IRequestChannel    requestChannel;
     @InjectMocks
-    ResourcePresence           adHandler          = new ResourcePresence();
+    private ResourcePresence   adHandler          = new ResourcePresence();
 
     @Before
     public void setUp() throws Exception {
-        res = null;
-        req = null;
-        Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
+        mRes = null;
+        mReq = null;
+        Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
         MockitoAnnotations.initMocks(this);
         deviceServerSystem.addResource(adHandler);
         Mockito.doAnswer(new Answer<Object>() {
@@ -66,7 +88,7 @@ public class ResourcePresenceTest {
                         "\t----------uripath : " + request.getUriPath());
                 System.out.println(
                         "\t---------uriquery : " + request.getUriQuery());
-                req = request;
+                mReq = request;
                 latch.countDown();
                 return request;
             }
@@ -81,13 +103,13 @@ public class ResourcePresenceTest {
                 "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------");
         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
                 RES_PRS_URI, null);
-        deviceServerSystem.onRequestReceived(mockDevice, request);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
+        deviceServerSystem.onRequestReceived(mMockDevice, request);
+        HashMap<String, List<String>> queryMap = mReq.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(), Constants.GROUP_FULL_URI + "/null");
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     @Test
@@ -96,19 +118,19 @@ public class ResourcePresenceTest {
         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<String, List<String>> queryMap = req.getUriQueryMap();
+                RES_PRS_URI, "di=" + mDi);
+        deviceServerSystem.onRequestReceived(mMockDevice, request);
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
         assertTrue(latch.await(1L, SECONDS));
         assertTrue(queryMap.containsKey("mid"));
-        assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
+        assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
     }
 
     IRequest                               requestEntireDevices = MessageBuilder
             .createRequest(RequestMethod.GET, RES_PRS_URI, null);
     @InjectMocks
     ResourcePresence.AccountReceiveHandler entireDeviceHandler  = adHandler.new AccountReceiveHandler(
-            requestEntireDevices, mockDevice);
+            requestEntireDevices, mMockDevice);
 
     @Test
     public void testEntireDeviceonResponseReceived() throws ClientException {
@@ -116,10 +138,10 @@ public class ResourcePresenceTest {
                 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
         IResponse responseFromAccountServer = responseFromAccountServer();
         entireDeviceHandler.onResponseReceived(responseFromAccountServer);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
         // assertion : if query has pre-requested multiple di list given from
         // the AS
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
         assertTrue(queryMap.get("di").contains("device1"));
         assertTrue(queryMap.get("di").contains("device2"));
         assertTrue(queryMap.get("di").contains("device3"));
@@ -129,7 +151,7 @@ public class ResourcePresenceTest {
             .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
     @InjectMocks
     ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
-            requestSpecificDevice, mockDevice);
+            requestSpecificDevice, mMockDevice);
 
     @Test
     public void testSpecificDeviceonResponseReceived() throws ClientException {
@@ -137,10 +159,10 @@ public class ResourcePresenceTest {
                 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
         IResponse response = responseFromAccountServer();
         specificDeviceHandler.onResponseReceived(response);
-        HashMap<String, List<String>> queryMap = req.getUriQueryMap();
+        HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
         // assertion : if query has pre-requested di
         assertTrue(queryMap.get("di").contains("device1"));
-        assertTrue(req.getMethod() == RequestMethod.GET);
+        assertTrue(mReq.getMethod() == RequestMethod.GET);
     }
 
     private IResponse responseFromAccountServer() {