Operational policy modification
[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.HashSet;
37 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.loop.service.Service;
45 import org.onap.clamp.loop.template.LoopElementModel;
46 import org.onap.clamp.loop.template.LoopTemplate;
47 import org.onap.clamp.loop.template.PolicyModel;
48 import org.onap.clamp.policy.microservice.MicroServicePolicy;
49 import org.onap.clamp.policy.operational.OperationalPolicy;
50 import org.skyscreamer.jsonassert.JSONAssert;
51
52 public class LoopToJsonTest {
53
54     private Gson gson = new Gson();
55
56     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
57         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class),
58                 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1"));
59     }
60
61     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
62                          String dcaeId, String dcaeUrl, String dcaeBlueprintId)
63             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);
69         return loop;
70     }
71
72     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation,
73                                                      String policyTosca, String jsonProperties, boolean shared) {
74         MicroServicePolicy microService = new MicroServicePolicy(name, new PolicyModel(modelType, policyTosca, "1.0.0"),
75                 shared,
76                 gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>());
77         microService.setConfigurationsJson(new Gson().fromJson(jsonProperties, JsonObject.class));
78         return microService;
79     }
80
81     private LoopElementModel getLoopElementModel(String yaml, String name, PolicyModel policyModel) {
82         LoopElementModel model = new LoopElementModel();
83         model.setBlueprint(yaml);
84         model.setName(name);
85         model.addPolicyModel(policyModel);
86         model.setLoopElementType("OPERATIONAL_POLICY");
87         return model;
88     }
89
90     private PolicyModel getPolicyModel(String policyType, String policyModelTosca, String version, String policyAcronym,
91                                        String policyVariant) {
92         return new PolicyModel(policyType, policyModelTosca, version, policyAcronym);
93     }
94
95     private LoopTemplate getLoopTemplate(String name, String blueprint, String svgRepresentation,
96                                          Integer maxInstancesAllowed) {
97         LoopTemplate template = new LoopTemplate(name, blueprint, svgRepresentation, maxInstancesAllowed, null);
98         template.addLoopElementModel(getLoopElementModel("yaml", "microService1",
99                 getPolicyModel("org.onap.policy.drools", "yaml", "1.0.0", "Drools", "type1")));
100         return template;
101     }
102
103     private LoopLog getLoopLog(LogType type, String message, Loop loop) {
104         LoopLog log = new LoopLog(message, type, "CLAMP", loop);
105         log.setId(Long.valueOf(new Random().nextInt()));
106         return log;
107     }
108
109     @Test
110     public void loopGsonTest() throws IOException {
111         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
112                 "123456789", "https://dcaetest.org", "UUID-blueprint");
113         OperationalPolicy opPolicy = this.getOperationalPolicy(
114                 ResourceFileUtil.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", "svg", 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         // svg and blueprint not exposed so wont be deserialized
137         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
138
139         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
140         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
141         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
142         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
143                 "loop");
144
145         // Verify the loop template
146         assertThat(loopTestDeserialized.getLoopTemplate()).isEqualTo(loopTemplate);
147     }
148
149     @Test
150     public void loopServiceTest() throws IOException {
151         Loop loopTest2 = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
152                 "123456789", "https://dcaetest.org", "UUID-blueprint");
153
154         JsonObject jsonModel = new GsonBuilder().create()
155                 .fromJson(ResourceFileUtil.getResourceAsString("tosca/model-properties.json"), JsonObject.class);
156         Service service = new Service(jsonModel.get("serviceDetails").getAsJsonObject(),
157                 jsonModel.get("resourceDetails").getAsJsonObject(), "1.0");
158         loopTest2.setModelService(service);
159         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest2);
160         assertThat(jsonSerialized).isNotNull().isNotEmpty();
161         System.out.println(jsonSerialized);
162
163         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
164         assertNotNull(loopTestDeserialized);
165         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest2, "modelService", "svgRepresentation",
166                 "blueprint", "components");
167     }
168
169     @Test
170     public void createPoliciesPayloadPdpGroupTest() throws IOException {
171         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
172                 "123456789", "https://dcaetest.org", "UUID-blueprint");
173         OperationalPolicy opPolicy = this.getOperationalPolicy(
174                 ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), "GuardOpPolicyTest");
175         loopTest.addOperationalPolicy(opPolicy);
176         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "",
177                 "{\"configtype\":\"json\"}", "tosca_definitions_version: tosca_simple_yaml_1_0_0",
178                 "{\"param1\":\"value1\"}", true);
179         loopTest.addMicroServicePolicy(microServicePolicy);
180
181         JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/pdp-group-policy-payload.json"),
182                 PolicyComponent.createPoliciesPayloadPdpGroup(loopTest), false);
183     }
184 }