diff options
author | yeonghun.nam <yeonghun.nam@samsung.com> | 2016-11-15 15:03:30 +0900 |
---|---|---|
committer | Jee Hyeok Kim <jihyeok13.kim@samsung.com> | 2016-11-22 05:55:47 +0000 |
commit | 3becdbad309742d707d33449500446fe87f49a5a (patch) | |
tree | 4ee7c1594bb7ee796c7bd6b8974a0c510c4faeae /cloud | |
parent | 9d686a6b7e8f2a8bdb893056d44665da370c7cb7 (diff) | |
download | iotivity-3becdbad309742d707d33449500446fe87f49a5a.tar.gz iotivity-3becdbad309742d707d33449500446fe87f49a5a.tar.bz2 iotivity-3becdbad309742d707d33449500446fe87f49a5a.zip |
[IOT-1556] Changes in cloud according to the stateless request/response model (group features)
The cloud implementation is revised, due to the protocol update to the stateless request/response model in the device-to-cloud communication.
- Group Management
- Group creation implementation
- add/update/delete properties
- delete group
- get group information
patch #6: POST /oic/acl/group/ response correction
Change-Id: I7db5836bff5b4cb33a7effa523f9388ff4240262
Signed-off-by: yeonghun.nam <yeonghun.nam@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/14333
Reviewed-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
Tested-by: Jee Hyeok Kim <jihyeok13.kim@samsung.com>
(cherry picked from commit 834187fe46f97e101496d0d5f156efdbd330f3fa)
Reviewed-on: https://gerrit.iotivity.org/gerrit/14585
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Diffstat (limited to 'cloud')
8 files changed, 202 insertions, 90 deletions
diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/GroupTable.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/GroupTable.java index 27eebd24f..42e79a21a 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/GroupTable.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/db/GroupTable.java @@ -29,7 +29,6 @@ import org.iotivity.cloud.accountserver.Constants; import org.iotivity.cloud.base.exception.ServerException.BadRequestException; public class GroupTable { - // TODO add doxygen private String gid = null; private String gname = null; private String owner = null; diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerManager.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerManager.java index d68490c3f..820f62e72 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerManager.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerManager.java @@ -167,14 +167,20 @@ public class GroupBrokerManager { } storeGroupInfo(uid, gid, gname, parent); - return makePostResponse(gid, gname); + return makePostResponse(uid, gid, gname, parent); } - private HashMap<String, Object> makePostResponse(String gid, String gname) { + private HashMap<String, Object> makePostResponse(String uid, String gid, + String gname, String parent) { HashMap<String, Object> response = new HashMap<>(); response.put(Constants.KEYFIELD_GID, gid); response.put(Constants.KEYFIELD_GROUP_NAME, gname); + response.put(Constants.KEYFIELD_GROUP_OWNER, uid); + response.put(Constants.KEYFIELD_GROUP_MEMBERS, Arrays.asList(uid)); + if (parent != null) { + response.put(Constants.KEYFIELD_GROUP_PARENT, parent); + } Log.d("Group post response : " + response.toString()); diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupManager.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupManager.java index f0e87d737..1a005e74b 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupManager.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupManager.java @@ -156,17 +156,18 @@ public class GroupManager { * member uuid list */ public void deleteMembersFromGroup(String gid, ArrayList<String> members) { - // delete devices owned by deleted members GroupTable groupTable = getGroupTable(gid); ArrayList<String> devices = groupTable.getDevices(); - ArrayList<String> deletedDevices = new ArrayList<String>(); - for (String device : devices) { - if (members.contains(findDeviceOwner(device))) { - deletedDevices.add(device); + if (devices != null) { + // delete devices owned by deleted members + ArrayList<String> deletedDevices = new ArrayList<String>(); + for (String device : devices) { + if (members.contains(findDeviceOwner(device))) { + deletedDevices.add(device); + } } + deleteDevicesFromGroup(gid, deletedDevices); } - deleteDevicesFromGroup(gid, deletedDevices); - deleteProperties(gid, Constants.KEYFIELD_GROUP_MEMBERS, members); deleteProperties(gid, Constants.KEYFIELD_GROUP_MASTERS, members); } @@ -292,6 +293,9 @@ public class GroupManager { public <T> ArrayList<T> getAddPropertyValues(String gid, String property, ArrayList<T> values) { GroupTable groupTable = getGroupTable(gid); + if (groupTable == null) { + throw new BadRequestException("group " + gid + " does not exist"); + } ArrayList<T> propertyValues = groupTable.getPropertyValue(property); ArrayList<T> addedValues = new ArrayList<>(); for (int i = 0; i < values.size(); i++) { @@ -316,6 +320,9 @@ public class GroupManager { public <T> ArrayList<T> getDeletePropertyValues(String gid, String property, ArrayList<T> values) { GroupTable groupTable = getGroupTable(gid); + if (groupTable == null) { + throw new BadRequestException("group " + gid + " does not exist"); + } ArrayList<T> propertyValues = groupTable.getPropertyValue(property); ArrayList<T> deletedValues = new ArrayList<>(); for (int i = 0; i < propertyValues.size(); i++) { @@ -335,7 +342,7 @@ public class GroupManager { * user uuid */ public void verifyGetRequestAuthz(String gid, String mid) { - verifyMemberExistanceInGroup(gid, mid); + verifyMemberExistenceInGroup(gid, mid); } /** @@ -368,8 +375,8 @@ public class GroupManager { String parentGid = groupTable.getParent(); // delete subgroup ID of the parent group if (parentGid != null && !parentGid.isEmpty()) { - ArrayList<Object> gidList = new ArrayList<Object>(); - gidList.add(gid); + ArrayList<Object> gidList = new ArrayList<Object>( + Arrays.asList(gid)); deleteProperties(parentGid, Constants.KEYFIELD_GROUP_SUBGROUPS, gidList); } @@ -555,8 +562,11 @@ public class GroupManager { } } - private void verifyMemberExistanceInGroup(String gid, String mid) { + private void verifyMemberExistenceInGroup(String gid, String mid) { GroupTable groupTable = getGroupTable(gid); + if (groupTable == null) { + throw new BadRequestException("group " + gid + " does not exist"); + } if (!groupTable.getMembers().contains(mid)) { throw new BadRequestException("uid is not a member of the group"); } @@ -573,7 +583,7 @@ public class GroupManager { case Constants.KEYFIELD_GROUP_DEVICES: verifyDeviceOwner(mid, (ArrayList<String>) properties.get(key)); - verifyExistanceInParentGroup(gid, key, + verifyExistenceInParentGroup(gid, key, (ArrayList<Object>) properties.get(key)); break; case Constants.KEYFIELD_GROUP_RESOURCES: @@ -586,8 +596,8 @@ public class GroupManager { verifyResourceOwner(mid, (ArrayList<HashMap<String, Object>>) properties .get(key)); - verifyExistanceInParentGroup(gid, key, - filterResourceExistanceInParentGroupDeviceProperty( + verifyExistenceInParentGroup(gid, key, + filterResourceExistenceInParentGroupDeviceProperty( gid, (ArrayList<HashMap<String, Object>>) properties .get(key))); @@ -596,7 +606,7 @@ public class GroupManager { case Constants.KEYFIELD_GROUP_MASTERS: // TODO verify if members are registered to the Account user // DB - verifyExistanceInParentGroup(gid, + verifyExistenceInParentGroup(gid, Constants.KEYFIELD_GROUP_MEMBERS, (ArrayList<Object>) properties.get(key)); break; @@ -651,7 +661,7 @@ public class GroupManager { throw new BadRequestException("cannot remove owner Id"); } case Constants.KEYFIELD_GROUP_MASTERS: - verifyExistanceInParentGroup(gid, key, + verifyExistenceInParentGroup(gid, key, (ArrayList<Object>) properties.get(key)); break; default: @@ -661,7 +671,7 @@ public class GroupManager { } } - private ArrayList<HashMap<String, Object>> filterResourceExistanceInParentGroupDeviceProperty( + private ArrayList<HashMap<String, Object>> filterResourceExistenceInParentGroupDeviceProperty( String gid, ArrayList<HashMap<String, Object>> resources) { GroupTable parentGroupTable = getParentGroupTable(gid); if (parentGroupTable == null) { @@ -690,7 +700,7 @@ public class GroupManager { } } - private <T> void verifyExistanceInParentGroup(String gid, String property, + private <T> void verifyExistenceInParentGroup(String gid, String property, ArrayList<T> values) { GroupTable parentGroupTable = getParentGroupTable(gid); if (parentGroupTable == null) { @@ -701,11 +711,11 @@ public class GroupManager { .getPropertyValue(property); if (groupValues == null) { throw new BadRequestException( - "verifying parent group existance failed"); + "verifying parent group Existence failed"); } if (!groupValues.containsAll(values)) { throw new BadRequestException( - "verifying parent group existance failed"); + "verifying parent group Existence failed"); } } @@ -837,4 +847,4 @@ public class GroupManager { property + " is not supported property in the group"); } } -}
\ No newline at end of file +} diff --git a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResource.java b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResource.java index 9b163a310..5601ce3b8 100644 --- a/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResource.java +++ b/cloud/account/src/main/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResource.java @@ -89,10 +89,15 @@ public class GroupResource extends Resource { HashMap<String, Object> parsedPayload = mCbor .parsePayloadFromCbor(payload, HashMap.class); - checkPayloadException(Arrays.asList(Constants.REQ_UUID_ID), + checkQueryException(Arrays.asList(Constants.KEYFIELD_UID), + request.getUriQueryMap()); + + checkPayloadException(Arrays.asList(Constants.KEYFIELD_GROUP_MEMBERS), parsedPayload); - String uid = parsedPayload.get(Constants.REQ_UUID_ID).toString(); + // get user id + String uid = request.getUriQueryMap().get(Constants.KEYFIELD_UID) + .get(0); if (uid == null || uid.isEmpty()) { throw new BadRequestException( @@ -105,8 +110,8 @@ public class GroupResource extends Resource { Constants.KEYFIELD_GROUP_PARENT); if (parent != null) { - ArrayList<String> properties = new ArrayList<>(); - properties.add(Constants.KEYFIELD_GROUP); + ArrayList<String> properties = new ArrayList<>( + Arrays.asList(Constants.KEYFIELD_GROUP)); GroupBrokerManager.getInstance().verifyAuthorization(uid, parent, properties, UserOperation.ADD); } @@ -128,11 +133,16 @@ public class GroupResource extends Resource { IRequest request) { HashMap<String, List<String>> queryMap = request.getUriQueryMap(); - checkQueryException(Arrays.asList(Constants.REQ_UUID_ID), queryMap); - - List<String> deviceList = queryMap.get(Constants.REQ_UUID_ID); - String uid = deviceList.get(0); + checkQueryException(Arrays.asList(Constants.REQ_UUID_ID, + Constants.KEYFIELD_GROUP_MEMBERS), queryMap); + String uid = queryMap.get(Constants.REQ_UUID_ID).get(0); + if (!uid.equals( + queryMap.get(Constants.KEYFIELD_GROUP_MEMBERS).get(0))) { + throw new BadRequestException( + Constants.REQ_UUID_ID + "query value should be equal to " + + Constants.KEYFIELD_GROUP_MEMBERS + "query value"); + } switch (request.getObserve()) { case SUBSCRIBE: GroupBrokerManager.getInstance().addObserver(uid, srcDevice, @@ -186,13 +196,20 @@ public class GroupResource extends Resource { HashMap<String, Object> payloadData = mCbor .parsePayloadFromCbor(request.getPayload(), HashMap.class); - // check if the payload has the key "uid" - checkPayloadException(Constants.REQ_UUID_ID, payloadData); + // TODO to be deleted + if (payloadData.containsKey(Constants.KEYFIELD_UID)) { + throw new BadRequestException( + " uid key is not supported in the payload"); + } + + checkQueryException(Arrays.asList(Constants.KEYFIELD_UID), + request.getUriQueryMap()); - // get "uid" value - String mid = (String) payloadData.get(Constants.REQ_UUID_ID); + // get user id + String uid = request.getUriQueryMap().get(Constants.KEYFIELD_UID) + .get(0); - if (mid == null || mid.isEmpty()) { + if (uid == null || uid.isEmpty()) { throw new BadRequestException( Constants.REQ_UUID_ID + " is null or empty"); } @@ -200,11 +217,10 @@ public class GroupResource extends Resource { String gid = request.getUriPathSegments() .get(getUriPathSegments().size()); - payloadData.remove(Constants.REQ_UUID_ID); - // process POST oic/acl/group/<gid> to update group info - if (request.getUriQuery() == null) { - handlePostUpdateRequest(gid, mid, payloadData); + if (!request.getUriQueryMap() + .containsKey(Constants.REQ_GROUP_QUERY_OPERATION)) { + handlePostUpdateRequest(gid, uid, payloadData); } else { checkQueryException(Constants.REQ_GROUP_QUERY_OPERATION, request.getUriQueryMap()); @@ -214,10 +230,10 @@ public class GroupResource extends Resource { switch (postOption) { case Constants.REQ_GROUP_QUERY_ADD: - handlePostAddRequest(gid, mid, payloadData); + handlePostAddRequest(gid, uid, payloadData); break; case Constants.REQ_GROUP_QUERY_DELETE: - handlePostDeleteRequest(gid, mid, payloadData); + handlePostDeleteRequest(gid, uid, payloadData); break; default: throw new PreconditionFailedException( @@ -378,13 +394,20 @@ public class GroupResource extends Resource { private IResponse handleGroupGetRequest(IRequest request) throws ServerException { - checkQueryException(Constants.REQ_UUID_ID, request.getUriQueryMap()); + HashMap<String, List<String>> queryMap = request.getUriQueryMap(); - HashMap<String, Object> responsePayload = null; + checkQueryException(Arrays.asList(Constants.REQ_UUID_ID, + Constants.KEYFIELD_GROUP_MEMBERS), queryMap); - String mid = request.getUriQueryMap().get(Constants.REQ_UUID_ID).get(0); + String uid = queryMap.get(Constants.REQ_UUID_ID).get(0); + if (!uid.equals( + queryMap.get(Constants.KEYFIELD_GROUP_MEMBERS).get(0))) { + throw new BadRequestException( + Constants.REQ_UUID_ID + "query value should be equal to " + + Constants.KEYFIELD_GROUP_MEMBERS + "query value"); + } - if (mid == null || mid.isEmpty()) { + if (uid == null || uid.isEmpty()) { throw new BadRequestException( Constants.REQ_UUID_ID + " is null or empty"); } @@ -392,7 +415,9 @@ public class GroupResource extends Resource { String gid = request.getUriPathSegments() .get(getUriPathSegments().size()); - GroupManager.getInstance().verifyGetRequestAuthz(gid, mid); + GroupManager.getInstance().verifyGetRequestAuthz(gid, uid); + + HashMap<String, Object> responsePayload = null; switch (request.getObserve()) { case NOTHING: @@ -413,12 +438,20 @@ public class GroupResource extends Resource { private IResponse handleGroupDeleteRequest(IRequest request) throws ServerException { - checkQueryException(Arrays.asList(Constants.REQ_UUID_ID), - request.getUriQueryMap()); + HashMap<String, List<String>> queryMap = request.getUriQueryMap(); + + checkQueryException(Arrays.asList(Constants.REQ_UUID_ID, + Constants.KEYFIELD_GROUP_OWNER), queryMap); - String mid = request.getUriQueryMap().get(Constants.REQ_UUID_ID).get(0); + String uid = queryMap.get(Constants.REQ_UUID_ID).get(0); + if (!uid.equals(queryMap.get(Constants.KEYFIELD_GROUP_OWNER).get(0))) { + throw new BadRequestException( + Constants.REQ_UUID_ID + "query value should be equal to " + + Constants.KEYFIELD_GROUP_OWNER + + "query value to delete group"); + } - if (mid == null || mid.isEmpty()) { + if (uid == null || uid.isEmpty()) { throw new BadRequestException( Constants.REQ_UUID_ID + " is null or empty"); } @@ -426,7 +459,7 @@ public class GroupResource extends Resource { String gid = request.getUriPathSegments() .get(getUriPathSegments().size()); - GroupManager.getInstance().verifyDeleteRequestAuthz(gid, mid); + GroupManager.getInstance().verifyDeleteRequestAuthz(gid, uid); String parent = GroupManager.getInstance().getGroupTable(gid) .getParent(); diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerTest.java index 6dfa5a3cc..72e2cf5ab 100644 --- a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerTest.java +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupBrokerTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.concurrent.CountDownLatch; @@ -33,7 +34,8 @@ import org.mockito.stubbing.Answer; public class GroupBrokerTest { private String GROUP_URI = Constants.GROUP_FULL_URI; - private String UID_QUERY = "uid="; + private String UID_QUERY = Constants.KEYFIELD_UID + + "="; private String mGid1 = "g1"; private String mGid2 = "g2"; private String mGname1 = "myHome"; @@ -91,6 +93,8 @@ public class GroupBrokerTest { assertTrue(responseCodeCheck(mResponse, ResponseStatus.CHANGED)); assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GID)); assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_NAME)); + assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_OWNER)); + assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_MEMBERS)); assertTrue(mLatch.await(2L, SECONDS)); } @@ -103,6 +107,9 @@ public class GroupBrokerTest { assertTrue(responseCodeCheck(mResponse, ResponseStatus.CHANGED)); assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GID)); assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_NAME)); + assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_OWNER)); + assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_MEMBERS)); + assertTrue(checkProperty(mResponse, Constants.KEYFIELD_GROUP_PARENT)); assertTrue(mLatch.await(2L, SECONDS)); } @@ -116,7 +123,9 @@ public class GroupBrokerTest { @Test public void testGetGroupList() throws Exception { getTestMethodName(); - GroupBrokerManager.getInstance().createGroup(mUid1, mGid1, null, null); + sendCreateGroupRequest(mMockDevice, mUid1, mGname1, null); + String gid = getProperty(mResponse, Constants.KEYFIELD_GID).toString(); + sendCreateGroupRequest(mMockDevice, mUid1, null, gid); sendGetGroupResquest(mMockDevice, mUid1); assertTrue(responseCodeCheck(mResponse, ResponseStatus.CONTENT)); } @@ -149,11 +158,14 @@ public class GroupBrokerTest { String gname, String parent) { IRequest request = null; HashMap<String, Object> payloadData = new HashMap<String, Object>(); - payloadData.put(Constants.REQ_UUID_ID, uid); payloadData.put(Constants.KEYFIELD_GROUP_NAME, gname); payloadData.put(Constants.KEYFIELD_GROUP_PARENT, parent); + payloadData.put(Constants.KEYFIELD_GROUP_MEMBERS, + new ArrayList<String>(Arrays.asList(uid))); + payloadData.put(Constants.KEYFIELD_OID, uid); request = MessageBuilder.createRequest(RequestMethod.POST, GROUP_URI, - null, ContentFormat.APPLICATION_CBOR, + Constants.KEYFIELD_UID + "=" + uid, + ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(payloadData)); mGroupResource.onDefaultRequestReceived(device, request); } @@ -161,7 +173,8 @@ public class GroupBrokerTest { private void sendGetGroupResquest(CoapDevice device, String uid) { IRequest request = null; request = MessageBuilder.createRequest(RequestMethod.GET, GROUP_URI, - UID_QUERY + uid); + UID_QUERY + uid + ";" + Constants.KEYFIELD_GROUP_MEMBERS + "=" + + uid); mGroupResource.onDefaultRequestReceived(device, request); } @@ -169,7 +182,8 @@ public class GroupBrokerTest { Observe obs) { IRequest request = null; request = MessageBuilder.createRequest(RequestMethod.GET, GROUP_URI, - UID_QUERY + uid); + UID_QUERY + uid + ";" + Constants.KEYFIELD_GROUP_MEMBERS + "=" + + uid); ((CoapRequest) request).setObserve(obs); mGroupResource.onDefaultRequestReceived(device, request); } diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResourceTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResourceTest.java index 92766a13f..325a49850 100644 --- a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResourceTest.java +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/group/GroupResourceTest.java @@ -399,6 +399,21 @@ public class GroupResourceTest { } @Test(expected = ServerException.BadRequestException.class) + public void testGetNonExistongGroup() throws Exception { + getTestMethodName(); + HashMap<String, Object> properties = new HashMap<>(); + ArrayList<String> members = new ArrayList<>(); + members.add(mUid2); + members.add(mUid3); + properties.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + // add members to the parent group + GroupManager.getInstance().addMembersToGroup(mGid1, members); + getGroupInfo(mMockDevice, mGid1 + "notExist", mUid1); + assertTrue(methodCheck(mResponse, ResponseStatus.CONTENT)); + assertTrue(mLatch.await(2L, SECONDS)); + } + + @Test(expected = ServerException.BadRequestException.class) public void testGetGroupByNonExistingUser() throws Exception { getTestMethodName(); HashMap<String, Object> properties = new HashMap<>(); @@ -502,6 +517,15 @@ public class GroupResourceTest { } @Test(expected = ServerException.BadRequestException.class) + public void testDeleteNonExistingGroup() throws Exception { + getTestMethodName(); + setExampleGroup(); + deleteGroup(mMockDevice, mGid1 + "notExist", mUid1); + assertTrue(methodCheck(mResponse, ResponseStatus.DELETED)); + assertTrue(mLatch.await(2L, SECONDS)); + } + + @Test(expected = ServerException.BadRequestException.class) public void testDeleteGroupByMaster() throws Exception { getTestMethodName(); setExampleGroup(); @@ -601,6 +625,33 @@ public class GroupResourceTest { assertTrue(mLatch.await(2L, SECONDS)); } + @Test(expected = ServerException.BadRequestException.class) + public void testUpdatePropertiesToNonExistingGroup() throws Exception { + getTestMethodName(); + setExampleGroup(); + HashMap<String, Object> properties = new HashMap<>(); + ArrayList<HashMap<String, Object>> resources = new ArrayList<>(); + // add resource 1 + resources.add(makeResources("/di/" + mDi1 + "/a/switch/1", + Arrays.asList("core.switch"), + Arrays.asList("oic.if.baseline"))); + properties.put(Constants.KEYFIELD_GROUP_RESOURCES, resources); + + ArrayList<String> members = new ArrayList<>(); + members.add(mUid1); + members.add(mUid2); + + properties.put(Constants.KEYFIELD_GROUP_MEMBERS, members); + + ArrayList<String> masters = new ArrayList<>(); + masters.add(mUid3); + properties.put(Constants.KEYFIELD_GROUP_MASTERS, masters); + + updateProperties(mMockDevice, mGid1 + "nonExists", mUid1, properties); + assertTrue(methodCheck(mResponse, ResponseStatus.CHANGED)); + assertTrue(mLatch.await(2L, SECONDS)); + } + private HashMap<String, Object> setExampleGroup() throws Exception { HashMap<String, Object> properties = new HashMap<>(); ArrayList<HashMap<String, Object>> resources = new ArrayList<>(); @@ -635,6 +686,7 @@ public class GroupResourceTest { subgroupProperties.put(Constants.KEYFIELD_GROUP_RESOURCES, subgroupResources); addProperties(mMockDevice, mGid2, mUid1, subgroupProperties); + System.out.println("---------- set Example Group END"); return properties; } @@ -642,7 +694,8 @@ public class GroupResourceTest { throws Exception { System.out.println("-----Get group, gid : " + gid); IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE, - GROUP_URI + "/" + gid, Constants.REQ_UUID_ID + "=" + uid); + GROUP_URI + "/" + gid, Constants.REQ_UUID_ID + "=" + uid + ";" + + Constants.KEYFIELD_GROUP_OWNER + "=" + uid); mGroupResource.onDefaultRequestReceived(device, request); } @@ -686,17 +739,20 @@ public class GroupResourceTest { throws Exception { System.out.println("-----Get group, gid : " + gid); IRequest request = MessageBuilder.createRequest(RequestMethod.GET, - GROUP_URI + "/" + gid, Constants.REQ_UUID_ID + "=" + uid); + GROUP_URI + "/" + gid, Constants.REQ_UUID_ID + "=" + uid + ";" + + Constants.KEYFIELD_GROUP_MEMBERS + "=" + uid); mGroupResource.onDefaultRequestReceived(device, request); } private void addProperties(CoapDevice device, String gid, String uid, HashMap<String, Object> properties) throws Exception { System.out.println("-----Add properties"); - properties.put(Constants.REQ_UUID_ID, uid); IRequest request = null; request = MessageBuilder.createRequest(RequestMethod.POST, - GROUP_URI + "/" + gid, "op=" + Constants.REQ_GROUP_QUERY_ADD, + GROUP_URI + "/" + gid, + Constants.KEYFIELD_UID + "=" + uid + ";" + + Constants.REQ_GROUP_QUERY_OPERATION + "=" + + Constants.REQ_GROUP_QUERY_ADD, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(properties)); mGroupResource.onDefaultRequestReceived(device, request); @@ -705,10 +761,10 @@ public class GroupResourceTest { private void updateProperties(CoapDevice device, String gid, String uid, HashMap<String, Object> properties) throws Exception { System.out.println("-----Update properties"); - properties.put(Constants.REQ_UUID_ID, uid); IRequest request = null; request = MessageBuilder.createRequest(RequestMethod.POST, - GROUP_URI + "/" + gid, null, ContentFormat.APPLICATION_CBOR, + GROUP_URI + "/" + gid, Constants.KEYFIELD_UID + "=" + uid, + ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(properties)); mGroupResource.onDefaultRequestReceived(device, request); } @@ -716,10 +772,12 @@ public class GroupResourceTest { private void deleteProperties(CoapDevice device, String gid, String uid, HashMap<String, Object> properties) throws Exception { System.out.println("-----Delete properties"); - properties.put(Constants.REQ_UUID_ID, uid); IRequest request = null; request = MessageBuilder.createRequest(RequestMethod.POST, - GROUP_URI + "/" + gid, "op=" + Constants.REQ_GROUP_QUERY_DELETE, + GROUP_URI + "/" + gid, + Constants.KEYFIELD_UID + "=" + uid + ";" + + Constants.REQ_GROUP_QUERY_OPERATION + "=" + + Constants.REQ_GROUP_QUERY_DELETE, ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(properties)); mGroupResource.onDefaultRequestReceived(device, request); @@ -729,21 +787,17 @@ public class GroupResourceTest { String parent) throws Exception { System.out.println("-----Create Group"); IRequest request = null; - request = createGroupRequest(uid, gname, parent); - mGroupResource.onDefaultRequestReceived(device, request); - } - - private IRequest createGroupRequest(String uid, String gname, - String parent) { - IRequest request = null; HashMap<String, Object> payloadData = new HashMap<String, Object>(); - payloadData.put(Constants.REQ_UUID_ID, uid); payloadData.put(Constants.KEYFIELD_GROUP_NAME, gname); payloadData.put(Constants.KEYFIELD_GROUP_PARENT, parent); + payloadData.put(Constants.KEYFIELD_GROUP_MEMBERS, + new ArrayList<String>(Arrays.asList(uid))); + payloadData.put(Constants.KEYFIELD_OID, uid); request = MessageBuilder.createRequest(RequestMethod.POST, GROUP_URI, - null, ContentFormat.APPLICATION_CBOR, + Constants.KEYFIELD_UID + "=" + uid, + ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(payloadData)); - return request; + mGroupResource.onDefaultRequestReceived(device, request); } private boolean hashmapCheck(IResponse response, String propertyName) { diff --git a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResourceTest.java b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResourceTest.java index 5005264e9..faa4cc1fd 100644 --- a/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResourceTest.java +++ b/cloud/account/src/test/java/org/iotivity/cloud/accountserver/resources/acl/invite/InviteResourceTest.java @@ -401,20 +401,16 @@ public class InviteResourceTest { String parent) throws Exception { System.out.println("-----Create Group"); IRequest request = null; - request = createGroupRequest(uid, gname, parent); - mGroupResource.onDefaultRequestReceived(device, request); - } - - private IRequest createGroupRequest(String uid, String gname, - String parent) { - IRequest request = null; HashMap<String, Object> payloadData = new HashMap<String, Object>(); - payloadData.put(Constants.REQ_UUID_ID, uid); payloadData.put(Constants.KEYFIELD_GROUP_NAME, gname); payloadData.put(Constants.KEYFIELD_GROUP_PARENT, parent); + payloadData.put(Constants.KEYFIELD_GROUP_MEMBERS, + new ArrayList<String>(Arrays.asList(uid))); + payloadData.put(Constants.KEYFIELD_OID, uid); request = MessageBuilder.createRequest(RequestMethod.POST, GROUP_URI, - null, ContentFormat.APPLICATION_CBOR, + Constants.KEYFIELD_UID + "=" + uid, + ContentFormat.APPLICATION_CBOR, mCbor.encodingPayloadToCbor(payloadData)); - return request; + mGroupResource.onDefaultRequestReceived(device, request); } }
\ No newline at end of file diff --git a/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/Resource.java b/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/Resource.java index 719070ec6..e09a31423 100644 --- a/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/Resource.java +++ b/cloud/stack/src/main/java/org/iotivity/cloud/base/resource/Resource.java @@ -99,7 +99,7 @@ public class Resource implements IRequestEventHandler { for (String property : propertyList) { if (!queryData.containsKey(property)) throw new PreconditionFailedException( - property + " property is not include"); + "query does not contatin " + property + " property"); if (queryData.get(property) == null) throw new PreconditionFailedException( property + " param is null"); @@ -121,10 +121,10 @@ public class Resource implements IRequestEventHandler { for (String property : propertyList) { if (!payloadData.containsKey(property)) throw new PreconditionFailedException( - property + " property is not include"); + "payload does not contain" + property + " property"); if (payloadData.get(property) == null) throw new PreconditionFailedException( - property + " param is null"); + property + " param is null in the payload"); } return true; } |