Create Service object
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopToJsonTest.java
index 8899a36..b68bf48 100644 (file)
@@ -5,7 +5,9 @@
  * Copyright (C) 2019 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Modifications Copyright (c) 2019 Samsung
+ * ================================================================================
+ *  Licensed 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
  *
@@ -27,7 +29,9 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertNotNull;
 
 import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
 import com.google.gson.JsonObject;
+import com.google.gson.JsonSyntaxException;
 
 import java.io.IOException;
 import java.util.HashSet;
@@ -39,6 +43,7 @@ import org.onap.clamp.clds.util.ResourceFileUtil;
 import org.onap.clamp.loop.components.external.PolicyComponent;
 import org.onap.clamp.loop.log.LogType;
 import org.onap.clamp.loop.log.LoopLog;
+import org.onap.clamp.loop.service.Service;
 import org.onap.clamp.policy.microservice.MicroServicePolicy;
 import org.onap.clamp.policy.operational.OperationalPolicy;
 import org.skyscreamer.jsonassert.JSONAssert;
@@ -52,7 +57,7 @@ public class LoopToJsonTest {
     }
 
     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
-        String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
+            String dcaeId, String dcaeUrl, String dcaeBlueprintId) throws JsonSyntaxException, IOException {
         Loop loop = new Loop(name, blueprint, svgRepresentation);
         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
         loop.setLastComputedState(LoopState.DESIGN);
@@ -63,12 +68,12 @@ public class LoopToJsonTest {
     }
 
     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
-        String policyTosca, String jsonProperties, boolean shared) {
-        MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
-            gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
-        µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
+            String policyTosca, String jsonProperties, boolean shared) {
+        MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
+                gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
+        microService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
 
-        return µService;
+        return microService;
     }
 
     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
@@ -78,15 +83,15 @@ public class LoopToJsonTest {
     }
 
     @Test
-    public void LoopGsonTest() throws IOException {
+    public void loopGsonTest() throws IOException {
         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
-            "123456789", "https://dcaetest.org", "UUID-blueprint");
+                "123456789", "https://dcaetest.org", "UUID-blueprint");
         OperationalPolicy opPolicy = this.getOperationalPolicy(
-            ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
+                ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
         loopTest.addOperationalPolicy(opPolicy);
         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
-            "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-            "{\"param1\":\"value1\"}", true);
+                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
+                "{\"param1\":\"value1\"}", true);
         loopTest.addMicroServicePolicy(microServicePolicy);
         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
         loopTest.addLog(loopLog);
@@ -97,11 +102,11 @@ public class LoopToJsonTest {
         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
         assertNotNull(loopTestDeserialized);
         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
-            "components");
+                "components");
         assertThat(loopTestDeserialized.getComponent("DCAE").getState())
-            .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
-        assertThat(loopTestDeserialized.getComponent("POLICY").getState())
-            .isEqualToComparingFieldByField(loopTest.getComponent("POLICY").getState());
+                .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
+        assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
+                loopTest.getComponent("POLICY").getState(), "stateName", "description");
         // svg and blueprint not exposed so wont be deserialized
         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
@@ -110,22 +115,45 @@ public class LoopToJsonTest {
         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
-            "loop");
+                "loop");
+    }
+
+    @Test
+    public void loopServiceTest() throws IOException {
+        Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
+                "123456789", "https://dcaetest.org", "UUID-blueprint");
+
+        JsonObject jsonModel = new GsonBuilder().create()
+                .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
+        Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
+                jsonModel.get("resourceDetails").getAsJsonObject());
+        loopTest2.setModelService(service);
+
+        String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
+        assertThat(jsonSerialized).isNotNull().isNotEmpty();
+        System.out.println(jsonSerialized);
+        JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/loop.json"),
+                jsonSerialized, true);
+
+        Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
+        assertNotNull(loopTestDeserialized);
+        assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", 
+            "svgRepresentation", "blueprint", "components");
     }
 
     @Test
     public void createPoliciesPayloadPdpGroupTest() throws IOException {
         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
-            "123456789", "https://dcaetest.org", "UUID-blueprint");
+                "123456789", "https://dcaetest.org", "UUID-blueprint");
         OperationalPolicy opPolicy = this.getOperationalPolicy(
-            ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
+                ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
         loopTest.addOperationalPolicy(opPolicy);
         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
-            "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
-            "{\"param1\":\"value1\"}", true);
+                "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
+                "{\"param1\":\"value1\"}", true);
         loopTest.addMicroServicePolicy(microServicePolicy);
 
         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
-            PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
+                PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
     }
 }