Rework the submit operation
[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  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.junit.Assert.assertNotNull;
28
29 import com.google.gson.Gson;
30 import com.google.gson.JsonObject;
31
32 import java.util.HashSet;
33 import java.util.Random;
34
35 import org.junit.Test;
36 import org.onap.clamp.loop.log.LogType;
37 import org.onap.clamp.loop.log.LoopLog;
38 import org.onap.clamp.policy.microservice.MicroServicePolicy;
39 import org.onap.clamp.policy.operational.OperationalPolicy;
40 import org.onap.clamp.clds.util.JsonUtils;
41
42 public class LoopToJsonTest {
43
44     private Gson gson = new Gson();
45
46     private OperationalPolicy getOperationalPolicy(String configJson, String name) {
47         return new OperationalPolicy(name, null, gson.fromJson(configJson, JsonObject.class));
48     }
49
50     private Loop getLoop(String name, String svgRepresentation, String blueprint, String globalPropertiesJson,
51         String dcaeId, String dcaeUrl, String dcaeBlueprintId) {
52         Loop loop = new Loop(name, blueprint, svgRepresentation);
53         loop.setGlobalPropertiesJson(new Gson().fromJson(globalPropertiesJson, JsonObject.class));
54         loop.setLastComputedState(LoopState.DESIGN);
55         loop.setDcaeDeploymentId(dcaeId);
56         loop.setDcaeDeploymentStatusUrl(dcaeUrl);
57         loop.setDcaeBlueprintId(dcaeBlueprintId);
58         return loop;
59     }
60
61     private MicroServicePolicy getMicroServicePolicy(String name, String modelType, String jsonRepresentation, String policyTosca,
62         String jsonProperties, boolean shared) {
63         MicroServicePolicy µService = new MicroServicePolicy(name, modelType, policyTosca, shared,
64             gson.fromJson(jsonRepresentation, JsonObject.class), new HashSet<>(), "");
65         µService.setProperties(new Gson().fromJson(jsonProperties, JsonObject.class));
66
67         return µService;
68     }
69
70     private LoopLog getLoopLog(LogType type, String message) {
71         LoopLog log = new LoopLog();
72         log.setLogType(type);
73         log.setMessage(message);
74         log.setId(Long.valueOf(new Random().nextInt()));
75         return log;
76     }
77
78     @Test
79     public void LoopGsonTest() {
80         Loop loopTest = getLoop("ControlLoopTest", "<xml></xml>", "yamlcontent", "{\"testname\":\"testvalue\"}",
81             "123456789", "https://dcaetest.org", "UUID-blueprint");
82         OperationalPolicy opPolicy = this.getOperationalPolicy("{\"type\":\"GUARD\"}", "GuardOpPolicyTest");
83         loopTest.addOperationalPolicy(opPolicy);
84         MicroServicePolicy microServicePolicy = getMicroServicePolicy("configPolicyTest", "", "{\"configtype\":\"json\"}",
85             "YamlContent", "{\"param1\":\"value1\"}", true);
86         loopTest.addMicroServicePolicy(microServicePolicy);
87         LoopLog loopLog = getLoopLog(LogType.INFO, "test message");
88         loopTest.addLog(loopLog);
89
90         String jsonSerialized = JsonUtils.GSON_JPA_MODEL.toJson(loopTest);
91         assertThat(jsonSerialized).isNotNull().isNotEmpty();
92         System.out.println(jsonSerialized);
93         Loop loopTestDeserialized = JsonUtils.GSON_JPA_MODEL.fromJson(jsonSerialized, Loop.class);
94         assertNotNull(loopTestDeserialized);
95         assertThat(loopTestDeserialized).isEqualToIgnoringGivenFields(loopTest, "svgRepresentation", "blueprint");
96
97         //svg and blueprint not exposed so wont be deserialized
98         assertThat(loopTestDeserialized.getBlueprint()).isEqualTo(null);
99         assertThat(loopTestDeserialized.getSvgRepresentation()).isEqualTo(null);
100
101         assertThat(loopTestDeserialized.getOperationalPolicies()).containsExactly(opPolicy);
102         assertThat(loopTestDeserialized.getMicroServicePolicies()).containsExactly(microServicePolicy);
103         assertThat(loopTestDeserialized.getLoopLogs()).containsExactly(loopLog);
104         assertThat((LoopLog) loopTestDeserialized.getLoopLogs().toArray()[0]).isEqualToIgnoringGivenFields(loopLog,
105             "loop");
106     }
107 }