diff options
Diffstat (limited to 'common/src')
3 files changed, 129 insertions, 10 deletions
diff --git a/common/src/main/java/org/onap/so/serviceinstancebeans/ModelType.java b/common/src/main/java/org/onap/so/serviceinstancebeans/ModelType.java index 11cd3d42b7..34edf91c0c 100644 --- a/common/src/main/java/org/onap/so/serviceinstancebeans/ModelType.java +++ b/common/src/main/java/org/onap/so/serviceinstancebeans/ModelType.java @@ -20,18 +20,75 @@ package org.onap.so.serviceinstancebeans; +import java.lang.reflect.InvocationTargetException; +import com.google.common.base.CaseFormat; + /* * Enum for Model Type values returned by API Handler to BPMN */ public enum ModelType { - service, - vnf, - vfModule, - volumeGroup, - network, - configuration, - connectionPoint, - pnf, - networkInstanceGroup, - instanceGroup + service("serviceInstance"), + vnf("vnf"), + vfModule("vfModule"), + volumeGroup("volumeGroup"), + network("network"), + configuration("configuration"), + connectionPoint("connectionPoint"), + pnf("pnf"), + networkInstanceGroup("networkInstanceGroup"), + instanceGroup("instanceGroup"); + + final String name; + + private ModelType(String name) { + this.name = name; + } + + + public <T> T getId(Object obj) { + return this.get(obj, "Id"); + } + + public <T> T getName(Object obj) { + return this.get(obj, "Name"); + } + + public void setId(Object obj, Object value) { + this.set(obj, "Id", value); + } + + public void setName(Object obj, Object value) { + this.set(obj, "Name", value); + } + + protected <T> T get(Object obj, String field) { + T result = null; + if (obj != null) { + try { + result = (T) obj.getClass().getMethod(String.format("%s%s%s", "get", + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field)).invoke(obj); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e) { + // silent fail + } + } + + return result; + } + + protected void set(Object obj, String field, Object value) { + if (obj != null) { + try { + obj.getClass() + .getMethod( + String.format("%s%s%s", "set", + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field), + value.getClass()) + .invoke(obj, value); + } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException e) { + // silent fail + } + } + } } diff --git a/common/src/test/java/org/onap/so/serviceinstancebeans/ModelTypeTest.java b/common/src/test/java/org/onap/so/serviceinstancebeans/ModelTypeTest.java new file mode 100644 index 0000000000..e4b694cd15 --- /dev/null +++ b/common/src/test/java/org/onap/so/serviceinstancebeans/ModelTypeTest.java @@ -0,0 +1,38 @@ +package org.onap.so.serviceinstancebeans; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class ModelTypeTest { + + + + @Test + public void reflectionTest() { + TestServiceInstanceBean a = new TestServiceInstanceBean(); + TestServiceInstanceBean b = new TestServiceInstanceBean(); + + a.setServiceInstanceId("my-id-a"); + a.setServiceInstanceName("my-name-a"); + + b.setServiceInstanceId("my-id-b"); + b.setServiceInstanceName("my-name-b"); + + assertEquals("my-id-a", ModelType.service.getId(a)); + assertEquals("my-name-a", ModelType.service.getName(a)); + ModelType.service.setName(a, ModelType.service.getName(b)); + ModelType.service.setId(a, ModelType.service.getId(b)); + assertEquals("my-name-b", ModelType.service.getName(a)); + assertEquals("my-id-b", ModelType.service.getId(a)); + } + + @Test + public void testSilentFail() { + TestServiceInstanceBean a = new TestServiceInstanceBean(); + + a.setServiceInstanceId("my-id-a"); + a.setServiceInstanceName("my-name-a"); + assertNull(ModelType.service.get(a, "NoField")); + } +} diff --git a/common/src/test/java/org/onap/so/serviceinstancebeans/TestServiceInstanceBean.java b/common/src/test/java/org/onap/so/serviceinstancebeans/TestServiceInstanceBean.java new file mode 100644 index 0000000000..b8cf6141e3 --- /dev/null +++ b/common/src/test/java/org/onap/so/serviceinstancebeans/TestServiceInstanceBean.java @@ -0,0 +1,24 @@ +package org.onap.so.serviceinstancebeans; + +public class TestServiceInstanceBean { + + private String serviceInstanceId; + private String serviceInstanceName; + + public String getServiceInstanceId() { + return serviceInstanceId; + } + + public void setServiceInstanceId(String serviceInstanceId) { + this.serviceInstanceId = serviceInstanceId; + } + + public String getServiceInstanceName() { + return serviceInstanceName; + } + + public void setServiceInstanceName(String serviceInstanceName) { + this.serviceInstanceName = serviceInstanceName; + } + +} |