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