Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce additional attribute json properties option #420

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,7 @@ public abstract class AbstractAttribute implements Attribute {
protected SCIMDefinitions.Uniqueness uniqueness;
//A container to hold custom attribute properties.
protected Map<String, String> additionalAttributeProperties = new HashMap<>();
protected Map<String, JSONObject> additionalAttributeJSONProperties = new HashMap<>();

public String getURI() {
return uri; }
Expand Down Expand Up @@ -145,4 +147,24 @@ public String removeAttributeProperty(String propertyName) {

return additionalAttributeProperties.remove(propertyName);
}

public Map<String, JSONObject> 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 removeAttributeJsonProperty(String propertyName) {

return additionalAttributeJSONProperties.remove(propertyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -62,4 +63,9 @@ public default Map<String, String> getAttributeProperties() {

throw new UnsupportedOperationException();
}

public default Map<String, JSONObject> getAttributeJSONProperties() {

throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ public JSONObject encodeBasicAttributeSchema(Attribute attribute) throws JSONExc
attributeSchema.put(entry.getKey(), entry.getValue());
}

Map<String, JSONObject> customJSONProperties = attribute.getAttributeJSONProperties();
for (Map.Entry<String, JSONObject> entry: customJSONProperties.entrySet()) {
attributeSchema.put(entry.getKey(), entry.getValue());
}

return attributeSchema;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions modules/charon-core/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<class name="org.wso2.charon3.core.utils.ResourceManagerUtilTest"/>
<class name="org.wso2.charon3.core.utils.SchemaUtilTest"/>
<class name="org.wso2.charon3.core.utils.PatchOperationUtilTest"/>
<class name="org.wso2.charon3.core.encoder.JsonEncoderTest"/>
<class name="org.wso2.charon3.core.schema.ServerSideValidatorTest"/>
<class name="org.wso2.charon3.core.protocol.endpoints.UserResourceManagerTest"/>
<class name="org.wso2.charon3.core.protocol.endpoints.MeResourceManagerTest"/>
Expand Down
Loading