6c7836e50ef1ad82142d3ce64ab1bec1be9a0687
[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  * 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
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
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  * ===================================================================
23  *
24  */
25
26 package org.onap.clamp.loop;
27
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.junit.Assert.assertNotNull;
30
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.components.external.PolicyComponent;
41 import org.onap.clamp.loop.log.LogType;
42 import org.onap.clamp.loop.log.LoopLog;
43 import org.onap.clamp.loop.service.Service;
44 import org.onap.clamp.loop.template.LoopElementModel;
45 import org.onap.clamp.loop.template.LoopTemplate;
46 import org.onap.clamp.loop.template.PolicyModel;
47 import org.onap.clamp.policy.microservice.MicroServicePolicy;
48 import org.onap.clamp.policy.operational.OperationalPolicy;
49 import org.skyscreamer.jsonassert.JSONAssert;
50
51 public class LoopToJsonTest {
52
53     private Gson gson = new Gson();
54
55     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
56         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class),
57                 getPolicyModel("org.onap.policy.drools.legacy", "yaml", "1.0.0", "Drools", "type1"), null,null,null);
58     }
59
60     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
61                          String dcaeId, String dcaeUrl, String dcaeBlueprintId)
62             throws JsonSyntaxException, IOException {
63         Loop loop = new Loop(name, svgRepresentation);
64         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
65         loop.setLastComputedState(LoopState.DESIGN);
66         loop.setDcaeDeploymentId(dcaeId);
67         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
68         return loop;
69     }
70
71     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
72                                                      String policyTosca, String jsonProperties, boolean shared) {
73         MicroServicePolicy microService = new MicroServicePolicy(name, new PolicyModel(modelType, policyTosca, "1.0.0"),
74                 shared,
75                 gson.fromJson(jsonRepresentation, JsonObject.class), null, null, null);
76         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
77         return microService;
78     }
79
80     private LoopElementModel getLoopElementModel(String yaml, String name, PolicyModel policyModel) {
81         LoopElementModel model = new LoopElementModel();
82         model.setBlueprint(yaml);
83         model.setName(name);
84         model.addPolicyModel(policyModel);
85         model.setLoopElementType("OPERATIONAL_POLICY");
86         return model;
87     }
88
89     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
90                                        String policyVariant) {
91         return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
92     }
93
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")));
99         return template;
100     }
101
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()));
105         return log;
106     }
107
108     /**
109      * This tests a GSON encode/decode.
110      * @throws IOException In case of failure
111      */
112     @Test
113     public void loopGsonTest() throws IOException {
114         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
115                 "123456789", "https://dcaetest.org", "UUID-blueprint");
116         OperationalPolicy opPolicy = this.getOperationalPolicy(
117                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
118         loopTest.addOperationalPolicy(opPolicy);
119         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
120                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
121                 "{\"param1\":\"value1\"}", true);
122         loopTest.addMicroServicePolicy(microServicePolicy);
123         LoopLog loopLog = getLoopLog(LogType.INFO, "test message", loopTest);
124         loopTest.addLog(loopLog);
125         LoopTemplate loopTemplate = getLoopTemplate("templateName", "yaml", "svg", 1);
126         loopTest.setLoopTemplate(loopTemplate);
127
128         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
129         assertThat(jsonSerialized).isNotNull().isNotEmpty();
130         System.out.println(jsonSerialized);
131         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
132         assertNotNull(loopTestDeserialized);
133         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint",
134                 "components");
135         assertThat(loopTestDeserialized.getComponent("DCAE").getState())
136                 .isEqualToComparingFieldByField(loopTest.getComponent("DCAE").getState());
137         assertThat(loopTestDeserialized.getComponent("POLICY").getState()).isEqualToComparingOnlyGivenFields(
138                 loopTest.getComponent("POLICY").getState(), "stateName", "description");
139         // svg and blueprint not exposed so wont be deserialized
140         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
141
142         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
143         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
144         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
145         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
146                 "loop");
147
148         // Verify the loop template
149         assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate);
150     }
151
152     /**
153      * This tests the service object GSON encode/decode.
154      *
155      * @throws IOException In case of issues
156      */
157     @Test
158     public void loopServiceTest() throws IOException {
159         Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
160                 "123456789", "https://dcaetest.org", "UUID-blueprint");
161
162         JsonObject jsonModel = new GsonBuilder().create()
163                 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
164         Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
165                 jsonModel.get("resourceDetails").getAsJsonObject(), "1.0");
166         loopTest2.setModelService(service);
167         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
168         assertThat(jsonSerialized).isNotNull().isNotEmpty();
169         System.out.println(jsonSerialized);
170
171         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
172         assertNotNull(loopTestDeserialized);
173         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
174                 "blueprint", "components");
175     }
176 }