From fc5cd57ddb61e2917024495488de3480c68d6c42 Mon Sep 17 00:00:00 2001 From: David Saro <147051848+davidsarosap@users.noreply.github.com> Date: Thu, 5 Oct 2023 15:30:49 +0200 Subject: [PATCH 1/2] Create a test for PATCH --- .../java/com/sap/scimono/api/UsersTest.java | 69 ++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/scimono-server/src/test/java/com/sap/scimono/api/UsersTest.java b/scimono-server/src/test/java/com/sap/scimono/api/UsersTest.java index f9ec6ccd..7537c2de 100644 --- a/scimono-server/src/test/java/com/sap/scimono/api/UsersTest.java +++ b/scimono-server/src/test/java/com/sap/scimono/api/UsersTest.java @@ -1,20 +1,52 @@ package com.sap.scimono.api; -import com.sap.scimono.SCIMApplication; -import com.sap.scimono.exception.InvalidInputException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; -import java.util.UUID; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.sap.scimono.SCIMApplication; +import com.sap.scimono.callback.schemas.SchemasCallback; +import com.sap.scimono.callback.users.UsersCallback; +import com.sap.scimono.entity.patch.PatchBody; +import com.sap.scimono.entity.patch.PatchOperation; +import com.sap.scimono.exception.InvalidInputException; public class UsersTest { private Users users; + private ObjectMapper mapper; + private SchemasCallback schemasCallbackMock = Mockito.mock(SchemasCallback.class, Mockito.CALLS_REAL_METHODS); + private UsersCallback usersCallbackMock = Mockito.mock(UsersCallback.class, Mockito.CALLS_REAL_METHODS); + + ArgumentCaptor userIdCaptor = ArgumentCaptor.forClass(String.class); + ArgumentCaptor patchBodyCaptor = ArgumentCaptor.forClass(PatchBody.class); + private final String PATCH_OP_SCHEMA = "urn:ietf:params:scim:api:messages:2.0:PatchOp"; + @Before public void setup() { + mapper = new ObjectMapper(); SCIMApplication scimApplication = new SCIMApplication() { + @Override + public SchemasCallback getSchemasCallback() { + return schemasCallbackMock; + } + + @Override + public UsersCallback getUsersCallback() { + return usersCallbackMock; + } }; users = new Users(scimApplication, null); } @@ -31,4 +63,35 @@ public void testPatchUserWithEmptyBody() { users.patchUser(userId, null); } + @Test + public void testPatchUserActivate() throws JsonProcessingException { + Mockito.doNothing().when(usersCallbackMock).patchUser(Mockito.any(), Mockito.any(), Mockito.any()); + Mockito.doReturn(new ArrayList<>()).when(schemasCallbackMock).getCustomSchemas(); + String userId = String.valueOf(UUID.randomUUID()); + Set schemas = new HashSet<>(); + schemas.add(PATCH_OP_SCHEMA); + + + JsonNode valueTrue = getValueTrue(); + PatchOperation patchOperation1 = new PatchOperation.Builder() + .setOp(PatchOperation.Type.ADD) + .setPath("active") + .setValue(valueTrue) + .build(); + PatchBody patchBody = new PatchBody.Builder() + .addOperation(patchOperation1) + .setSchemas(schemas) + .build(); + users.patchUser(userId, patchBody); + + Mockito.verify(usersCallbackMock).patchUser(userIdCaptor.capture(), patchBodyCaptor.capture(), Mockito.any()); + Assert.assertEquals(userId, userIdCaptor.getValue()); + Assert.assertEquals(patchBody, patchBodyCaptor.getValue()); + } + + private JsonNode getValueTrue() throws JsonProcessingException { + JsonNode boolValue = mapper.readTree("true"); + return boolValue; + } + } From e70594c98c2d8986cfaab7709f3904ddae5ffc36 Mon Sep 17 00:00:00 2001 From: David Saro <147051848+davidsarosap@users.noreply.github.com> Date: Thu, 5 Oct 2023 15:33:07 +0200 Subject: [PATCH 2/2] Fixing isCustomSchema(final String schemaId) --- .../java/com/sap/scimono/callback/schemas/SchemasCallback.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scimono-server/src/main/java/com/sap/scimono/callback/schemas/SchemasCallback.java b/scimono-server/src/main/java/com/sap/scimono/callback/schemas/SchemasCallback.java index 36d44ce9..ee73ee9a 100644 --- a/scimono-server/src/main/java/com/sap/scimono/callback/schemas/SchemasCallback.java +++ b/scimono-server/src/main/java/com/sap/scimono/callback/schemas/SchemasCallback.java @@ -193,7 +193,7 @@ default String removeValueFilterFromAttributeNotation(final String fullAttribute } static boolean isCustomSchema(final String schemaId) { - return schemaId.matches(SCHEMA_PATTERN.toString()); + return !isCoreSchema(schemaId) && schemaId.matches(SCHEMA_PATTERN.toString()); } static boolean isCoreSchema(final String schemaId) {