From fb090a2e01008e71d1319c04f012f1f7dcd76a88 Mon Sep 17 00:00:00 2001 From: Brian Demers Date: Mon, 29 Jul 2024 17:53:50 -0400 Subject: [PATCH] Add tests for 404 when updating SCIM resources Adds tests and updates example implementations --- .../scim/compliance/tests/GroupsIT.java | 35 +++++++++++++++++++ .../scim/compliance/tests/UsersIT.java | 33 +++++++++++++++++ .../jersey/service/InMemoryGroupService.java | 26 +++++++++----- .../jersey/service/InMemoryUserService.java | 13 +++++-- .../memory/service/InMemoryGroupService.java | 13 +++++-- .../memory/service/InMemoryUserService.java | 13 +++++-- .../quarkus/service/InMemoryGroupService.java | 13 +++++-- .../quarkus/service/InMemoryUserService.java | 13 +++++-- .../spring/service/InMemoryGroupService.java | 26 ++++++++++---- .../spring/service/InMemoryUserService.java | 15 ++++++-- .../it/testapp/InMemoryGroupService.java | 13 +++++-- .../it/testapp/InMemoryUserService.java | 13 +++++-- .../exception/ResourceNotFoundException.java | 35 +++++++++++++++++++ .../spring/it/app/InMemoryGroupService.java | 13 +++++-- .../spring/it/app/InMemoryUserService.java | 13 +++++-- 15 files changed, 253 insertions(+), 34 deletions(-) create mode 100644 scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/exception/ResourceNotFoundException.java diff --git a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/GroupsIT.java b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/GroupsIT.java index 6fae054a8..8be1c6b2c 100644 --- a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/GroupsIT.java +++ b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/GroupsIT.java @@ -213,6 +213,41 @@ public void updateGroupWithPatch() { ); } + @Test + @DisplayName("Update Invalid Group") + void updateInvalidGroup() { + + String id = randomName("group-invalidGroup-id"); + String groupName = randomName("group-invalidGroup"); + + String updatedBody = "{" + + "\"schemas\": [\"urn:ietf:params:scim:schemas:core:2.0:Group\"]," + + "\"displayName\": \"" + groupName + "\"," + + "\"members\": []}"; + + put("/Groups/" + id, updatedBody) + .statusCode(404) + .body( + "schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"), + "status", is("404"), + "detail", is("Resource " + id + " not found.") + ); + } + + @Test + @DisplayName("Delete Invalid Group") + void deleteInvalidGroup() { + String id = randomName("group-invalidGroup-id"); + + delete("/Groups/" + id) + .statusCode(404) + .body( + "schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"), + "status", is("404"), + "detail", is("Resource " + id + " not found.") + ); + } + String createUser(String name, String email) { String body = "{" + "\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"]," + diff --git a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/UsersIT.java b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/UsersIT.java index 414181d79..54e7723c4 100644 --- a/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/UsersIT.java +++ b/scim-compliance-tests/src/main/java/org/apache/directory/scim/compliance/tests/UsersIT.java @@ -248,4 +248,37 @@ public void deactivateWithPatch() { "active", is(false) ); } + + @Test + @DisplayName("Update Invalid User") + void updateInvalidUser() { + String id = randomName("user-invalidGroup-id"); + + String updatedBody = "{" + + "\"schemas\":[\"urn:ietf:params:scim:schemas:core:2.0:User\"]," + + "\"active\":true" + + "}"; + + put("/Users/" + id, updatedBody) + .statusCode(404) + .body( + "schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"), + "status", is("404"), + "detail", is("Resource " + id + " not found.") + ); + } + + @Test + @DisplayName("Delete Invalid Users") + void deleteInvalidUser() { + String id = randomName("user-invalidGroup-id"); + + delete("/Users/" + id) + .statusCode(404) + .body( + "schemas", contains("urn:ietf:params:scim:api:messages:2.0:Error"), + "status", is("404"), + "detail", is("Resource " + id + " not found.") + ); + } } diff --git a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryGroupService.java b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryGroupService.java index 6d248ab13..3d50e105d 100644 --- a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryGroupService.java +++ b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryGroupService.java @@ -20,17 +20,21 @@ package org.apache.directory.scim.example.jersey.service; import jakarta.annotation.PostConstruct; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; +import jakarta.inject.Named; import jakarta.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; import org.apache.directory.scim.core.repository.ETag; import org.apache.directory.scim.core.repository.PatchHandler; -import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.core.repository.Repository; +import org.apache.directory.scim.core.schema.SchemaRegistry; +import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; +import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; -import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.PageRequest; import org.apache.directory.scim.spec.filter.SortRequest; import org.apache.directory.scim.spec.filter.attribute.AttributeReference; @@ -46,10 +50,6 @@ import java.util.UUID; import java.util.stream.Collectors; -import jakarta.enterprise.context.ApplicationScoped; -import jakarta.inject.Named; -import org.apache.directory.scim.core.schema.SchemaRegistry; - @Named @ApplicationScoped public class InMemoryGroupService implements Repository { @@ -106,12 +106,20 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -123,8 +131,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryUserService.java b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryUserService.java index 715bc1db1..120cbb26c 100644 --- a/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryUserService.java +++ b/scim-server-examples/scim-server-jersey/src/main/java/org/apache/directory/scim/example/jersey/service/InMemoryUserService.java @@ -31,6 +31,7 @@ import org.apache.directory.scim.example.jersey.extensions.LuckyNumberExtension; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.extension.EnterpriseExtension; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -166,8 +173,10 @@ public ScimUser get(String id) { * @see Repository#delete(java.lang.String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /** diff --git a/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryGroupService.java b/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryGroupService.java index 6feb58e84..a1ed1c56f 100644 --- a/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryGroupService.java +++ b/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryGroupService.java @@ -28,6 +28,7 @@ import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.core.repository.Repository; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; import org.apache.directory.scim.spec.filter.Filter; @@ -106,12 +107,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -123,8 +130,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryUserService.java b/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryUserService.java index 6e3ba0006..3404e64c1 100644 --- a/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryUserService.java +++ b/scim-server-examples/scim-server-memory/src/main/java/org/apache/directory/scim/example/memory/service/InMemoryUserService.java @@ -31,6 +31,7 @@ import org.apache.directory.scim.example.memory.extensions.LuckyNumberExtension; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.extension.EnterpriseExtension; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -165,8 +172,10 @@ public ScimUser get(String id) { * @see Repository#delete(java.lang.String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /** diff --git a/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryGroupService.java b/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryGroupService.java index 79b4c2d93..fc45081da 100644 --- a/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryGroupService.java +++ b/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryGroupService.java @@ -31,6 +31,7 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; @@ -105,12 +106,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -122,8 +129,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryUserService.java b/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryUserService.java index 42d626b9b..5f66b4086 100644 --- a/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryUserService.java +++ b/scim-server-examples/scim-server-quarkus/src/main/java/org/apache/directory/scim/example/quarkus/service/InMemoryUserService.java @@ -31,6 +31,7 @@ import org.apache.directory.scim.example.quarkus.extensions.LuckyNumberExtension; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.extension.EnterpriseExtension; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; @@ -142,12 +143,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -165,8 +172,10 @@ public ScimUser get(String id) { * @see Repository#delete(java.lang.String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /** diff --git a/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryGroupService.java b/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryGroupService.java index df4b827e9..0053f0a36 100644 --- a/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryGroupService.java +++ b/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryGroupService.java @@ -27,11 +27,18 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; -import org.apache.directory.scim.spec.filter.*; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; +import org.apache.directory.scim.spec.filter.Filter; +import org.apache.directory.scim.spec.filter.FilterExpressions; +import org.apache.directory.scim.spec.filter.FilterResponse; +import org.apache.directory.scim.spec.filter.PageRequest; +import org.apache.directory.scim.spec.filter.SortRequest; import org.apache.directory.scim.spec.filter.attribute.AttributeReference; import org.apache.directory.scim.spec.patch.PatchOperation; import org.apache.directory.scim.spec.resources.ScimExtension; import org.apache.directory.scim.spec.resources.ScimGroup; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; import java.util.Collections; import java.util.HashMap; @@ -41,9 +48,6 @@ import java.util.UUID; import java.util.stream.Collectors; -import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; - @Service public class InMemoryGroupService implements Repository { @@ -96,12 +100,20 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -113,8 +125,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceNotFoundException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryUserService.java b/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryUserService.java index 77c9f581d..c6beeb8d5 100644 --- a/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryUserService.java +++ b/scim-server-examples/scim-server-spring-boot/src/main/java/org/apache/directory/scim/example/spring/service/InMemoryUserService.java @@ -28,6 +28,7 @@ import org.apache.directory.scim.example.spring.extensions.LuckyNumberExtension; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.extension.EnterpriseExtension; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; @@ -136,12 +137,20 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } + ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -159,8 +168,10 @@ public ScimUser get(String id) { * @see Repository#delete(java.lang.String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /** diff --git a/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryGroupService.java b/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryGroupService.java index 2d73deed9..99f4cba9b 100644 --- a/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryGroupService.java +++ b/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryGroupService.java @@ -31,6 +31,7 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; @@ -105,12 +106,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -122,8 +129,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryUserService.java b/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryUserService.java index f8ac2cc46..03ca89e11 100644 --- a/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryUserService.java +++ b/scim-server/src/test/java/org/apache/directory/scim/server/it/testapp/InMemoryUserService.java @@ -30,6 +30,7 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; @@ -136,12 +137,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -159,8 +166,10 @@ public ScimUser get(String id) { * @see Repository#delete(String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /** diff --git a/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/exception/ResourceNotFoundException.java b/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/exception/ResourceNotFoundException.java new file mode 100644 index 000000000..ef66647dc --- /dev/null +++ b/scim-spec/scim-spec-schema/src/main/java/org/apache/directory/scim/spec/exception/ResourceNotFoundException.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.directory.scim.spec.exception; + +public class ResourceNotFoundException extends ResourceException { + + public ResourceNotFoundException(String resourceId) { + super(404, message(resourceId)); + } + + public ResourceNotFoundException(String resourceId, Throwable cause) { + super(404, message(resourceId), cause); + } + + private static String message(String resourceId) { + return "Resource " +resourceId + " not found."; + } +} diff --git a/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryGroupService.java b/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryGroupService.java index ff7676526..3ecd0991a 100644 --- a/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryGroupService.java +++ b/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryGroupService.java @@ -28,6 +28,7 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; @@ -99,12 +100,18 @@ public ScimGroup create(ScimGroup resource) throws UnableToCreateResourceExcepti @Override public ScimGroup update(String id, Set etags, ScimGroup resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } groups.put(id, resource); return resource; } @Override public ScimGroup patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!groups.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimGroup resource = patchHandler.apply(get(id), patchOperations); groups.put(id, resource); return resource; @@ -116,8 +123,10 @@ public ScimGroup get(String id) { } @Override - public void delete(String id) { - groups.remove(id); + public void delete(String id) throws ResourceException { + if (groups.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } @Override diff --git a/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryUserService.java b/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryUserService.java index 69a2aea2d..fd90a3f29 100644 --- a/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryUserService.java +++ b/support/spring-boot/src/test/java/org/apache/directory/scim/spring/it/app/InMemoryUserService.java @@ -27,6 +27,7 @@ import org.apache.directory.scim.core.schema.SchemaRegistry; import org.apache.directory.scim.server.exception.UnableToCreateResourceException; import org.apache.directory.scim.spec.exception.ResourceException; +import org.apache.directory.scim.spec.exception.ResourceNotFoundException; import org.apache.directory.scim.spec.filter.Filter; import org.apache.directory.scim.spec.filter.FilterExpressions; import org.apache.directory.scim.spec.filter.FilterResponse; @@ -134,12 +135,18 @@ public ScimUser create(ScimUser resource) throws UnableToCreateResourceException @Override public ScimUser update(String id, Set etags, ScimUser resource, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } users.put(id, resource); return resource; } @Override public ScimUser patch(String id, Set etags, List patchOperations, Set includedAttributeReferences, Set excludedAttributeReferences) throws ResourceException { + if (!users.containsKey(id)) { + throw new ResourceNotFoundException(id); + } ScimUser resource = patchHandler.apply(get(id), patchOperations); users.put(id, resource); return resource; @@ -157,8 +164,10 @@ public ScimUser get(String id) { * @see Repository#delete(String) */ @Override - public void delete(String id) { - users.remove(id); + public void delete(String id) throws ResourceException { + if (users.remove(id) == null) { + throw new ResourceNotFoundException(id); + } } /**