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.GsonBuilder;
33 import com.google.gson.JsonObject;
34 import com.google.gson.JsonSyntaxException;
36 import java.io.IOException;
37 import java.util.HashSet;
38 import java.util.Random;
40 import org.junit.Test;
41 import org.onap.clamp.clds.util.JsonUtils;
42 import org.onap.clamp.clds.util.ResourceFileUtil;
43 import org.onap.clamp.loop.components.external.PolicyComponent;
44 import org.onap.clamp.loop.log.LogType;
45 import org.onap.clamp.loop.log.LoopLog;
46 import org.onap.clamp.loop.service.Service;
47 import org.onap.clamp.loop.template.LoopElementModel;
48 import org.onap.clamp.loop.template.LoopTemplate;
49 import org.onap.clamp.loop.template.PolicyModel;
50 import org.onap.clamp.policy.microservice.MicroServicePolicy;
51 import org.onap.clamp.policy.operational.OperationalPolicy;
52 import org.skyscreamer.jsonassert.JSONAssert;
54 public class LoopToJsonTest {
56 private Gson gson = new Gson();
58 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
59 return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
62 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
63 String dcaeId, String dcaeUrl, String dcaeBlueprintId) throws JsonSyntaxException, IOException {
64 Loop loop = new Loop(name, svgRepresentation);
65 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
66 loop.setLastComputedState(LoopState.DESIGN);
67 loop.setDcaeDeploymentId(dcaeId);
68 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
72 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
73 String policyTosca, String jsonProperties, boolean shared) {
74 MicroServicePolicy microService = new MicroServicePolicy(name, modelType, policyTosca, shared,
75 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
76 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
80 private LoopElementModel getLoopElementModel(String yaml, String name, PolicyModel policyModel) {
81 LoopElementModel model = new LoopElementModel();
82 model.setBlueprint(yaml);
84 model.addPolicyModel(policyModel);
85 model.setLoopElementType("OPERATIONAL_POLICY");
89 private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
90 String policyVariant) {
91 return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
94 private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation,
95 Integer maxInstancesAllowed) {
96 LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
97 template.addLoopElementModel(getLoopElementModel("yaml", "microService1",
98 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1")));
102 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
103 LoopLog log = new LoopLog(message, type, "CLAMP", loop);
104 log.setId(Long.valueOf(new Random().nextInt()));
109 public void loopGsonTest() throws IOException {
110 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
111 "123456789", "https://dcaetest.org", "UUID-blueprint");
112 OperationalPolicy opPolicy = this.getOperationalPolicy(
113 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
114 loopTest.addOperationalPolicy(opPolicy);
115 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
116 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
117 "{\"param1\":\"value1\"}", true);
118 loopTest.addMicroServicePolicy(microServicePolicy);
119 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
120 loopTest.addLog(loopLog);
121 LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1);
122 loopTest.setLoopTemplate(loopTemplate);
124 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
125 assertThat(jsonSerialized).isNotNull().isNotEmpty();
126 System.out.println(jsonSerialized);
127 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
128 assertNotNull(loopTestDeserialized);
129 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
131 assertThat(loopTestDeserialized.getComponent("DCAE").getState())
132 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
133 assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
134 loopTest.getComponent("POLICY").getState(), "stateName", "description");
135 // svg and blueprint not exposed so wont be deserialized
136 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
138 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
139 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
140 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
141 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
144 // Verify the loop template
145 assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate);
149 public void loopServiceTest() throws IOException {
150 Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
151 "123456789", "https://dcaetest.org", "UUID-blueprint");
153 JsonObject jsonModel = new GsonBuilder().create()
154 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
155 Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
156 jsonModel.get("resourceDetails").getAsJsonObject(), "1.0");
157 loopTest2.setModelService(service);
158 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
159 assertThat(jsonSerialized).isNotNull().isNotEmpty();
160 System.out.println(jsonSerialized);
162 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
163 assertNotNull(loopTestDeserialized);
164 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
165 "blueprint", "components");
169 public void createPoliciesPayloadPdpGroupTest() throws IOException {
170 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
171 "123456789", "https://dcaetest.org", "UUID-blueprint");
172 OperationalPolicy opPolicy = this.getOperationalPolicy(
173 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
174 loopTest.addOperationalPolicy(opPolicy);
175 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
176 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
177 "{\"param1\":\"value1\"}", true);
178 loopTest.addMicroServicePolicy(microServicePolicy);
180 JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
181 PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);