Merge changes I8c744a32,Id357ac37,I7df87e14,I2abd173c,Ie53b1dfd
[clamp.git] / src / test / java / org / onap / clamp / loop / LoopToJsonTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
31
32 import java.io.IOException;
33 import java.util.HashSet;
34 import java.util.Random;
35
36 import org.junit.Test;
37 import org.onap.clamp.clds.util.JsonUtils;
38 import org.onap.clamp.clds.util.ResourceFileUtil;
39 import org.onap.clamp.loop.log.LogType;
40 import org.onap.clamp.loop.log.LoopLog;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
42 import org.onap.clamp.policy.operational.OperationalPolicy;
43 import org.skyscreamer.jsonassert.JSONAssert;
44
45 public class LoopToJsonTest {
46
47     private Gson gson = new Gson();
48
49     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
50         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
51     }
52
53     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
54         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
55         Loop loop = new Loop(name, blueprint, svgRepresentation);
56         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
57         loop.setLastComputedState(LoopState.DESIGN);
58         loop.setDcaeDeploymentId(dcaeId);
59         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
60         loop.setDcaeBlueprintId(dcaeBlueprintId);
61         return loop;
62     }
63
64     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
65         String policyTosca, String jsonProperties, boolean shared) {
66         MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
67             gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
68         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
69
70         return µService;
71     }
72
73     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
74         LoopLog log = new LoopLog(message, type, loop);
75         log.setId(Long.valueOf(new Random().nextInt()));
76         return log;
77     }
78
79     @Test
80     public void LoopGsonTest() throws IOException {
81         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
82             "123456789", "https://dcaetest.org", "UUID-blueprint");
83         OperationalPolicy opPolicy = this.getOperationalPolicy(
84             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
85         loopTest.addOperationalPolicy(opPolicy);
86         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
87             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
88             "{\"param1\":\"value1\"}", true);
89         loopTest.addMicroServicePolicy(microServicePolicy);
90         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
91         loopTest.addLog(loopLog);
92
93         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
94         assertThat(jsonSerialized).isNotNull().isNotEmpty();
95         System.out.println(jsonSerialized);
96         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
97         assertNotNull(loopTestDeserialized);
98         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint");
99
100         // svg and blueprint not exposed so wont be deserialized
101         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
102         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
103
104         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
105         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
106         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
107         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
108             "loop");
109     }
110
111     @Test
112     public void createPoliciesPayloadPdpGroupTest() throws IOException {
113         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
114             "123456789", "https://dcaetest.org", "UUID-blueprint");
115         OperationalPolicy opPolicy = this.getOperationalPolicy(
116             ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
117         loopTest.addOperationalPolicy(opPolicy);
118         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
119             "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
120             "{\"param1\":\"value1\"}", true);
121         loopTest.addMicroServicePolicy(microServicePolicy);
122
123         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
124             loopTest.createPoliciesPayloadPdpGroup(), false);
125     }
126 }