From 194332355ecd1ca94c9b5de312da7d3ed2f85931 Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Sun, 19 Jan 2025 11:45:41 +0530 Subject: [PATCH 1/6] Introduce additional attribute json properties option --- .../core/attributes/AbstractAttribute.java | 17 +++++++++++++++++ .../wso2/charon3/core/attributes/Attribute.java | 6 ++++++ .../wso2/charon3/core/encoder/JSONEncoder.java | 5 +++++ 3 files changed, 28 insertions(+) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java index 5f4274f85..962f58a5d 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java @@ -15,6 +15,7 @@ */ package org.wso2.charon3.core.attributes; +import org.json.JSONObject; import org.wso2.charon3.core.schema.SCIMDefinitions; import java.util.HashMap; @@ -48,6 +49,7 @@ public abstract class AbstractAttribute implements Attribute { protected SCIMDefinitions.Uniqueness uniqueness; //A container to hold custom attribute properties. protected Map additionalAttributeProperties = new HashMap<>(); + protected Map additionalAttributeJSONProperties = new HashMap<>(); public String getURI() { return uri; } @@ -145,4 +147,19 @@ public String removeAttributeProperty(String propertyName) { return additionalAttributeProperties.remove(propertyName); } + + public Map getAttributeJSONProperties() { + + return additionalAttributeJSONProperties; + } + + public void addAttributeJsonProperty(String propertyName, JSONObject jsonObject) { + + this.additionalAttributeJSONProperties.put(propertyName, jsonObject); + } + + public JSONObject getAttributeJsonProperty(String propertyName) { + + return additionalAttributeJSONProperties.remove(propertyName); + } } diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java index 43acf9e04..1aeb0b7f1 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java @@ -15,6 +15,7 @@ */ package org.wso2.charon3.core.attributes; +import org.json.JSONObject; import org.wso2.charon3.core.exceptions.CharonException; import org.wso2.charon3.core.schema.SCIMDefinitions; @@ -62,4 +63,9 @@ public default Map getAttributeProperties() { throw new UnsupportedOperationException(); } + + public default Map getAttributeJSONProperties() { + + throw new UnsupportedOperationException(); + } } diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/encoder/JSONEncoder.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/encoder/JSONEncoder.java index 338712a1f..a410aabec 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/encoder/JSONEncoder.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/encoder/JSONEncoder.java @@ -233,6 +233,11 @@ public JSONObject encodeBasicAttributeSchema(Attribute attribute) throws JSONExc attributeSchema.put(entry.getKey(), entry.getValue()); } + Map customJSONProperties = attribute.getAttributeJSONProperties(); + for (Map.Entry entry: customJSONProperties.entrySet()) { + attributeSchema.put(entry.getKey(), entry.getValue()); + } + return attributeSchema; } From 6ed1177eb5c409ffa6b9d024c1955b645910ddb5 Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Sun, 19 Jan 2025 12:25:27 +0530 Subject: [PATCH 2/6] Add remove method --- .../wso2/charon3/core/attributes/AbstractAttribute.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java index 962f58a5d..eb6287ad7 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java @@ -153,12 +153,17 @@ public Map getAttributeJSONProperties() { return additionalAttributeJSONProperties; } + public JSONObject getAttributeJsonProperty(String propertyName) { + + return additionalAttributeJSONProperties.get(propertyName); + } + public void addAttributeJsonProperty(String propertyName, JSONObject jsonObject) { this.additionalAttributeJSONProperties.put(propertyName, jsonObject); } - public JSONObject getAttributeJsonProperty(String propertyName) { + public JSONObject removeAttributeJsonProperty(String propertyName) { return additionalAttributeJSONProperties.remove(propertyName); } From f0ff81815a5854e738ee27a2d106b00cec715f47 Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Mon, 20 Jan 2025 00:45:48 +0530 Subject: [PATCH 3/6] Add unit tests --- .../charon3/core/encoder/JsonEncoderTest.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java diff --git a/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java b/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java new file mode 100644 index 000000000..faf590263 --- /dev/null +++ b/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). + * + * WSO2 LLC. 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.wso2.charon3.core.encoder; + +import org.json.JSONObject; +import org.testng.Assert; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.wso2.charon3.core.attributes.AbstractAttribute; +import org.wso2.charon3.core.attributes.SimpleAttribute; +import org.wso2.charon3.core.schema.SCIMDefinitions; + +/** + * Test class for JSONEncoder. + */ +public class JsonEncoderTest { + + private JSONEncoder jsonEncoder; + + @BeforeMethod + public void setUp() { + + jsonEncoder = new JSONEncoder(); + } + + @Test + public void testEncodeBasicAttributeSchema() { + + AbstractAttribute attribute = new SimpleAttribute("test", null); + attribute.setType(SCIMDefinitions.DataType.STRING); + attribute.setMultiValued(false); + attribute.setDescription("Test Description"); + attribute.setRequired(true); + attribute.setCaseExact(true); + attribute.setReturned(SCIMDefinitions.Returned.DEFAULT); + attribute.setMutability(SCIMDefinitions.Mutability.READ_WRITE); + attribute.setURI("testURI"); + attribute.setUniqueness(SCIMDefinitions.Uniqueness.NONE); + attribute.addAttributeProperty("custom1", "value1"); + + JSONObject testJsonObject = new JSONObject(); + testJsonObject.put("key1", "value1"); + attribute.addAttributeJsonProperty("json1", testJsonObject); + attribute.addAttributeJsonProperty("json2", new JSONObject()); + attribute.removeAttributeJsonProperty("json2"); + + JSONObject responseJson = jsonEncoder.encodeBasicAttributeSchema(attribute); + + // Assert. + Assert.assertEquals(responseJson.get("name"), "test"); + Assert.assertEquals(responseJson.get("type"), SCIMDefinitions.DataType.STRING); + Assert.assertEquals(responseJson.get("multiValued"), false); + Assert.assertEquals(responseJson.get("description"), "Test Description"); + Assert.assertEquals(responseJson.get("required"), true); + Assert.assertEquals(responseJson.get("caseExact"), true); + Assert.assertEquals(responseJson.get("mutability"), SCIMDefinitions.Mutability.READ_WRITE); + Assert.assertEquals(responseJson.get("returned"), SCIMDefinitions.Returned.DEFAULT); + Assert.assertEquals(responseJson.get("uniqueness"), SCIMDefinitions.Uniqueness.NONE); + + Assert.assertEquals(responseJson.get("custom1"), "value1"); + + JSONObject customJson = responseJson.getJSONObject("json1"); + Assert.assertEquals(customJson.get("key1"), "value1"); + + Assert.assertTrue(attribute.getAttributeJSONProperties().containsKey("json1")); + Assert.assertFalse(attribute.getAttributeJSONProperties().containsKey("json2")); + Assert.assertEquals(attribute.getAttributeJsonProperty("json1"), testJsonObject); + } +} From 231f1928d479e63369e51966dcca68988bac46f3 Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Mon, 20 Jan 2025 09:10:05 +0530 Subject: [PATCH 4/6] Add unit test to testng --- modules/charon-core/src/test/resources/testng.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/charon-core/src/test/resources/testng.xml b/modules/charon-core/src/test/resources/testng.xml index 704ca559d..a7a87babd 100644 --- a/modules/charon-core/src/test/resources/testng.xml +++ b/modules/charon-core/src/test/resources/testng.xml @@ -26,6 +26,7 @@ + From aa912b8076dc71f9476f288885acee6275a5d19c Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Mon, 20 Jan 2025 12:19:56 +0530 Subject: [PATCH 5/6] Rename Json to JSON --- .../wso2/charon3/core/attributes/AbstractAttribute.java | 6 +++--- .../org/wso2/charon3/core/encoder/JsonEncoderTest.java | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java index eb6287ad7..3eb292a00 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/AbstractAttribute.java @@ -153,17 +153,17 @@ public Map getAttributeJSONProperties() { return additionalAttributeJSONProperties; } - public JSONObject getAttributeJsonProperty(String propertyName) { + public JSONObject getAttributeJSONProperty(String propertyName) { return additionalAttributeJSONProperties.get(propertyName); } - public void addAttributeJsonProperty(String propertyName, JSONObject jsonObject) { + public void addAttributeJSONProperty(String propertyName, JSONObject jsonObject) { this.additionalAttributeJSONProperties.put(propertyName, jsonObject); } - public JSONObject removeAttributeJsonProperty(String propertyName) { + public JSONObject removeAttributeJSONProperty(String propertyName) { return additionalAttributeJSONProperties.remove(propertyName); } diff --git a/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java b/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java index faf590263..183277c3f 100644 --- a/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java +++ b/modules/charon-core/src/test/java/org/wso2/charon3/core/encoder/JsonEncoderTest.java @@ -56,9 +56,9 @@ public void testEncodeBasicAttributeSchema() { JSONObject testJsonObject = new JSONObject(); testJsonObject.put("key1", "value1"); - attribute.addAttributeJsonProperty("json1", testJsonObject); - attribute.addAttributeJsonProperty("json2", new JSONObject()); - attribute.removeAttributeJsonProperty("json2"); + attribute.addAttributeJSONProperty("json1", testJsonObject); + attribute.addAttributeJSONProperty("json2", new JSONObject()); + attribute.removeAttributeJSONProperty("json2"); JSONObject responseJson = jsonEncoder.encodeBasicAttributeSchema(attribute); @@ -80,6 +80,6 @@ public void testEncodeBasicAttributeSchema() { Assert.assertTrue(attribute.getAttributeJSONProperties().containsKey("json1")); Assert.assertFalse(attribute.getAttributeJSONProperties().containsKey("json2")); - Assert.assertEquals(attribute.getAttributeJsonProperty("json1"), testJsonObject); + Assert.assertEquals(attribute.getAttributeJSONProperty("json1"), testJsonObject); } } From 4d1aa7b314fe5eef7942dbd4de8078a69f8b940d Mon Sep 17 00:00:00 2001 From: Pasindu Yeshan Date: Mon, 20 Jan 2025 12:34:50 +0530 Subject: [PATCH 6/6] Add getAttributeJSONProperty method --- .../java/org/wso2/charon3/core/attributes/Attribute.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java index 1aeb0b7f1..709f41134 100644 --- a/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java +++ b/modules/charon-core/src/main/java/org/wso2/charon3/core/attributes/Attribute.java @@ -68,4 +68,9 @@ public default Map getAttributeJSONProperties() { throw new UnsupportedOperationException(); } + + public default JSONObject getAttributeJSONProperty(String propertyName) { + + throw new UnsupportedOperationException(); + } }