From c3fb95f45239453d8765d6e51118f49dc5f90444 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Wed, 27 Apr 2022 10:32:51 +0100 Subject: Fix Issue when serialization into JSON Schema Issue-ID: POLICY-4104 Change-Id: Iab42134820fc547429a988b43a0bd6edb344c3db Signed-off-by: FrancescoFioraEst --- .../commissioning/CommissioningProviderTest.java | 70 +++++++++++----------- .../rest/CommissioningControllerTest.java | 36 ++++------- 2 files changed, 44 insertions(+), 62 deletions(-) (limited to 'runtime-acm/src/test/java') diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java index bcdb4ddd6..71aadccb9 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/CommissioningProviderTest.java @@ -29,10 +29,9 @@ import static org.mockito.Mockito.when; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper; import java.util.List; import java.util.Map; import java.util.Objects; @@ -56,6 +55,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; class CommissioningProviderTest { private static final Coder CODER = new StandardCoder(); + private final ObjectMapper mapper = new ObjectMapper(); /** * Test the fetching of automation composition definitions (ToscaServiceTemplates). @@ -69,13 +69,13 @@ class CommissioningProviderTest { var serviceTemplateProvider = mock(ServiceTemplateProvider.class); CommissioningProvider provider = - new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); + new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); List listOfTemplates = provider.getAutomationCompositionDefinitions(null, null); assertThat(listOfTemplates).isEmpty(); when(acProvider.getFilteredNodeTemplates(any())) - .thenReturn(List.of(new ToscaNodeTemplate(), new ToscaNodeTemplate())); + .thenReturn(List.of(new ToscaNodeTemplate(), new ToscaNodeTemplate())); listOfTemplates = provider.getAutomationCompositionDefinitions(null, null); assertThat(listOfTemplates).hasSize(2); } @@ -92,7 +92,7 @@ class CommissioningProviderTest { var participantProvider = mock(ParticipantProvider.class); CommissioningProvider provider = - new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); + new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); List listOfTemplates = provider.getAutomationCompositionDefinitions(null, null); assertThat(listOfTemplates).isEmpty(); @@ -102,11 +102,11 @@ class CommissioningProviderTest { // Response should return the number of node templates present in the service template List affectedDefinitions = provider - .createAutomationCompositionDefinitions(serviceTemplate).getAffectedAutomationCompositionDefinitions(); + .createAutomationCompositionDefinitions(serviceTemplate).getAffectedAutomationCompositionDefinitions(); assertThat(affectedDefinitions).hasSize(13); when(acProvider.getFilteredNodeTemplates(any())) - .thenReturn(List.of(new ToscaNodeTemplate(), new ToscaNodeTemplate())); + .thenReturn(List.of(new ToscaNodeTemplate(), new ToscaNodeTemplate())); listOfTemplates = provider.getAutomationCompositionDefinitions(null, null); assertThat(listOfTemplates).hasSize(2); @@ -124,9 +124,8 @@ class CommissioningProviderTest { var participantProvider = mock(ParticipantProvider.class); CommissioningProvider provider = - new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); - ToscaServiceTemplate serviceTemplate = - InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); + new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); + ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); when(serviceTemplateProvider.createServiceTemplate(serviceTemplate)).thenReturn(serviceTemplate); provider.createAutomationCompositionDefinitions(serviceTemplate); @@ -138,7 +137,7 @@ class CommissioningProviderTest { assertThat(returnedServiceTemplate).isNotNull(); Map nodeTemplates = - returnedServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); + returnedServiceTemplate.getToscaTopologyTemplate().getNodeTemplates(); assertThat(nodeTemplates).hasSize(8); } @@ -155,15 +154,14 @@ class CommissioningProviderTest { var participantProvider = mock(ParticipantProvider.class); CommissioningProvider provider = - new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); - ToscaServiceTemplate serviceTemplate = - InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); + new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); + ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); when(serviceTemplateProvider.createServiceTemplate(serviceTemplate)).thenReturn(serviceTemplate); provider.createAutomationCompositionDefinitions(serviceTemplate); - when(serviceTemplateProvider.getServiceTemplateList(any(), any())).thenReturn(List.of( - Objects.requireNonNull(serviceTemplate))); + when(serviceTemplateProvider.getServiceTemplateList(any(), any())) + .thenReturn(List.of(Objects.requireNonNull(serviceTemplate))); String returnedServiceTemplate = provider.getToscaServiceTemplateReduced(null, null); assertThat(returnedServiceTemplate).isNotNull(); @@ -185,36 +183,36 @@ class CommissioningProviderTest { var participantProvider = mock(ParticipantProvider.class); CommissioningProvider provider = - new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); - ToscaServiceTemplate serviceTemplate = - InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); + new CommissioningProvider(serviceTemplateProvider, acProvider, null, participantProvider); + ToscaServiceTemplate serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); when(serviceTemplateProvider.createServiceTemplate(serviceTemplate)).thenReturn(serviceTemplate); provider.createAutomationCompositionDefinitions(serviceTemplate); provider.initialize(); - final Map> sections = Map.of("all", ToscaServiceTemplate.class, - "data_types", ToscaDataType.class, "capability_types", ToscaCapabilityType.class, - "node_types", ToscaNodeType.class, "relationship_types", - ToscaRelationshipType.class, "policy_types", ToscaPolicyType.class, "topology_template", - ToscaTopologyTemplate.class, "node_templates", List.class); + mapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); - String localServiceTemplateSchema = new String(); - Gson gson = new Gson(); - GsonBuilder builder = new GsonBuilder(); - builder.registerTypeAdapter(ToscaTopologyTemplate.class, - (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); - builder.setPrettyPrinting(); - gson = builder.create(); + Map> sections = Map.of("all", ToscaServiceTemplate.class, "data_types", ToscaDataType.class, + "capability_types", ToscaCapabilityType.class, "node_types", ToscaNodeType.class, "relationship_types", + ToscaRelationshipType.class, "policy_types", ToscaPolicyType.class, "topology_template", + ToscaTopologyTemplate.class, "node_templates", List.class); for (Map.Entry> entry : sections.entrySet()) { String returnedServiceTemplateSchema = provider.getToscaServiceTemplateSchema(entry.getKey()); assertThat(returnedServiceTemplateSchema).isNotNull(); - if (entry.getKey().equals("topology_template")) { - localServiceTemplateSchema = gson.toJson(new ToscaTopologyTemplate()); - assertThat(localServiceTemplateSchema).isEqualTo(returnedServiceTemplateSchema); + var visitor = new SchemaFactoryWrapper(); + + if (entry.getKey().equals("node_templates")) { + mapper.acceptJsonFormatVisitor( + mapper.getTypeFactory().constructCollectionType(List.class, ToscaNodeTemplate.class), visitor); + } else { + mapper.acceptJsonFormatVisitor(mapper.constructType(entry.getValue()), visitor); } + + var jsonSchema = visitor.finalSchema(); + String localServiceTemplateSchema = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema); + assertThat(localServiceTemplateSchema).isEqualTo(returnedServiceTemplateSchema); } } } diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java index f9a7b20e4..87633dfd4 100644 --- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java +++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/commissioning/rest/CommissioningControllerTest.java @@ -29,12 +29,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_SERVICE_TEMPLATE_YAML; import static org.onap.policy.clamp.acm.runtime.util.CommonTestData.TOSCA_ST_TEMPLATE_YAML; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import com.google.gson.JsonSerializer; import java.util.List; import java.util.Map; import javax.ws.rs.client.Entity; @@ -82,8 +76,7 @@ class CommissioningControllerTest extends CommonRestController { @BeforeAll public static void setUpBeforeClass() { serviceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_SERVICE_TEMPLATE_YAML); - commonPropertiesServiceTemplate = - InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); + commonPropertiesServiceTemplate = InstantiationUtils.getToscaServiceTemplate(TOSCA_ST_TEMPLATE_YAML); } @BeforeEach @@ -143,18 +136,9 @@ class CommissioningControllerTest extends CommonRestController { Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaservicetemplate"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); - String template = rawresp.readEntity(String.class); - final JsonObject jsonObject = new JsonParser().parse(template).getAsJsonObject(); - - Gson gson = new Gson(); - GsonBuilder builder = new GsonBuilder(); - builder.registerTypeAdapter(ToscaNodeTemplate.class, - (JsonSerializer) (src, typeOfSrc, context) -> new JsonPrimitive(src.toString())); - builder.setPrettyPrinting(); - gson = builder.create(); - - ToscaNodeTemplate toscaNodeTemplate = gson.fromJson(jsonObject, ToscaNodeTemplate.class); - assertNotNull(toscaNodeTemplate); + ToscaServiceTemplate template = rawresp.readEntity(ToscaServiceTemplate.class); + assertNotNull(template); + assertThat(template.getNodeTypes()).hasSize(7); } @Test @@ -162,7 +146,7 @@ class CommissioningControllerTest extends CommonRestController { createFullEntryInDbWithCommonProps(); Invocation.Builder invocationBuilder = - super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema"); + super.sendRequest(COMMISSIONING_ENDPOINT + "/toscaServiceTemplateSchema"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); String schema = rawresp.readEntity(String.class); @@ -174,7 +158,7 @@ class CommissioningControllerTest extends CommonRestController { createFullEntryInDbWithCommonProps(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT - + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0"); + + "/getCommonOrInstanceProperties" + "?common=true&name=ToscaServiceTemplateSimple&version=1.0.0"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); @@ -209,7 +193,7 @@ class CommissioningControllerTest extends CommonRestController { assertThat(commissioningResponse.getAffectedAutomationCompositionDefinitions()).hasSize(13); for (String nodeTemplateName : serviceTemplate.getToscaTopologyTemplate().getNodeTemplates().keySet()) { assertTrue(commissioningResponse.getAffectedAutomationCompositionDefinitions().stream() - .anyMatch(ac -> ac.getName().equals(nodeTemplateName))); + .anyMatch(ac -> ac.getName().equals(nodeTemplateName))); } } @@ -250,8 +234,8 @@ class CommissioningControllerTest extends CommonRestController { void testQueryElements() throws Exception { createEntryInDB(); - Invocation.Builder invocationBuilder = super.sendRequest( - COMMISSIONING_ENDPOINT + "/elements" + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition"); + Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "/elements" + + "?name=org.onap.domain.pmsh.PMSHAutomationCompositionDefinition"); Response rawresp = invocationBuilder.buildGet().invoke(); assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus()); List entityList = rawresp.readEntity(List.class); @@ -274,7 +258,7 @@ class CommissioningControllerTest extends CommonRestController { var serviceTemplateCreated = createEntryInDB(); Invocation.Builder invocationBuilder = super.sendRequest(COMMISSIONING_ENDPOINT + "?name=" - + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion()); + + serviceTemplateCreated.getName() + "&version=" + serviceTemplateCreated.getVersion()); // Call delete with no info Response resp = invocationBuilder.delete(); assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus()); -- cgit 1.2.3-korg