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;
31 import java.io.IOException;
32 import java.util.HashSet;
34 import org.apache.camel.Exchange;
35 import org.apache.camel.Message;
36 import org.junit.Test;
37 import org.mockito.Mockito;
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.policy.microservice.MicroServicePolicy;
43 public class DcaeComponentTest {
45 private Loop createTestLoop() {
46 String yaml = "imports:\n" + " - \"http://www.getcloudify.org/spec/cloudify/3.4/types.yaml\"\n"
47 + "node_templates:\n" + " docker_service_host:\n" + " type: dcae.nodes.SelectedDockerHost";
49 Loop loopTest = new Loop("ControlLoopTest", yaml, "<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");
55 loopTest.setDcaeBlueprintId("UUID-blueprint");
57 MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", "",
58 "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
59 new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), new HashSet<>());
60 microServicePolicy.setProperties(new Gson().fromJson("{\"param1\":\"value1\"}", JsonObject.class));
62 loopTest.addMicroServicePolicy(microServicePolicy);
67 public void convertDcaeResponseTest() throws IOException {
68 String dcaeFakeResponse = "{'requestId':'testId','operationType':'install','status':'state','error':'errorMessage', 'links':{'self':'selfUrl','uninstall':'uninstallUrl'}}";
69 DcaeOperationStatusResponse responseObject = DcaeComponent.convertDcaeResponse(dcaeFakeResponse);
70 assertThat(responseObject.getRequestId()).isEqualTo("testId");
71 assertThat(responseObject.getOperationType()).isEqualTo("install");
72 assertThat(responseObject.getStatus()).isEqualTo("state");
73 assertThat(responseObject.getError()).isEqualTo("errorMessage");
74 assertThat(responseObject.getLinks()).isNotNull();
75 assertThat(responseObject.getLinks().getSelf()).isEqualTo("selfUrl");
76 assertThat(responseObject.getLinks().getUninstall()).isEqualTo("uninstallUrl");
78 assertThat(responseObject.getLinks().getStatus()).isNull();
82 public void testGetDeployPayload() throws IOException {
83 Loop loop = this.createTestLoop();
84 String deploymentPayload = DcaeComponent.getDeployPayload(loop);
85 String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\",\"inputs\":{\"policy_id\":\"name\"}}";
86 assertThat(deploymentPayload).isEqualTo(expectedPayload);
90 public void testGetUndeployPayload() throws IOException {
91 Loop loop = this.createTestLoop();
92 String unDeploymentPayload = DcaeComponent.getUndeployPayload(loop);
93 String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\"}";
94 assertThat(unDeploymentPayload).isEqualTo(expectedPayload);
98 public void computeStateTest() throws IOException {
99 Exchange exchange = Mockito.mock(Exchange.class);
100 Message message = Mockito.mock(Message.class);
101 Exchange exchange2 = Mockito.mock(Exchange.class);
102 Mockito.when(exchange.getIn()).thenReturn(message);
103 Mockito.when(message.getExchange()).thenReturn(exchange2);
104 Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(null);
106 DcaeComponent dcae = new DcaeComponent();
109 ExternalComponentState state = dcae.computeState(exchange);
110 assertThat(state.getStateName()).isEqualTo("BLUEPRINT_DEPLOYED");
112 // OperationalType = install
113 DcaeOperationStatusResponse dcaeResponse = Mockito.mock(DcaeOperationStatusResponse.class);
114 Mockito.when(dcaeResponse.getOperationType()).thenReturn("install");
116 Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded");
117 Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse);
118 ExternalComponentState state2 = dcae.computeState(exchange);
119 assertThat(state2.getStateName()).isEqualTo("MICROSERVICE_INSTALLED_SUCCESSFULLY");
120 Mockito.when(dcaeResponse.getStatus()).thenReturn("processing");
121 ExternalComponentState state3 = dcae.computeState(exchange);
122 assertThat(state3.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_INSTALLATION");
124 Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
125 ExternalComponentState state4 = dcae.computeState(exchange);
126 assertThat(state4.getStateName()).isEqualTo("MICROSERVICE_INSTALLATION_FAILED");
128 // OperationalType = uninstall
129 Mockito.when(dcaeResponse.getOperationType()).thenReturn("uninstall");
131 Mockito.when(dcaeResponse.getStatus()).thenReturn("succeeded");
132 Mockito.when(exchange2.getProperty("dcaeResponse")).thenReturn(dcaeResponse);
133 ExternalComponentState state5 = dcae.computeState(exchange);
134 assertThat(state5.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLED_SUCCESSFULLY");
136 Mockito.when(dcaeResponse.getStatus()).thenReturn("processing");
137 ExternalComponentState state6 = dcae.computeState(exchange);
138 assertThat(state6.getStateName()).isEqualTo("PROCESSING_MICROSERVICE_UNINSTALLATION");
140 Mockito.when(dcaeResponse.getStatus()).thenReturn("failed");
141 ExternalComponentState state7 = dcae.computeState(exchange);
142 assertThat(state7.getStateName()).isEqualTo("MICROSERVICE_UNINSTALLATION_FAILED");
145 Mockito.when(dcaeResponse.getOperationType()).thenReturn("whatever");
146 ExternalComponentState state8 = dcae.computeState(exchange);
147 assertThat(state8.getStateName()).isEqualTo("IN_ERROR");
149 Mockito.when(dcaeResponse.getOperationType()).thenReturn("install");
150 Mockito.when(dcaeResponse.getStatus()).thenReturn("anythingelse");
151 ExternalComponentState state9 = dcae.computeState(exchange);
152 assertThat(state9.getStateName()).isEqualTo("IN_ERROR");