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;
35 import java.io.IOException;
36 import java.util.Random;
37 import org.junit.Test;
38 import org.onap.clamp.clds.util.JsonUtils;
39 import org.onap.clamp.clds.util.ResourceFileUtil;
40 import org.onap.clamp.loop.log.LogType;
41 import org.onap.clamp.loop.log.LoopLog;
42 import org.onap.clamp.loop.service.Service;
43 import org.onap.clamp.loop.template.LoopElementModel;
44 import org.onap.clamp.loop.template.LoopTemplate;
45 import org.onap.clamp.loop.template.PolicyModel;
46 import org.onap.clamp.policy.microservice.MicroServicePolicy;
47 import org.onap.clamp.policy.operational.OperationalPolicy;
49 public class LoopToJsonTest {
51 private Gson gson = new Gson();
53 private OperationalPolicy getOperationalPolicy(String configJson, String name) {
54 return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class),
55 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1"), null, null, null);
58 private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
59 String dcaeId, String dcaeUrl, String dcaeBlueprintId)
60 throws JsonSyntaxException, IOException {
61 Loop loop = new Loop(name, svgRepresentation);
62 loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
63 loop.setLastComputedState(LoopState.DESIGN);
64 loop.setDcaeDeploymentId(dcaeId);
65 loop.setDcaeDeploymentStatusUrl(dcaeUrl);
69 private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
70 String policyTosca, String jsonProperties, boolean shared) {
71 MicroServicePolicy microService = new MicroServicePolicy(name, new PolicyModel(modelType, policyTosca, "1.0.0"),
73 gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
74 microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
78 private LoopElementModel getLoopElementModel(String yaml, String name, PolicyModel policyModel) {
79 LoopElementModel model = new LoopElementModel();
80 model.setBlueprint(yaml);
82 model.addPolicyModel(policyModel);
83 model.setLoopElementType("OPERATIONAL_POLICY");
87 private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
88 String policyVariant) {
89 return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
92 private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation,
93 Integer maxInstancesAllowed) {
94 LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
95 template.addLoopElementModel(getLoopElementModel("yaml", "microService1",
96 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1")));
100 private LoopLog getLoopLog(LogType type, String message, Loop loop) {
101 LoopLog log = new LoopLog(message, type, "CLAMP", loop);
102 log.setId(Long.valueOf(new Random().nextInt()));
107 * This tests a GSON encode/decode.
108 * @throws IOException In case of failure
111 public void loopGsonTest() throws IOException {
112 Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
113 "123456789", "https://dcaetest.org", "UUID-blueprint");
114 OperationalPolicy opPolicy = this.getOperationalPolicy(
115 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
116 loopTest.addOperationalPolicy(opPolicy);
117 MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
118 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
119 "{\"param1\":\"value1\"}", true);
120 loopTest.addMicroServicePolicy(microServicePolicy);
121 LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
122 loopTest.addLog(loopLog);
123 LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1);
124 loopTest.setLoopTemplate(loopTemplate);
126 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
127 assertThat(jsonSerialized).isNotNull().isNotEmpty();
128 System.out.println(jsonSerialized);
129 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
130 assertNotNull(loopTestDeserialized);
131 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
133 assertThat(loopTestDeserialized.getComponent("DCAE").getState())
134 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
135 assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
136 loopTest.getComponent("POLICY").getState(), "stateName", "description");
137 // svg and blueprint not exposed so wont be deserialized
138 assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
140 assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
141 assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
142 assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
143 assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
146 // Verify the loop template
147 assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate);
151 * This tests the service object GSON encode/decode.
153 * @throws IOException In case of issues
156 public void loopServiceTest() throws IOException {
157 Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
158 "123456789", "https://dcaetest.org", "UUID-blueprint");
160 JsonObject jsonModel = new GsonBuilder().create()
161 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
162 Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
163 jsonModel.get("resourceDetails").getAsJsonObject(), "1.0");
164 loopTest2.setModelService(service);
165 String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
166 assertThat(jsonSerialized).isNotNull().isNotEmpty();
167 System.out.println(jsonSerialized);
169 Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
170 assertNotNull(loopTestDeserialized);
171 assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
172 "blueprint", "components");