From f7ee4ce78cd91cca3e7936273bfdb7caa204f450 Mon Sep 17 00:00:00 2001 From: Jung Seungho Date: Tue, 15 Nov 2016 16:26:51 +0900 Subject: [PATCH] [IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountServer - appended uid in AclInvite, Account and AccoutSession in CI - apppeded uid, members in AclGroup, ResourceFind, ResourcePresence and DevicePresence - added logic for verifying accesstoken in AccountManger in AS - fixed testcase Change-Id: I468c13127452edaee7301b9cafd0ab9f28bbf65d Signed-off-by: Jung Seungho Reviewed-on: https://gerrit.iotivity.org/gerrit/14341 Reviewed-by: Yeonghun Nam Tested-by: jenkins-iotivity Reviewed-by: Jee Hyeok Kim (cherry picked from commit 186bc52a19653b48a154d8dfb1815e1cabce2efc) Reviewed-on: https://gerrit.iotivity.org/gerrit/14583 Reviewed-by: Uze Choi Tested-by: Uze Choi Reviewed-on: https://gerrit.iotivity.org/gerrit/16079 --- .../resources/account/AccountManager.java | 29 ++++-- .../resources/account/AccountResource.java | 11 ++- .../resources/account/session/SessionResource.java | 46 +++++----- .../resources/acl/invite/InviteManager.java | 4 + .../resources/acl/invite/InviteResource.java | 1 + .../resources/account/AccountResourceTest.java | 3 +- .../account/session/SessionResourceTest.java | 6 +- .../org/iotivity/cloud/ciserver/Constants.java | 7 +- .../cloud/ciserver/DeviceServerSystem.java | 42 ++++++++- .../ciserver/resources/proxy/account/Account.java | 35 ++++++- .../resources/proxy/account/AccountSession.java | 11 ++- .../ciserver/resources/proxy/account/AclGroup.java | 9 +- .../resources/proxy/account/AclInvite.java | 35 +++---- .../resources/proxy/rd/DevicePresence.java | 30 +++--- .../resources/proxy/rd/ResourceDirectory.java | 10 +- .../ciserver/resources/proxy/rd/ResourceFind.java | 50 +++++----- .../resources/proxy/rd/ResourcePresence.java | 23 +++-- .../ciserver/resources/KeepAliveResourceTest.java | 12 --- .../proxy/account/AccountSessionTest.java | 20 +++- .../resources/proxy/account/AclGroupTest.java | 102 ++++++++++++--------- .../resources/proxy/account/AclInviteTest.java | 29 +++++- .../ciserver/resources/proxy/account/CrlTest.java | 83 +++++++++++++++++ .../resources/proxy/rd/DevicePresenceTest.java | 19 ++-- .../resources/proxy/rd/ResourceFindTest.java | 31 ++++--- .../resources/proxy/rd/ResourcePresenceTest.java | 18 ++-- 25 files changed, 455 insertions(+), 211 deletions(-) create mode 100644 cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CrlTest.java diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java index 296669a..eba4b26 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountManager.java @@ -135,19 +135,16 @@ public class AccountManager { // find token information corresponding to the uuid and did HashMap condition = new HashMap<>(); condition.put(Constants.KEYFIELD_UUID, uuid); + condition.put(Constants.KEYFIELD_DID, did); - ArrayList> recordList = findRecord( - AccountDBManager.getInstance() - .selectRecord(Constants.TOKEN_TABLE, condition), - Constants.KEYFIELD_DID, did); + ArrayList> recordList = AccountDBManager + .getInstance().selectRecord(Constants.TOKEN_TABLE, condition); if (recordList.isEmpty()) { throw new UnAuthorizedException("access token doesn't exist"); } - HashMap record = recordList.get(0); - - TokenTable tokenInfo = castMapToTokenTable(record); + TokenTable tokenInfo = castMapToTokenTable(recordList.get(0)); // token verification to check if the accesstoken is expired if (verifyToken(tokenInfo, accessToken)) { @@ -558,7 +555,7 @@ public class AccountManager { return searchType; } - public void deleteDevice(String uid, String di) { + public boolean deleteDevice(String uid, String di, String accetoken) { HashSet diSet = new HashSet(); diSet.add(di); @@ -568,11 +565,25 @@ public class AccountManager { condition.put(Constants.KEYFIELD_UUID, uid); condition.put(Constants.KEYFIELD_DID, di); + ArrayList> recordList = AccountDBManager + .getInstance().selectRecord(Constants.TOKEN_TABLE, condition); + + if (recordList.isEmpty()) { + throw new UnAuthorizedException("access token doesn't exist"); + } + + TokenTable tokenInfo = castMapToTokenTable(recordList.get(0)); + + if (!verifyToken(tokenInfo, accetoken)) { + return false; + } + // delete Token information from the DB AccountDBManager.getInstance().deleteRecord(Constants.TOKEN_TABLE, condition); // delete device ID from all groups in the DB GroupManager.getInstance().deleteDevicesFromAllGroup(di); - } + return true; + } } diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java index 30b0a48..4e7cfb7 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/AccountResource.java @@ -26,10 +26,10 @@ import java.util.HashMap; import java.util.List; import org.iotivity.cloud.accountserver.Constants; -import org.iotivity.cloud.accountserver.resources.acl.id.AclManager; import org.iotivity.cloud.base.device.Device; import org.iotivity.cloud.base.exception.ServerException; import org.iotivity.cloud.base.exception.ServerException.BadRequestException; +import org.iotivity.cloud.base.exception.ServerException.UnAuthorizedException; import org.iotivity.cloud.base.protocols.IRequest; import org.iotivity.cloud.base.protocols.IResponse; import org.iotivity.cloud.base.protocols.MessageBuilder; @@ -153,13 +153,16 @@ public class AccountResource extends Resource { HashMap> queryMap = request.getUriQueryMap(); - if (checkQueryException( - Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_DEVICE_ID), + if (checkQueryException(Arrays.asList(Constants.REQ_UUID_ID, + Constants.REQ_DEVICE_ID, Constants.REQ_ACCESS_TOKEN), queryMap)) { String uid = queryMap.get(Constants.REQ_UUID_ID).get(0); String did = queryMap.get(Constants.REQ_DEVICE_ID).get(0); - mAsManager.deleteDevice(uid, did); + String accesstoken = queryMap.get(Constants.REQ_ACCESS_TOKEN) + .get(0); + if (!mAsManager.deleteDevice(uid, did, accesstoken)) + throw new UnAuthorizedException("accesstoken is not valid"); } return MessageBuilder.createResponse(request, ResponseStatus.DELETED); diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java index f05c900..f2d6763 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResource.java @@ -86,31 +86,31 @@ public class SessionResource extends Resource { HashMap responsePayload = null; - if (checkPayloadException( - Arrays.asList(Constants.REQ_UUID_ID, Constants.REQ_DEVICE_ID, - Constants.REQ_ACCESS_TOKEN, Constants.REQ_LOGIN), - payloadData)) { + checkPayloadException(Arrays.asList(Constants.REQ_DEVICE_ID, + Constants.REQ_ACCESS_TOKEN), payloadData); + String deviceId = payloadData.get(Constants.REQ_DEVICE_ID).toString(); + String accessToken = payloadData.get(Constants.REQ_ACCESS_TOKEN) + .toString(); + + checkPayloadException(Constants.REQ_LOGIN, payloadData); + + boolean signinRequest = (boolean) payloadData.get(Constants.REQ_LOGIN); + + if (signinRequest) { + // sign-in response payload + checkPayloadException(Constants.REQ_UUID_ID, payloadData); String uuid = payloadData.get(Constants.REQ_UUID_ID).toString(); - String deviceId = payloadData.get(Constants.REQ_DEVICE_ID) - .toString(); - String accessToken = payloadData.get(Constants.REQ_ACCESS_TOKEN) - .toString(); - - // identify if the request is sign-in or sign-out - boolean signinRequest = (boolean) payloadData - .get(Constants.REQ_LOGIN); - - if (signinRequest) { - // sign-in response payload - responsePayload = mAsManager.signInOut(uuid, deviceId, - accessToken); - } else { - // sign-out response - mAsManager.signInOut(uuid, deviceId, accessToken); - return MessageBuilder.createResponse(request, - ResponseStatus.CHANGED); - } + responsePayload = mAsManager.signInOut(uuid, deviceId, accessToken); + } else { + // sign-out response + checkQueryException(Constants.REQ_UUID_ID, + request.getUriQueryMap()); + String uuid = request.getUriQueryMap().get(Constants.REQ_UUID_ID) + .get(0); + mAsManager.signInOut(uuid, deviceId, accessToken); + return MessageBuilder.createResponse(request, + ResponseStatus.CHANGED); } // sign-in response diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteManager.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteManager.java index 529eef3..afcc6aa 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteManager.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteManager.java @@ -117,6 +117,10 @@ public class InviteManager { uidList.add(getInviteTable.getInviteUser()); } + if (getInviteList == null || getInviteList.isEmpty()) { + throw new BadRequestException("mid or gid is not valid"); + } + AccountDBManager.getInstance().deleteRecord(Constants.INVITE_TABLE, condition); diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResource.java index d018924..99f953e 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResource.java @@ -169,6 +169,7 @@ public class InviteResource extends Resource { String acceptStr = queryParams.get(Constants.REQ_INVITE_ACCEPT) .get(0); + boolean accepted = false; if (acceptStr.equals(Constants.INVITE_ACCEPT)) { accepted = true; diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/AccountResourceTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/AccountResourceTest.java index 9f62d55..a01bf24 100644 --- a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/AccountResourceTest.java +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/AccountResourceTest.java @@ -325,7 +325,8 @@ public class AccountResourceTest { private IRequest deleteDeviceRequest(String deviceId, String uid) { IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - REGISTER_URI, "di=" + deviceId + ";uid=" + uid); + REGISTER_URI, + "accesstoken=at0001" + ";di=" + deviceId + ";uid=" + uid); return request; } diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResourceTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResourceTest.java index a699048..5224f76 100644 --- a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResourceTest.java +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/account/session/SessionResourceTest.java @@ -313,9 +313,11 @@ public class SessionResourceTest { payloadData.put("accesstoken", accessToken); payloadData.put("login", false); payloadData.put("di", deviceId); - payloadData.put("uid", USER_UUID); + StringBuffer query = new StringBuffer(); + query.append("uid=" + USER_UUID); request = MessageBuilder.createRequest(RequestMethod.POST, - DEFAULT_AUTH_LOGOUT, null, ContentFormat.APPLICATION_CBOR, + DEFAULT_AUTH_LOGOUT, query.toString(), + ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(payloadData)); return request; } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java index a502fc4..af91bcc 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/Constants.java @@ -41,9 +41,9 @@ public class Constants extends OICConstants { 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_MEMBER_LIST = "members"; public static final String REQ_DEVICE_ID = "di"; - public static final String REQ_DEVICE_LIST = "dilist"; + public static final String REQ_DEVICE_LIST = "devices"; public static final String REQ_INVITE = "invite"; public static final String REQ_PING_ARRAY = "inarray"; public static final String REQ_PING = "in"; @@ -60,7 +60,4 @@ public class Constants extends OICConstants { public static final String REQ_LINKS = "links"; public static final String REQ_HREF = "href"; public static final String REQ_CRL = "crl"; - - public static final String KEYFIELD_GROUPS = "groups"; - public static final String KEYFIELD_DEVICES = "devices"; } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java index 1fc959c..8aa5823 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/DeviceServerSystem.java @@ -60,7 +60,9 @@ import io.netty.channel.ChannelPromise; public class DeviceServerSystem extends ServerSystem { - IRequestChannel mRDServer = null; + private Cbor> mCbor = new Cbor>(); + + IRequestChannel mRDServer = null; public DeviceServerSystem() { mRDServer = ConnectorPool.getConnection("rd"); @@ -185,11 +187,44 @@ public class DeviceServerSystem extends ServerSystem { ctx.close(); } } - ctx.fireChannelRead(msg); } @Override + public void write(ChannelHandlerContext ctx, Object msg, + ChannelPromise promise) throws Exception { + + if (!(msg instanceof CoapResponse)) { + throw new BadRequestException( + "this msg type is not CoapResponse"); + } + // This is CoapResponse + // Once the response is valid, add this to deviceList + CoapResponse response = (CoapResponse) msg; + + switch (response.getUriPath()) { + case OICConstants.ACCOUNT_SESSION_FULL_URI: + if (response.getStatus() != ResponseStatus.CHANGED) { + throw new UnAuthorizedException(); + } + + if (response.getPayload() != null) { + break; + } + + ctx.close(); + break; + case OICConstants.ACCOUNT_FULL_URI: + if (response.getStatus() != ResponseStatus.DELETED) { + break; + } + ctx.close(); + break; + } + ctx.writeAndFlush(msg); + } + + @Override public void channelActive(ChannelHandlerContext ctx) { Device device = ctx.channel().attr(keyDevice).get(); // Authenticated device connected @@ -246,7 +281,6 @@ public class DeviceServerSystem extends ServerSystem { @Sharable class CoapAuthHandler extends ChannelDuplexHandler { - private Cbor> mCbor = new Cbor>(); @Override public void channelActive(ChannelHandlerContext ctx) { @@ -288,7 +322,7 @@ public class DeviceServerSystem extends ServerSystem { ((CoapDevice) device).setExpiredPolicy(remainTime); // Remove current auth handler and replace to - // LifeCycleHandler + // LifeCycleHandle ctx.channel().pipeline().replace(this, "LifeCycleHandler", mLifeCycleHandler); diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java index 4e41179..a4e08ca 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/Account.java @@ -52,6 +52,33 @@ public class Account extends Resource { mASServer = ConnectorPool.getConnection("account"); } + class RDReceiveHandler implements IResponseEventHandler { + + private Device mSrcDevice; + private IResponse mResponse; + private IRequest mRequest; + + public RDReceiveHandler(IRequest request, IResponse response, + Device srcDevice) { + mSrcDevice = srcDevice; + mRequest = request; + mResponse = response; + } + + @Override + public void onResponseReceived(IResponse response) + throws ClientException { + switch (response.getStatus()) { + case DELETED: + mSrcDevice.sendResponse(mResponse); + break; + default: + mSrcDevice.sendResponse(MessageBuilder.createResponse( + mRequest, ResponseStatus.BAD_REQUEST)); + } + } + } + class AccountReceiveHandler implements IResponseEventHandler { IRequestChannel mRDServer = null; @@ -75,7 +102,8 @@ public class Account extends Resource { mRDServer.sendRequest( MessageBuilder.createRequest(RequestMethod.DELETE, uriPath.toString(), mRequest.getUriQuery()), - mSrcDevice); + new RDReceiveHandler(mRequest, response, + mSrcDevice)); break; case CHANGED: case CONTENT: @@ -95,8 +123,9 @@ public class Account extends Resource { StringBuffer additionalQuery = new StringBuffer(); additionalQuery .append(Constants.USER_ID + "=" + srcDevice.getUserId()); - String uriQuery = request.getUriQuery() + ";" - + additionalQuery.toString(); + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null + ? (";" + request.getUriQuery()) : ""); request = MessageBuilder.modifyRequest(request, null, uriQuery, null, null); } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java index 7f3bf84..595164a 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSession.java @@ -61,10 +61,13 @@ public class AccountSession extends Resource { 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()); - payloadData.put(Constants.ACCESS_TOKEN, srcDevice.getAccessToken()); - request = MessageBuilder.modifyRequest(request, null, null, + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery + .append(Constants.USER_ID + "=" + srcDevice.getUserId()); + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null + ? (";" + request.getUriQuery()) : ""); + request = MessageBuilder.modifyRequest(request, null, uriQuery, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(payloadData)); } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java index d106e96..429e537 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroup.java @@ -30,6 +30,7 @@ import org.iotivity.cloud.base.device.IRequestChannel; 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.MessageBuilder; import org.iotivity.cloud.base.resource.Resource; import org.iotivity.cloud.ciserver.Constants; import org.iotivity.cloud.util.Cbor; @@ -73,6 +74,12 @@ public class AclGroup extends Resource { throw new BadRequestException( request.getMethod() + " request type is not support"); } - mAuthServer.sendRequest(request, srcDevice); + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId()); + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null ? (";" + request.getUriQuery()) + : ""); + mAuthServer.sendRequest(MessageBuilder.modifyRequest(request, null, + uriQuery, null, null), srcDevice); } } \ No newline at end of file diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java index ff05fab..86e53f1 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInvite.java @@ -28,9 +28,9 @@ 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.exception.ServerException; +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.enums.ContentFormat; import org.iotivity.cloud.base.resource.Resource; import org.iotivity.cloud.ciserver.Constants; import org.iotivity.cloud.util.Cbor; @@ -66,28 +66,23 @@ public class AclInvite extends Resource { request.getPayload(), HashMap.class); checkPayloadException(Constants.REQ_INVITE, payloadData); - - payloadData.put(Constants.USER_ID, srcDevice.getUserId()); - - request = MessageBuilder.modifyRequest(request, null, null, - ContentFormat.APPLICATION_CBOR, - cbor.encodingPayloadToCbor(payloadData)); break; - - default: - - StringBuffer additionalQuery = new StringBuffer(); - additionalQuery.append( - Constants.USER_ID + "=" + srcDevice.getUserId()); - - String uriQuery = additionalQuery.toString() - + (request.getUriQuery() != null - ? (";" + request.getUriQuery()) : ""); - request = MessageBuilder.modifyRequest(request, null, uriQuery, - null, null); - + case GET: break; + case DELETE: + break; + default: + throw new BadRequestException( + request.getMethod() + " request type is not support"); } + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId()); + + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null ? (";" + request.getUriQuery()) + : ""); + request = MessageBuilder.modifyRequest(request, null, uriQuery, null, + null); mAuthServer.sendRequest(request, srcDevice); } diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java index 864c755..fe86968 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresence.java @@ -21,6 +21,7 @@ */ package org.iotivity.cloud.ciserver.resources.proxy.rd; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @@ -85,12 +86,14 @@ public class DevicePresence extends Resource { return; } + ArrayList devices = (ArrayList) getResponseDeviceList( + payloadData); + if (mRequest.getUriQuery() != null && mRequest.getUriQueryMap() .containsKey(Constants.REQ_DEVICE_ID)) { - if (!getResponseDeviceList(payloadData) - .containsAll(mRequest.getUriQueryMap() - .get(Constants.REQ_DEVICE_ID))) { + if (!devices.containsAll(mRequest.getUriQueryMap() + .get(Constants.REQ_DEVICE_ID))) { mSrcDevice.sendResponse( MessageBuilder.createResponse(mRequest, ResponseStatus.BAD_REQUEST)); @@ -104,12 +107,11 @@ public class DevicePresence extends Resource { ResponseStatus.BAD_REQUEST)); return; } + String uriQuery = additionalQuery.toString() + + (mRequest.getUriQuery() != null + ? (";" + mRequest.getUriQuery()) : ""); mRequest = MessageBuilder.modifyRequest(mRequest, null, - additionalQuery - + (mRequest.getUriQuery() != null - ? ";" + mRequest.getUriQuery() - : ""), - null, null); + uriQuery, null, null); } mRDServer.sendRequest(mRequest, mSrcDevice); @@ -154,8 +156,14 @@ public class DevicePresence extends Resource { @Override public void onDefaultRequestReceived(Device srcDevice, IRequest request) throws ServerException { - StringBuffer uriQuery = new StringBuffer(); - uriQuery.append(Constants.REQ_MEMBER_ID + "=" + srcDevice.getUserId()); + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId()); + additionalQuery.append(";"); + additionalQuery.append( + Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId()); + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null ? (";" + request.getUriQuery()) + : ""); StringBuffer uriPath = new StringBuffer(); uriPath.append(Constants.PREFIX_OIC + "/"); @@ -164,7 +172,7 @@ public class DevicePresence extends Resource { uriPath.append(srcDevice.getUserId()); IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET, - uriPath.toString(), uriQuery.toString()); + uriPath.toString(), uriQuery); mASServer.sendRequest(requestToAS, new AccountReceiveHandler(request, srcDevice)); diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java index addbbe8..1be0ebf 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceDirectory.java @@ -68,6 +68,9 @@ public class ResourceDirectory extends Resource { HashMap payloadData = mCbor .parsePayloadFromCbor(request.getPayload(), HashMap.class); + StringBuffer query = new StringBuffer(); + query.append("op=add;"); + query.append(Constants.USER_ID + "=" + srcDevice.getUserId()); StringBuffer uriPath = new StringBuffer(); uriPath.append(Constants.PREFIX_OIC + "/"); @@ -76,14 +79,13 @@ public class ResourceDirectory extends Resource { uriPath.append(srcDevice.getUserId()); String di = payloadData.get(Constants.REQ_DEVICE_ID).toString(); - HashMap requestPayload = new HashMap<>(); - requestPayload.put(Constants.USER_ID, srcDevice.getUserId()); + requestPayload.put(Constants.REQ_GROUP_DEVICES, Arrays.asList(di)); IRequest requestToAS = MessageBuilder.createRequest( - RequestMethod.POST, uriPath.toString(), "op=add", - ContentFormat.APPLICATION_CBOR, + RequestMethod.POST, uriPath.toString(), + query.toString(), ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(requestPayload)); mASServer.sendRequest(requestToAS, diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java index 656fafc..55da35e 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFind.java @@ -86,12 +86,8 @@ public class ResourceFind extends Resource { return; } - ArrayList devices = new ArrayList<>(); - - devices = (ArrayList) payloadData - .get(Constants.KEYFIELD_DEVICES); - - System.out.println("devices : " + devices); + ArrayList devices = (ArrayList) getResponseDeviceList( + payloadData); if (mRequest.getUriQuery() != null && mRequest.getUriQueryMap() @@ -103,15 +99,15 @@ public class ResourceFind extends Resource { ResponseStatus.BAD_REQUEST)); } } else { - String additionalQuery = makeAdditionalQuery(devices); + StringBuilder additionalQuery = makeAdditionalQuery( + devices); + String uriQuery = (additionalQuery != null + ? additionalQuery.toString() : "") + + (mRequest.getUriQuery() != null + ? (";" + mRequest.getUriQuery()) : ""); mRequest = MessageBuilder.modifyRequest(mRequest, null, - (mRequest.getUriQuery() != null - ? mRequest.getUriQuery() : "") - + (additionalQuery == null ? "" - : ";" + additionalQuery), - null, null); + uriQuery, null, null); } - mRDServer.sendRequest(mRequest, mSrcDevice); break; default: @@ -120,7 +116,8 @@ public class ResourceFind extends Resource { } } - private String makeAdditionalQuery(ArrayList deviceList) { + private StringBuilder makeAdditionalQuery( + ArrayList deviceList) { StringBuilder additionalQuery = new StringBuilder(); @@ -139,15 +136,17 @@ public class ResourceFind extends Resource { additionalQuery.append(";"); } } - return additionalQuery.toString(); + return additionalQuery; } @SuppressWarnings("unchecked") private List getResponseDeviceList( HashMap payloadData) { - List deviceList = (List) payloadData - .get(Constants.REQ_DEVICE_LIST); - + ArrayList deviceList = new ArrayList<>(); + if (payloadData.containsKey(Constants.REQ_DEVICE_LIST)) { + deviceList = (ArrayList) payloadData + .get(Constants.REQ_DEVICE_LIST); + } return deviceList; } } @@ -160,9 +159,16 @@ public class ResourceFind extends Resource { mRDServer.sendRequest(request, srcDevice); } else { - StringBuffer uriQuery = new StringBuffer(); - uriQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId()); - + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery + .append(Constants.USER_ID + "=" + srcDevice.getUserId()); + additionalQuery.append(";"); + additionalQuery.append( + Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId()); + + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null + ? (";" + request.getUriQuery()) : ""); StringBuffer uriPath = new StringBuffer(); uriPath.append(Constants.PREFIX_OIC + "/"); uriPath.append(Constants.ACL_URI + "/"); @@ -170,7 +176,7 @@ public class ResourceFind extends Resource { uriPath.append(srcDevice.getUserId()); IRequest requestToAS = MessageBuilder.createRequest( - RequestMethod.GET, uriPath.toString(), uriQuery.toString()); + RequestMethod.GET, uriPath.toString(), uriQuery); mASServer.sendRequest(requestToAS, new AccountReceiveHandler(request, srcDevice)); diff --git a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java index f708115..59c2981 100644 --- a/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java +++ b/cloud/interface/src/main/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresence.java @@ -103,12 +103,11 @@ public class ResourcePresence extends Resource { ResponseStatus.BAD_REQUEST)); return; } + String uriQuery = additionalQuery.toString() + + (mRequest.getUriQuery() != null + ? (";" + mRequest.getUriQuery()) : ""); mRequest = MessageBuilder.modifyRequest(mRequest, null, - additionalQuery - + (mRequest.getUriQuery() != null - ? ";" + mRequest.getUriQuery() - : ""), - null, null); + uriQuery, null, null); } mRDServer.sendRequest(mRequest, mSrcDevice); @@ -153,9 +152,15 @@ public class ResourcePresence extends Resource { @Override public void onDefaultRequestReceived(Device srcDevice, IRequest request) throws ServerException { - StringBuffer uriQuery = new StringBuffer(); - uriQuery.append(Constants.REQ_MEMBER_ID + "=" + srcDevice.getUserId()); - + StringBuffer additionalQuery = new StringBuffer(); + additionalQuery.append(Constants.USER_ID + "=" + srcDevice.getUserId()); + additionalQuery.append(";"); + additionalQuery.append( + Constants.REQ_MEMBER_LIST + "=" + srcDevice.getUserId()); + + String uriQuery = additionalQuery.toString() + + (request.getUriQuery() != null ? (";" + request.getUriQuery()) + : ""); StringBuffer uriPath = new StringBuffer(); uriPath.append(Constants.PREFIX_OIC + "/"); uriPath.append(Constants.ACL_URI + "/"); @@ -163,7 +168,7 @@ public class ResourcePresence extends Resource { uriPath.append(srcDevice.getUserId()); IRequest requestToAS = MessageBuilder.createRequest(RequestMethod.GET, - uriPath.toString(), uriQuery.toString()); + uriPath.toString(), uriQuery); mASServer.sendRequest(requestToAS, new AccountReceiveHandler(request, srcDevice)); diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java index 1f29a41..d53e2e2 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/KeepAliveResourceTest.java @@ -31,7 +31,6 @@ 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; @@ -118,17 +117,6 @@ public class KeepAliveResourceTest { assertTrue(methodCheck(mRes, ResponseStatus.VALID)); } - @Test(expected = BadRequestException.class) - public void testOnDefaultExceptionRequestReceived() { - HashMap payloadData = new HashMap<>(); - Cbor> cbor = new Cbor<>(); - payloadData.put("in", 8); - IRequest request = MessageBuilder.createRequest(RequestMethod.POST, - "/oic/ping", null, ContentFormat.APPLICATION_CBOR, - cbor.encodingPayloadToCbor(payloadData)); - keepAliveResource.onDefaultRequestReceived(mockDevice, request); - } - private boolean methodCheck(IResponse response, ResponseStatus responseStatus) { if (responseStatus == response.getStatus()) diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java index d80d24a..a6ad127 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AccountSessionTest.java @@ -116,10 +116,10 @@ public class AccountSessionTest { // assertion : request msg to the AS is identical to the request msg // from the client 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)); + assertTrue(queryCheck(mReq, Constants.USER_ID)); + assertTrue(payloadCheck(mReq, Constants.DEVICE_ID)); + assertTrue(payloadCheck(mReq, Constants.ACCESS_TOKEN)); + assertTrue(payloadCheck(mReq, Constants.REQ_LOGIN)); } private IRequest makeSignInRequest() { @@ -142,13 +142,16 @@ public class AccountSessionTest { IRequest request = null; HashMap payloadData = new HashMap<>(); payloadData.put(Constants.REQ_LOGIN, false); + payloadData.put(Constants.DEVICE_ID, mDi); + payloadData.put(Constants.ACCESS_TOKEN, + "1689c70ffa245effc563017fee36d250"); request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI, null, ContentFormat.APPLICATION_CBOR, cbor.encodingPayloadToCbor(payloadData)); return request; } - private boolean hashmapCheck(IRequest request, String propertyName) { + private boolean payloadCheck(IRequest request, String propertyName) { Cbor> mCbor = new Cbor<>(); HashMap payloadData = mCbor .parsePayloadFromCbor(request.getPayload(), HashMap.class); @@ -157,4 +160,11 @@ public class AccountSessionTest { else return false; } + + private boolean queryCheck(IRequest request, String propertyName) { + if (request.getUriQueryMap().get(propertyName) != null) + return true; + else + return false; + } } diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java index 30b3a44..89c6a5b 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclGroupTest.java @@ -52,20 +52,21 @@ 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 mMockDevice = mock( + private CoapDevice mMockDevice = mock( CoapDevice.class); - private IRequest mReq = null; - private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem(); - private final CountDownLatch mLatch = new CountDownLatch( + private IRequest mReq = null; + private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem(); + private final CountDownLatch mLatch = new CountDownLatch( 1); + Cbor> mCbor = new Cbor<>(); @Mock - private IRequestChannel mRequestChannel; + private IRequestChannel mRequestChannel; @InjectMocks - private AclGroup mAclGroupHandler = new AclGroup(); + private AclGroup mAclGroupHandler = new AclGroup(); @Before public void setUp() throws Exception { @@ -100,18 +101,23 @@ public class AclGroupTest { "\t--------------OnRequestReceived(AS) Create Group Test------------"); HashMap payloadData = new HashMap<>(); - payloadData.put("gtype", "public"); - Cbor> cbor = new Cbor<>(); + payloadData.put("owner", "u1"); + payloadData.put("members", Arrays.asList("ui")); + payloadData.put("gname", Arrays.asList("home")); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, TEST_RESOURCE_GROUP_URI, null, ContentFormat.APPLICATION_CBOR, - cbor.encodingPayloadToCbor(payloadData)); + mCbor.encodingPayloadToCbor(payloadData)); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) - .containsKey("gtype")); - assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) - .containsKey("gmid")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("owner")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("members")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("gname")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI); } @@ -121,16 +127,18 @@ public class AclGroupTest { "\t--------------OnRequestReceived(AS) Add Member Test------------"); HashMap payloadData = new HashMap<>(); - Cbor> cbor = new Cbor<>(); + payloadData.put("members", Arrays.asList("sampleMember")); IRequest request = MessageBuilder.createRequest(RequestMethod.POST, - TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=add", ContentFormat.APPLICATION_CBOR, - cbor.encodingPayloadToCbor(payloadData)); + mCbor.encodingPayloadToCbor(payloadData)); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) - .containsKey("midlist")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("members")); + assertTrue(mReq.getUriQueryMap().containsKey("op")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); } @@ -141,17 +149,18 @@ public class AclGroupTest { "\t--------------OnRequestReceived(AS) Add Device Test------------"); HashMap payloadData = new HashMap<>(); - payloadData.put("dilist", Arrays.asList("sampleDevice")); - Cbor> cbor = new Cbor<>(); + payloadData.put("devices", Arrays.asList("sampleDevice")); IRequest request = MessageBuilder.createRequest(RequestMethod.POST, - TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=add", ContentFormat.APPLICATION_CBOR, - cbor.encodingPayloadToCbor(payloadData)); + mCbor.encodingPayloadToCbor(payloadData)); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) - .containsKey("dilist")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("devices")); + assertTrue(mReq.getUriQueryMap().containsKey("op")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); } @@ -163,11 +172,12 @@ public class AclGroupTest { "\t--------------OnRequestReceived(AS) Get Group List & Get Group Info Test------------"); IRequest request = MessageBuilder.createRequest(RequestMethod.GET, - TEST_RESOURCE_GROUP_URI, null, null, null); + TEST_RESOURCE_GROUP_URI, "members=u1", null, null); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(mReq.getUriQueryMap().containsKey("mid")); + assertTrue(mReq.getUriQueryMap().containsKey("members")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI); } @@ -177,27 +187,32 @@ public class AclGroupTest { "\t--------------OnRequestReceived(AS) Delete Group Test------------"); IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - TEST_RESOURCE_GROUP_URI, "gid=samplegid", null, null); + TEST_RESOURCE_GROUP_URI + "/g1", "owner=u1", null, null); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(mReq.getUriQueryMap().containsKey("gid")); - assertTrue(mReq.getUriQueryMap().containsKey("gmid")); - assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI); + assertTrue(mReq.getUriQueryMap().containsKey("owner")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); + assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/g1"); } @Test public void testDeleteMemberRequestReceived() throws Exception { System.out.println( "\t--------------OnRequestReceived(AS) Delete Member Test------------"); - - IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", null, null, - null); + HashMap payloadData = new HashMap<>(); + payloadData.put("members", Arrays.asList("sampleMember")); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=delete", + ContentFormat.APPLICATION_CBOR, + mCbor.encodingPayloadToCbor(payloadData)); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(mReq.getUriQueryMap().containsKey("midlist")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("members")); + assertTrue(mReq.getUriQueryMap().containsKey("op")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); } @@ -206,14 +221,19 @@ public class AclGroupTest { public void testDeleteDeviceRequestReceived() throws Exception { System.out.println( "\t--------------OnRequestReceived(AS) Delete Member Test------------"); - - IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", - "dilist=sampledi", null, null); + HashMap payloadData = new HashMap<>(); + payloadData.put("devices", Arrays.asList("sampleDevice")); + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup", "op=delete", + ContentFormat.APPLICATION_CBOR, + mCbor.encodingPayloadToCbor(payloadData)); mAclGroupHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(mReq.getUriQueryMap().containsKey("dilist")); + assertTrue(mCbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) + .containsKey("devices")); + assertTrue(mReq.getUriQueryMap().containsKey("op")); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_GROUP_URI + "/" + "sampleGroup"); } diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java index 443eb68..b6bfe2d 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/AclInviteTest.java @@ -103,6 +103,7 @@ public class AclInviteTest { HashMap invite = new HashMap<>(); invite.put("gid", "g0001"); invite.put("mid", "u0001"); + payloadData.put("uid", "u0001"); payloadData.put("invite", Arrays.asList(invite)); Cbor> cbor = new Cbor<>(); IRequest request = MessageBuilder.createRequest(RequestMethod.POST, @@ -111,6 +112,7 @@ public class AclInviteTest { mAclInviteHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) .containsKey("uid")); assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class) @@ -124,16 +126,36 @@ public class AclInviteTest { } @Test - public void testGetRequestReceived() throws Exception { + public void testGetRequestReceivedAccept() throws Exception { System.out.println( "\t--------------OnRequestReceived(AS) Get Message Test------------"); IRequest request = MessageBuilder.createRequest(RequestMethod.GET, - TEST_RESOURCE_INVITE_URI, null, null, null); + TEST_RESOURCE_INVITE_URI, "gid=g0001;accept=1", null, null); mAclInviteHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); assertTrue(mReq.getUriQueryMap().containsKey("uid")); + assertTrue(mReq.getUriQueryMap().containsKey("gid")); + assertTrue(mReq.getUriQueryMap().containsKey("accept")); + assertTrue(mReq.getUriQueryMap().get("accept").get(0).equals("1")); + assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI); + } + + @Test + public void testGetRequestReceivedDeny() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Get Message Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.GET, + TEST_RESOURCE_INVITE_URI, "gid=g0001;accept=0", null, null); + mAclInviteHandler.onRequestReceived(mMockDevice, request); + + assertTrue(mLatch.await(1L, SECONDS)); + assertTrue(mReq.getUriQueryMap().containsKey("uid")); + assertTrue(mReq.getUriQueryMap().containsKey("gid")); + assertTrue(mReq.getUriQueryMap().containsKey("accept")); + assertTrue(mReq.getUriQueryMap().get("accept").get(0).equals("0")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI); } @@ -143,11 +165,12 @@ public class AclInviteTest { "\t--------------OnRequestReceived(AS) Delete Message Test------------"); IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - TEST_RESOURCE_INVITE_URI, "gid=g0001", null, null); + TEST_RESOURCE_INVITE_URI, "gid=g0001;mid=u0002", null, null); mAclInviteHandler.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); assertTrue(mReq.getUriQueryMap().containsKey("gid")); + assertTrue(mReq.getUriQueryMap().containsKey("mid")); assertTrue(mReq.getUriQueryMap().containsKey("uid")); assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI); } diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CrlTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CrlTest.java new file mode 100644 index 0000000..3bab49f --- /dev/null +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/account/CrlTest.java @@ -0,0 +1,83 @@ +package org.iotivity.cloud.ciserver.resources.proxy.account; + +import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + +import java.util.concurrent.CountDownLatch; + +import org.iotivity.cloud.base.OICConstants; +import org.iotivity.cloud.base.device.CoapDevice; +import org.iotivity.cloud.base.device.IRequestChannel; +import org.iotivity.cloud.base.protocols.IRequest; +import org.iotivity.cloud.base.protocols.MessageBuilder; +import org.iotivity.cloud.base.protocols.coap.CoapRequest; +import org.iotivity.cloud.base.protocols.enums.RequestMethod; +import org.iotivity.cloud.ciserver.Constants; +import org.iotivity.cloud.ciserver.DeviceServerSystem; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +public class CrlTest { + + private static final String TEST_RESOURCE_CRL_URI = "/" + + OICConstants.PREFIX_OIC + "/" + OICConstants.CREDPROV_URI + "/" + + Constants.REQ_CRL; + private CoapDevice mMockDevice = mock(CoapDevice.class); + private IRequest mReq = null; + private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem(); + final CountDownLatch mLatch = new CountDownLatch(1); + + @Mock + private IRequestChannel mRequestChannel; + + @InjectMocks + private Crl mCertHandler = new Crl(); + + @Before + public void setUp() throws Exception { + mReq = null; + MockitoAnnotations.initMocks(this); + mDeviceServerSystem.addResource(mCertHandler); + // callback mock + Mockito.doAnswer(new Answer() { + @Override + public CoapRequest answer(InvocationOnMock invocation) + throws Throwable { + Object[] args = invocation.getArguments(); + CoapRequest request = (CoapRequest) args[0]; + System.out.println( + "\t----------payload : " + request.getPayloadString()); + System.out.println( + "\t----------uripath : " + request.getUriPath()); + System.out.println( + "\t---------uriquery : " + request.getUriQuery()); + mReq = request; + mLatch.countDown(); + return null; + } + }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class), + Mockito.any(CoapDevice.class)); + } + + @Test + public void testOnDefaultRequestReceived() throws Exception { + System.out.println( + "\t--------------OnRequestReceived(AS) Test------------"); + + IRequest request = MessageBuilder.createRequest(RequestMethod.POST, + TEST_RESOURCE_CRL_URI, null, null, null); + mCertHandler.onRequestReceived(mMockDevice, request); + + assertTrue(mLatch.await(1L, SECONDS)); + assertEquals(mReq.getUriPath(), TEST_RESOURCE_CRL_URI); + } + +} diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java index 1d58984..e2b52a7 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/DevicePresenceTest.java @@ -72,6 +72,7 @@ public class DevicePresenceTest { MockitoAnnotations.initMocks(this); mDeviceServerSystem.addResource(mPrsHandler); Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId(); + Mockito.doReturn("mockUserId").when(mMockDevice).getUserId(); Mockito.doAnswer(new Answer() { @Override public CoapRequest answer(InvocationOnMock invocation) @@ -140,9 +141,11 @@ public class DevicePresenceTest { mDeviceServerSystem.onRequestReceived(mMockDevice, request); assertTrue(mLatch.await(1L, SECONDS)); HashMap> queryMap = mReq.getUriQueryMap(); - assertTrue(queryMap.containsKey("mid")); + assertTrue(queryMap.containsKey("uid")); + assertTrue(queryMap.containsKey("members")); assertTrue(mReq.getMethod().equals(RequestMethod.GET)); - assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null"); + assertEquals(mReq.getUriPath(), + Constants.GROUP_FULL_URI + "/mockUserId"); } // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived @@ -175,8 +178,10 @@ public class DevicePresenceTest { assertTrue(mLatch.await(1L, SECONDS)); assertTrue(mReq.getMethod().equals(RequestMethod.GET)); HashMap> queryMap = mReq.getUriQueryMap(); - assertTrue(queryMap.containsKey("mid")); - assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null"); + assertTrue(queryMap.containsKey("uid")); + assertTrue(queryMap.containsKey("members")); + assertEquals(mReq.getUriPath(), + Constants.GROUP_FULL_URI + "/mockUserId"); } public IRequest makePresenceEntireDevice() { @@ -215,15 +220,15 @@ public class DevicePresenceTest { deviceList.add("device1"); deviceList.add("device2"); deviceList.add("device3"); - responsePayload.put("dilist", deviceList); + responsePayload.put("devices", deviceList); responsePayload.put("gid", "g0001"); responsePayload.put("gmid", "u0001"); ArrayList midList = new ArrayList(); midList.add("u0001"); - responsePayload.put("midlist", midList); + responsePayload.put("members", midList); IRequest requestFromCitoAs = MessageBuilder.createRequest( RequestMethod.GET, Constants.GROUP_FULL_URI + "/g0001", - "mid=null"); + "uid=mockUserId;members=mockUserId"); IResponse response = MessageBuilder.createResponse(requestFromCitoAs, ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, cbor.encodingPayloadToCbor(responsePayload)); diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java index 109b22a..9173cdc 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourceFindTest.java @@ -59,7 +59,7 @@ import org.mockito.stubbing.Answer; 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); + private CoapDevice mMockDevice = mock(CoapDevice.class); private IResponse mRes = null; private IRequest mReq = null; private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem(); @@ -77,7 +77,8 @@ public class ResourceFindTest { public void setUp() throws Exception { mRes = null; mReq = null; - Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId(); + Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId(); + Mockito.doReturn("mockUserId").when(mMockDevice).getUserId(); MockitoAnnotations.initMocks(this); mDeviceServerSystem.addResource(mResHandler); // callback mock @@ -90,7 +91,7 @@ public class ResourceFindTest { mRes = resp; return resp; } - }).when(mockDevice).sendResponse(Mockito.anyObject()); + }).when(mMockDevice).sendResponse(Mockito.anyObject()); Mockito.doAnswer(new Answer() { @Override public CoapRequest answer(InvocationOnMock invocation) @@ -136,7 +137,7 @@ public class ResourceFindTest { "rt=core.light;di=" + "device1"); @InjectMocks ResourceFind.AccountReceiveHandler specificDeviceHandler = mResHandler.new AccountReceiveHandler( - requestSpecificDevice, mockDevice); + requestSpecificDevice, mMockDevice); @Test public void testSpecificDeviceonResponseReceived() @@ -158,15 +159,15 @@ public class ResourceFindTest { .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, "rt=core.light"); @InjectMocks - ResourceFind.AccountReceiveHandler handler = mResHandler.new AccountReceiveHandler( - requestEntireDevices, mockDevice); + ResourceFind.AccountReceiveHandler entireDevicehandler = mResHandler.new AccountReceiveHandler( + requestEntireDevices, mMockDevice); @Test public void testEntireDeviceonResponseReceived() throws ClientException { System.out.println( "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------"); IResponse response = responseFromAccountServer(); - handler.onResponseReceived(response); + entireDevicehandler.onResponseReceived(response); HashMap> queryMap = mReq.getUriQueryMap(); assertTrue(mReq.getMethod() == RequestMethod.GET); assertTrue(queryMap.get("rt").contains("core.light")); @@ -180,7 +181,7 @@ public class ResourceFindTest { .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null); @InjectMocks ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = mResHandler.new AccountReceiveHandler( - requestEntireDevicesNoQuery, mockDevice); + requestEntireDevicesNoQuery, mMockDevice); @Test public void testEntireDeviceNoQueryonResponseReceived() @@ -203,11 +204,13 @@ public class ResourceFindTest { "\t--------------OnRequestReceived(RD) Resource Find (entire deivces) Test------------"); IRequest request = MessageBuilder.createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, "rt=core.light"); - mResHandler.onRequestReceived(mockDevice, request); + mResHandler.onRequestReceived(mMockDevice, request); HashMap> queryMap = mReq.getUriQueryMap(); assertTrue(mLatch.await(1L, SECONDS)); - assertTrue(queryMap.containsKey("mid")); - assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null"); + assertTrue(queryMap.containsKey("uid")); + assertTrue(queryMap.containsKey("members")); + assertEquals(mReq.getUriPath(), + Constants.GROUP_FULL_URI + "/mockUserId"); } @Test @@ -217,7 +220,7 @@ 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); - mResHandler.onRequestReceived(mockDevice, request); + mResHandler.onRequestReceived(mMockDevice, request); HashMap> queryMap = mReq.getUriQueryMap(); // assertion: if the request packet from the CI contains the query // which includes device ID and the accesstoken @@ -235,12 +238,12 @@ public class ResourceFindTest { deviceList.add("device1"); deviceList.add("device2"); deviceList.add("device3"); - responsePayload.put("dilist", deviceList); + responsePayload.put("devices", deviceList); responsePayload.put("gid", "g0001"); responsePayload.put("gmid", "u0001"); ArrayList midList = new ArrayList(); midList.add("u0001"); - responsePayload.put("midlist", midList); + responsePayload.put("memebers", midList); IResponse response = MessageBuilder.createResponse(requestEntireDevices, ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, cbor.encodingPayloadToCbor(responsePayload)); diff --git a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java index 9ed3e0a..93ef37a 100644 --- a/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java +++ b/cloud/interface/src/test/java/org/iotivity/cloud/ciserver/resources/proxy/rd/ResourcePresenceTest.java @@ -73,6 +73,7 @@ public class ResourcePresenceTest { mRes = null; mReq = null; Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId(); + Mockito.doReturn("mockUserId").when(mMockDevice).getUserId(); MockitoAnnotations.initMocks(this); deviceServerSystem.addResource(adHandler); Mockito.doAnswer(new Answer() { @@ -107,8 +108,10 @@ public class ResourcePresenceTest { // 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(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null"); + assertTrue(queryMap.containsKey("uid")); + assertTrue(queryMap.containsKey("members")); + assertEquals(mReq.getUriPath(), + Constants.GROUP_FULL_URI + "/mockUserId"); } @Test @@ -121,8 +124,10 @@ public class ResourcePresenceTest { deviceServerSystem.onRequestReceived(mMockDevice, request); HashMap> queryMap = mReq.getUriQueryMap(); assertTrue(latch.await(1L, SECONDS)); - assertTrue(queryMap.containsKey("mid")); - assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null"); + assertTrue(queryMap.containsKey("uid")); + assertTrue(queryMap.containsKey("members")); + assertEquals(mReq.getUriPath(), + Constants.GROUP_FULL_URI + "/mockUserId"); } IRequest requestEntireDevices = MessageBuilder @@ -173,12 +178,11 @@ public class ResourcePresenceTest { deviceList.add("device1"); deviceList.add("device2"); deviceList.add("device3"); - responsePayload.put("dilist", deviceList); + responsePayload.put("devices", deviceList); responsePayload.put("gid", "g0001"); - responsePayload.put("gmid", "u0001"); ArrayList midList = new ArrayList(); midList.add("u0001"); - responsePayload.put("midlist", midList); + responsePayload.put("members", midList); IResponse response = MessageBuilder.createResponse(requestEntireDevices, ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR, cbor.encodingPayloadToCbor(responsePayload)); -- 2.7.4