From 7ea6f109558e5a93021bb7838627f93cc18c5608 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] Enum as not complex type. --- .../scim2/common/utils/SchemaUtils.java | 20 ++++++-- .../common/schema/SchemaGenerationTest.java | 48 +++++++++++++++---- .../schema/testobjects/TestEnumObject.java | 33 +++++++++++++ .../schema/testobjects/TestObject4.java | 45 +++++++++++++++++ 4 files changed, 132 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..af5530b5 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,16 +17,10 @@ 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.beans.IntrospectionException; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -34,6 +28,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 +83,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..bbec8f72 --- /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..a3013ddc --- /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. + * @return attribute value. + */ + public void setValue(TestEnumObject value) { + this.value = value; + } + + /** + * Getter for attribute in test class. + * @return attribute value. + */ + public TestEnumObject getValue() { + return value; + } +}