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