2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
32 import java.io.IOException;
33 import java.util.HashSet;
34 import java.util.Random;
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.components.external.PolicyComponent;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.policy.microservice.MicroServicePolicy;
43 import org.onap.clamp.policy.operational.OperationalPolicy;
44 import org.skyscreamer.jsonassert.JSONAssert;
46 public class LoopToJsonTest {
48 private Gson gson = new Gson();
50 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
51 return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
54 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
55 String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
56 Loop loop = new Loop(name, blueprint, svgRepresentation);
57 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
58 loop.setLastComputedState(LoopState.DESIGN);
59 loop.setDcaeDeploymentId(dcaeId);
60 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
61 loop.setDcaeBlueprintId(dcaeBlueprintId);
65 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
66 String policyTosca, String jsonProperties, boolean shared) {
67 MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
68 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
69 µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
74 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
75 LoopLog log = new LoopLog(message, type, "CLAMP", loop);
76 log.setId(Long.valueOf(new Random().nextInt()));
81 public void LoopGsonTest() throws IOException {
82 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
83 "123456789", "https://dcaetest.org", "UUID-blueprint");
84 OperationalPolicy opPolicy = this.getOperationalPolicy(
85 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
86 loopTest.addOperationalPolicy(opPolicy);
87 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
88 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
89 "{\"param1\":\"value1\"}", true);
90 loopTest.addMicroServicePolicy(microServicePolicy);
91 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
92 loopTest.addLog(loopLog);
94 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
95 assertThat(jsonSerialized).isNotNull().isNotEmpty();
96 System.out.println(jsonSerialized);
97 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
98 assertNotNull(loopTestDeserialized);
99 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
101 assertThat(loopTestDeserialized.getComponent("DCAE").getState())
102 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
103 assertThat(loopTestDeserialized.getComponent("POLICY").getState())
104 .isEqualToComparingFieldByField(loopTest.getComponent("POLICY").getState());
105 // svg and blueprint not exposed so wont be deserialized
106 assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
107 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
109 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
110 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
111 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
112 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
117 public void createPoliciesPayloadPdpGroupTest() throws IOException {
118 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
119 "123456789", "https://dcaetest.org", "UUID-blueprint");
120 OperationalPolicy opPolicy = this.getOperationalPolicy(
121 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
122 loopTest.addOperationalPolicy(opPolicy);
123 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
124 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
125 "{\"param1\":\"value1\"}", true);
126 loopTest.addMicroServicePolicy(microServicePolicy);
128 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
129 PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);