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