From 2c2fa619fdfaeb83d48fbedf6d874dc0febbee69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20D=C4=85browski?= Date: Mon, 15 Jan 2018 21:33:39 +0100 Subject: [PATCH 1/2] Enum as not complex type. --- .../scim2/common/utils/SchemaUtils.java | 20 ++++++-- .../common/schema/SchemaGenerationTest.java | 47 +++++++++++++++---- .../schema/testobjects/TestEnumObject.java | 33 +++++++++++++ .../schema/testobjects/TestObject4.java | 45 ++++++++++++++++++ 4 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestEnumObject.java create mode 100644 scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java diff --git a/scim2-sdk-common/src/main/java/com/unboundid/scim2/common/utils/SchemaUtils.java b/scim2-sdk-common/src/main/java/com/unboundid/scim2/common/utils/SchemaUtils.java index 6897eaee..1dfaf72c 100644 --- a/scim2-sdk-common/src/main/java/com/unboundid/scim2/common/utils/SchemaUtils.java +++ b/scim2-sdk-common/src/main/java/com/unboundid/scim2/common/utils/SchemaUtils.java @@ -18,6 +18,7 @@ package com.unboundid.scim2.common.utils; import com.fasterxml.jackson.annotation.JsonProperty; +import com.unboundid.scim2.common.types.AttributeDefinition.Builder; import com.unboundid.scim2.common.types.Meta; import com.unboundid.scim2.common.annotations.Schema; import com.unboundid.scim2.common.annotations.Attribute; @@ -227,7 +228,6 @@ private static Collection getAttributes( addReferenceTypes(attributeBuilder, schemaProperty); addMutability(attributeBuilder, schemaProperty); addMultiValued(attributeBuilder, propertyDescriptor, schemaProperty); - addCanonicalValues(attributeBuilder, schemaProperty); Class propertyCls = propertyDescriptor.getPropertyType(); @@ -237,6 +237,7 @@ private static Collection getAttributes( { propertyCls = schemaProperty.multiValueClass(); } + addCanonicalValues(attributeBuilder, schemaProperty, propertyCls); AttributeDefinition.Type type = getAttributeType(propertyCls); attributeBuilder.setType(type); @@ -400,15 +401,24 @@ private static AttributeDefinition.Builder addRequired( * @param attributeBuilder builder for a scim attribute. * @param schemaProperty the schema property annotation for the field * to build an attribute for. + * @param propertyCls * @return this. */ private static AttributeDefinition.Builder addCanonicalValues( - final AttributeDefinition.Builder attributeBuilder, - final Attribute schemaProperty) + final Builder attributeBuilder, + final Attribute schemaProperty, + final Class propertyCls) { if(schemaProperty != null) { - attributeBuilder.addCanonicalValues(schemaProperty.canonicalValues()); + if(propertyCls.isEnum()){ + Enum[] enumVales = (Enum[]) propertyCls.getEnumConstants(); + for (Enum e: enumVales) { + attributeBuilder.addCanonicalValues(e.name()); + } + } else { + attributeBuilder.addCanonicalValues(schemaProperty.canonicalValues()); + } } return attributeBuilder; @@ -532,7 +542,7 @@ else if ((cls == Double.class) || return AttributeDefinition.Type.DECIMAL; } else if ((cls == String.class) || - (cls == boolean.class)) + (cls == boolean.class) || cls.isEnum()) { return AttributeDefinition.Type.STRING; } diff --git a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java index 7cef05f5..e8e1ef6d 100644 --- a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java +++ b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java @@ -17,15 +17,8 @@ package com.unboundid.scim2.common.schema; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.unboundid.scim2.common.types.AttributeDefinition; -import com.unboundid.scim2.common.types.SchemaResource; -import com.unboundid.scim2.common.schema.testobjects.TestObject1; -import com.unboundid.scim2.common.schema.testobjects.TestObject2; -import com.unboundid.scim2.common.schema.testobjects.TestObject3; -import com.unboundid.scim2.common.utils.SchemaUtils; -import org.testng.Assert; -import org.testng.annotations.Test; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Lists.transform; import java.util.Arrays; import java.util.Collection; @@ -34,6 +27,20 @@ import java.util.List; import java.util.Set; +import org.testng.Assert; +import org.testng.annotations.Test; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Function; +import com.unboundid.scim2.common.schema.testobjects.TestEnumObject; +import com.unboundid.scim2.common.schema.testobjects.TestObject1; +import com.unboundid.scim2.common.schema.testobjects.TestObject2; +import com.unboundid.scim2.common.schema.testobjects.TestObject3; +import com.unboundid.scim2.common.schema.testobjects.TestObject4; +import com.unboundid.scim2.common.types.AttributeDefinition; +import com.unboundid.scim2.common.types.SchemaResource; +import com.unboundid.scim2.common.utils.SchemaUtils; + /** * Tests cases for SCIM schema generation. */ @@ -75,6 +82,28 @@ public void testCase1() throws Exception String schemaJsonString = mapper.writeValueAsString(schemaDefinition); } + /** + * Test enum as a field. + * @throws Exception in the event an error occurs. + */ + @Test + public void testEnumAsField() throws Exception { + SchemaResource schemaDefinition = SchemaUtils.getSchema(TestObject4.class); + + Collection attributes = schemaDefinition.getAttributes(); + Assert.assertNotNull(attributes); + Assert.assertEquals(attributes.size(), 1); + AttributeDefinition onlyAttribute = attributes.iterator().next(); + Assert.assertNotNull(onlyAttribute.getCanonicalValues()); + List collect = newArrayList(TestEnumObject.values()); + Assert.assertTrue(onlyAttribute.getCanonicalValues().containsAll( transform(collect, + new Function() { + public String apply(TestEnumObject input) { + return input.name(); + } + }))); + } + /** * Tests schema property annotations for some of the basic types * and attributes. diff --git a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestEnumObject.java b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestEnumObject.java new file mode 100644 index 00000000..8278365b --- /dev/null +++ b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestEnumObject.java @@ -0,0 +1,33 @@ +/* + * Copyright 2015-2018 Ping Identity Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License (GPLv2 only) + * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +package com.unboundid.scim2.common.schema.testobjects; + +public enum TestEnumObject { + /** + * Test case 1. + */ + CASE1, + /** + * Test case 2. + */ + CASE2, + /** + * Test case 3. + */ + CASE3 +} diff --git a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java new file mode 100644 index 00000000..04d8dfd2 --- /dev/null +++ b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java @@ -0,0 +1,45 @@ +/* + * Copyright 2015-2018 Ping Identity Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License (GPLv2 only) + * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +package com.unboundid.scim2.common.schema.testobjects; + +import com.unboundid.scim2.common.annotations.Attribute; +import com.unboundid.scim2.common.annotations.Schema; + +@Schema(id="urn:com.unboundid:schemas:TestObject4", + description = "description:TestObject4", name = "TestObject4") +public class TestObject4 { + + @Attribute(description = "description:value") + private TestEnumObject value; + + /** + * Getter for attribute in test class. + * @param value attribute value. + */ + public void setValue(TestEnumObject value) { + this.value = value; + } + + /** + * Getter for attribute in test class. + * @return attribute value. + */ + public TestEnumObject getValue() { + return value; + } +} From d63387183f7b9fa31736612228ef0618608f6d15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20D=C4=85browski?= Date: Tue, 16 Jan 2018 11:46:30 +0100 Subject: [PATCH 2/2] codecy review comments fix. --- .../com/unboundid/scim2/common/schema/SchemaGenerationTest.java | 2 +- .../unboundid/scim2/common/schema/testobjects/TestObject4.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java index e8e1ef6d..cd88ecf7 100644 --- a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java +++ b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/SchemaGenerationTest.java @@ -98,7 +98,7 @@ public void testEnumAsField() throws Exception { List collect = newArrayList(TestEnumObject.values()); Assert.assertTrue(onlyAttribute.getCanonicalValues().containsAll( transform(collect, new Function() { - public String apply(TestEnumObject input) { + public String apply(final TestEnumObject input) { return input.name(); } }))); diff --git a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java index 04d8dfd2..630c1789 100644 --- a/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java +++ b/scim2-sdk-common/src/test/java/com/unboundid/scim2/common/schema/testobjects/TestObject4.java @@ -31,7 +31,7 @@ public class TestObject4 { * Getter for attribute in test class. * @param value attribute value. */ - public void setValue(TestEnumObject value) { + public void setValue(final TestEnumObject value) { this.value = value; }