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