2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop;
26 import static org.assertj.core.api.Assertions.assertThat;
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;
45 public class DcaeComponentTest {
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");
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));
60 loopTest.addMicroServicePolicy(microServicePolicy);
61 LoopTemplate loopTemplate = new LoopTemplate("test", "yaml", "svg", 1, null);
62 loopTemplate.setDcaeBlueprintId("UUID-blueprint");
63 loopTest.setLoopTemplate(loopTemplate);
69 * Test the DcaeReponse roughly.
70 * @throws IOException In case of issues
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");
85 assertThat(responseObject.getLinks().getStatus()).isNull();
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);
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);
105 * Test the computeState method of the DcaeComponent roughly.
107 * @throws IOException In case of issues
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);
118 DcaeComponent dcae = new DcaeComponent();
121 ExternalComponentState state = dcae.computeState(exchange);
122 assertThat(state.getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
124 // OperationalType = install
125 DcaeOperationStatusResponse dcaeResponse = Mockito.mock(DcaeOperationStatusResponse.class);
126 Mockito.when(dcaeResponse.getOperationType()).thenReturn("install");
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");
136 Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
137 ExternalComponentState state4 = dcae.computeState(exchange);
138 assertThat(state4.getStateName()).isEqualTo("MICROSERVICE_INSTALLATION_FAILED");
140 // OperationalType = uninstall
141 Mockito.when(dcaeResponse.getOperationType()).thenReturn("uninstall");
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");
148 Mockito.when(dcaeResponse.getStatus()).thenReturn("processing");
149 ExternalComponentState state6 = dcae.computeState(exchange);
150 assertThat(state6.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_UNINSTALLATION");
152 Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
153 ExternalComponentState state7 = dcae.computeState(exchange);
154 assertThat(state7.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLATION_FAILED");
157 Mockito.when(dcaeResponse.getOperationType()).thenReturn("whatever");
158 ExternalComponentState state8 = dcae.computeState(exchange);
159 assertThat(state8.getStateName()).isEqualTo("IN_ERROR");
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");
168 * Test the Converter to DcaeInventoryResponse method.
169 * @throws IOException In case of failure
170 * @throws ParseException In case of failure
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");