Create SVG in UI
[clamp.git] / src / test / java / org / onap / clamp / loop / DcaeComponentTest.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
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
30 import java.io.IOException;
31 import java.util.List;
32 import org.apache.camel.Exchange;
33 import org.apache.camel.Message;
34 import org.json.simple.parser.ParseException;
35 import org.junit.Test;
36 import org.mockito.Mockito;
37 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
38 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
39 import org.onap.clamp.loop.components.external.DcaeComponent;
40 import org.onap.clamp.loop.components.external.ExternalComponentState;
41 import org.onap.clamp.loop.template.LoopTemplate;
42 import org.onap.clamp.loop.template.PolicyModel;
43 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44
45 public class DcaeComponentTest {
46
47     private Loop createTestLoop() {
48         Loop loopTest = new Loop("ControlLoopTest");
49         loopTest.setGlobalPropertiesJson(
50                 new Gson().fromJson(
51                         "{\"dcaeDeployParameters\":{\"uniqueBlueprintParameters\": {\"policy_id\": \"name\"}}}",
52                         JsonObject.class));
53         loopTest.setLastComputedState(LoopState.DESIGN);
54         loopTest.setDcaeDeploymentId("123456789");
55         loopTest.setDcaeDeploymentStatusUrl("http4://localhost:8085");
56
57         MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", new PolicyModel("policy1",
58                 "tosca_definitions_version: tosca_simple_yaml_1_0_0", "1.0.0"), true,
59                 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), null, null, null);
60         microServicePolicy.setConfigurationsJson(new Gson().fromJson("{\"param1\":\"value1\"}", JsonObject.class));
61
62         loopTest.addMicroServicePolicy(microServicePolicy);
63         LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", 1, null);
64         loopTemplate.setDcaeBlueprintId("UUID-blueprint");
65         loopTest.setLoopTemplate(loopTemplate);
66
67         return loopTest;
68     }
69
70     /**
71      * Test the DcaeReponse roughly.
72      *
73      * @throws IOException In case of issues
74      */
75     @Test
76     public void convertDcaeResponseTest() throws IOException {
77         String dcaeFakeResponse = "{'requestId':'testId','operationType':'install','status':'state',"
78                 + "'error':'errorMessage', 'links':{'self':'selfUrl','uninstall':'uninstallUrl'}}";
79         DcaeOperationStatusResponse responseObject = DcaeComponent.convertDcaeResponse(dcaeFakeResponse);
80         assertThat(responseObject.getRequestId()).isEqualTo("testId");
81         assertThat(responseObject.getOperationType()).isEqualTo("install");
82         assertThat(responseObject.getStatus()).isEqualTo("state");
83         assertThat(responseObject.getError()).isEqualTo("errorMessage");
84         assertThat(responseObject.getLinks()).isNotNull();
85         assertThat(responseObject.getLinks().getSelf()).isEqualTo("selfUrl");
86         assertThat(responseObject.getLinks().getUninstall()).isEqualTo("uninstallUrl");
87
88         assertThat(responseObject.getLinks().getStatus()).isNull();
89     }
90
91     @Test
92     public void testGetDeployPayload() throws IOException {
93         Loop loop = this.createTestLoop();
94         String deploymentPayload = DcaeComponent.getDeployPayload(loop);
95         String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\",\"inputs\":{\"policy_id\":\"name\"}}";
96         assertThat(deploymentPayload).isEqualTo(expectedPayload);
97     }
98
99     @Test
100     public void testGetUndeployPayload() throws IOException {
101         Loop loop = this.createTestLoop();
102         String unDeploymentPayload = DcaeComponent.getUndeployPayload(loop);
103         String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\"}";
104         assertThat(unDeploymentPayload).isEqualTo(expectedPayload);
105     }
106
107     /**
108      * Test the computeState method of the DcaeComponent roughly.
109      *
110      * @throws IOException In case of issues
111      */
112     @Test
113     public void computeStateTest() throws IOException {
114         Exchange exchange = Mockito.mock(Exchange.class);
115         Message message = Mockito.mock(Message.class);
116         Exchange exchange2 = Mockito.mock(Exchange.class);
117         Mockito.when(exchange.getIn()).thenReturn(message);
118         Mockito.when(message.getExchange()).thenReturn(exchange2);
119         Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(null);
120
121         DcaeComponent dcae = new DcaeComponent();
122
123         // initial state
124         ExternalComponentState state = dcae.computeState(exchange);
125         assertThat(state.getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
126
127         // OperationalType = install
128         DcaeOperationStatusResponse dcaeResponse = Mockito.mock(DcaeOperationStatusResponse.class);
129         Mockito.when(dcaeResponse.getOperationType()).thenReturn("install");
130
131         Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded");
132         Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse);
133         ExternalComponentState state2 = dcae.computeState(exchange);
134         assertThat(state2.getStateName()).isEqualTo("MICROSERVICE_INSTALLED_SUCCESSFULLY");
135         Mockito.when(dcaeResponse.getStatus()).thenReturn("processing");
136         ExternalComponentState state3 = dcae.computeState(exchange);
137         assertThat(state3.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_INSTALLATION");
138
139         Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
140         ExternalComponentState state4 = dcae.computeState(exchange);
141         assertThat(state4.getStateName()).isEqualTo("MICROSERVICE_INSTALLATION_FAILED");
142
143         // OperationalType = uninstall
144         Mockito.when(dcaeResponse.getOperationType()).thenReturn("uninstall");
145
146         Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded");
147         Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse);
148         ExternalComponentState state5 = dcae.computeState(exchange);
149         assertThat(state5.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLED_SUCCESSFULLY");
150
151         Mockito.when(dcaeResponse.getStatus()).thenReturn("processing");
152         ExternalComponentState state6 = dcae.computeState(exchange);
153         assertThat(state6.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_UNINSTALLATION");
154
155         Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
156         ExternalComponentState state7 = dcae.computeState(exchange);
157         assertThat(state7.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLATION_FAILED");
158
159         // error cases
160         Mockito.when(dcaeResponse.getOperationType()).thenReturn("whatever");
161         ExternalComponentState state8 = dcae.computeState(exchange);
162         assertThat(state8.getStateName()).isEqualTo("IN_ERROR");
163
164         Mockito.when(dcaeResponse.getOperationType()).thenReturn("install");
165         Mockito.when(dcaeResponse.getStatus()).thenReturn("anythingelse");
166         ExternalComponentState state9 = dcae.computeState(exchange);
167         assertThat(state9.getStateName()).isEqualTo("IN_ERROR");
168     }
169
170     /**
171      * Test the Converter to DcaeInventoryResponse method.
172      *
173      * @throws IOException    In case of failure
174      * @throws ParseException In case of failure
175      */
176     @Test
177     public void convertToDcaeInventoryResponseTest() throws IOException, ParseException {
178         String dcaeFakeResponse = "{\n" + "  \"links\": {\n" + "    \"previousLink\": {\n"
179                 + "      \"title\": \"string\",\n" + "      \"rel\": \"string\",\n" + "      \"uri\": \"string\",\n"
180                 + "      \"uriBuilder\": {},\n" + "      \"rels\": [\n" + "        \"string\"\n" + "      ],\n"
181                 + "      \"params\": {\n" + "        \"additionalProp1\": \"string\",\n"
182                 + "        \"additionalProp2\": \"string\",\n" + "        \"additionalProp3\": \"string\"\n"
183                 + "      },\n" + "      \"type\": \"string\"\n" + "    },\n" + "    \"nextLink\": {\n"
184                 + "      \"title\": \"string\",\n" + "      \"rel\": \"string\",\n" + "      \"uri\": \"string\",\n"
185                 + "      \"uriBuilder\": {},\n" + "      \"rels\": [\n" + "        \"string\"\n" + "      ],\n"
186                 + "      \"params\": {\n" + "        \"additionalProp1\": \"string\",\n"
187                 + "        \"additionalProp2\": \"string\",\n" + "        \"additionalProp3\": \"string\"\n"
188                 + "      },\n" + "      \"type\": \"string\"\n" + "    }\n" + "  },\n" + "  \"totalCount\": 0,\n"
189                 + "  \"items\": [\n" + "    {\n" + "      \"owner\": \"testOwner\",\n"
190                 + "      \"application\": \"testApplication\",\n" + "      \"component\": \"testComponent\",\n"
191                 + "      \"typeName\": \"testTypeName\",\n" + "      \"typeVersion\": 0,\n"
192                 + "      \"blueprintTemplate\": \"testBlueprintTemplate\",\n" + "      \"serviceIds\": [\n"
193                 + "        \"serviceId1\", \"serviceId2\"\n" + "      ],\n" + "      \"vnfTypes\": [\n"
194                 + "        \"vnfType1\", \"vnfType2\"\n" + "      ],\n" + "      \"serviceLocations\": [\n"
195                 + "        \"serviceLocation1\", \"serviceLocation2\"\n" + "      ],\n"
196                 + "      \"asdcServiceId\": \"testAsdcServiceId\",\n"
197                 + "      \"asdcResourceId\": \"testAsdcResourceId\",\n"
198                 + "      \"asdcServiceURL\": \"testAsdcServiceURL\",\n" + "      \"typeId\": \"testTypeId\",\n"
199                 + "      \"selfLink\": {\n" + "        \"title\": \"selfLinkTitle\",\n"
200                 + "        \"rel\": \"selfLinkRel\",\n" + "        \"uri\": \"selfLinkUri\",\n"
201                 + "        \"uriBuilder\": {},\n" + "        \"rels\": [\n" + "          \"string\"\n" + "        ],\n"
202                 + "        \"params\": {\n" + "          \"additionalProp1\": \"string\",\n"
203                 + "          \"additionalProp2\": \"string\",\n" + "          \"additionalProp3\": \"string\"\n"
204                 + "        },\n" + "        \"type\": \"string\"\n" + "      },\n"
205                 + "      \"created\": \"2020-01-22T09:38:15.436Z\",\n"
206                 + "      \"deactivated\": \"2020-01-22T09:38:15.437Z\"\n" + "    }\n" + "  ]\n" + "}";
207         List<DcaeInventoryResponse> responseObject = DcaeComponent.convertToDcaeInventoryResponse(dcaeFakeResponse);
208         assertThat(responseObject.get(0).getAsdcResourceId()).isEqualTo("testAsdcResourceId");
209         assertThat(responseObject.get(0).getAsdcServiceId()).isEqualTo("testAsdcServiceId");
210         assertThat(responseObject.get(0).getTypeName()).isEqualTo("testTypeName");
211         assertThat(responseObject.get(0).getTypeId()).isEqualTo("testTypeId");
212         assertThat(responseObject.get(0).getBlueprintTemplate()).isEqualTo("testBlueprintTemplate");
213     }
214 }