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