ce13d98dd15e7d132aa9ffa250216852969ec843
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / cloudify / beans / DeploymentInfoBuilderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SO
4  * ================================================================================
5  * Copyright (C) 2018 Nokia.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.cloudify.beans;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23
24 import com.google.common.collect.ImmutableMap;
25 import org.junit.Test;
26 import org.onap.so.cloudify.v3.model.Execution;
27
28 public class DeploymentInfoBuilderTest {
29
30     private static final String ERROR_MESSAGE = "something went wrong";
31     private static final String INSTALL_WORKFLOW_ID = "install";
32     private static final String UNINSTALL_WORKFLOW_ID = "uninstall";
33
34     @Test
35     public void shouldConstructDeploymentInfo_withBasicValues() {
36         DeploymentInfo deploymentInfo = new DeploymentInfoBuilder()
37             .withId("id")
38             .withStatus(DeploymentStatus.CREATED)
39             .withDeploymentOutputs(ImmutableMap.of())
40             .withDeploymentInputs(ImmutableMap.of())
41             .withActionStatus("started")
42             .withLastAction(INSTALL_WORKFLOW_ID)
43             .withErrorMessage(ERROR_MESSAGE)
44             .build();
45
46         assertThat(deploymentInfo.getId()).isEqualTo("id");
47         assertThat(deploymentInfo.getStatus()).isEqualTo(DeploymentStatus.CREATED);
48         assertThat(deploymentInfo.getOutputs()).isEqualTo(ImmutableMap.of());
49         assertThat(deploymentInfo.getInputs()).isEqualTo(ImmutableMap.of());
50         assertThat(deploymentInfo.getActionStatus()).isEqualTo("started");
51         assertThat(deploymentInfo.getLastAction()).isEqualTo(INSTALL_WORKFLOW_ID);
52         assertThat(deploymentInfo.getErrorMessage()).isEqualTo(ERROR_MESSAGE);
53     }
54
55     @Test
56     public void shouldConstructDeploymentInfo_withCreateDeploymentStatus_fromNullExecution() {
57         DeploymentInfo deploymentInfo = new DeploymentInfoBuilder()
58             .fromExecution(null)
59             .build();
60
61         assertThat(deploymentInfo.getStatus()).isEqualTo(DeploymentStatus.CREATED);
62     }
63
64     @Test
65     public void shouldConstructDeploymentInfo_withInstalledDeploymentStatus_fromTerminatedExecution() {
66         String workflowIdLastAction = INSTALL_WORKFLOW_ID;
67         String status = "terminated";
68         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLED;
69         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
70     }
71
72     @Test
73     public void shouldConstructDeploymentInfo_withFailedDeploymentStatus_fromFailedInstallExecution() {
74         String workflowIdLastAction = INSTALL_WORKFLOW_ID;
75         String status = "failed";
76         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.FAILED;
77         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
78     }
79
80     @Test
81     public void shouldConstructDeploymentInfo_withInstallingDeploymentStatus_fromStartedExecution() {
82         String workflowIdLastAction = INSTALL_WORKFLOW_ID;
83         String status = "started";
84         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLING;
85         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
86     }
87
88     @Test
89     public void shouldConstructDeploymentInfo_withInstallingDeploymentStatus_fromPendingExecution() {
90         String workflowIdLastAction = INSTALL_WORKFLOW_ID;
91         String status = "pending";
92         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.INSTALLING;
93         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
94     }
95
96     @Test
97     public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_fromUnmappableExecution() {
98         String workflowIdLastAction = INSTALL_WORKFLOW_ID;
99         String status = "strangeStatus";
100         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN;
101         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
102     }
103
104     @Test
105     public void shouldConstructDeploymentInfo_withCreatedDeploymentStatus_fromTerminatedExecution() {
106         String workflowIdLastAction = UNINSTALL_WORKFLOW_ID;
107         String status = "terminated";
108         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.CREATED;
109         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
110     }
111
112     @Test
113     public void shouldConstructDeploymentInfo_withFailedDeploymentStatus_fromFailedUninstallExecution() {
114         String workflowIdLastAction = UNINSTALL_WORKFLOW_ID;
115         String status = "failed";
116         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.FAILED;
117         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
118     }
119
120     @Test
121     public void shouldConstructDeploymentInfo_withUninstallingDeploymentStatus_fromStartedUninstallExecution() {
122         String workflowIdLastAction = UNINSTALL_WORKFLOW_ID;
123         String status = "started";
124         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNINSTALLING;
125         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
126     }
127
128     @Test
129     public void shouldConstructDeploymentInfo_withUninstallingDeploymentStatus_fromPendingUninstallExecution() {
130         String workflowIdLastAction = UNINSTALL_WORKFLOW_ID;
131         String status = "pending";
132         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNINSTALLING;
133         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
134     }
135
136     @Test
137     public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_fromUnmappableUninstallExecution() {
138         String workflowIdLastAction = UNINSTALL_WORKFLOW_ID;
139         String status = "strangeStatus";
140         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN;
141         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
142     }
143
144     @Test
145     public void shouldConstructDeploymentInfo_withUnknownDeploymentStatus_forUnknownExecutionWorkflowId() {
146         String workflowIdLastAction = "strangeWorkflowIdLastAction";
147         String status = "strangeStatus";
148         DeploymentStatus expectedDeploymentStatus = DeploymentStatus.UNKNOWN;
149         verifyDeploymentInfoConstruction(workflowIdLastAction, status, expectedDeploymentStatus);
150     }
151
152     @Test
153     public void shouldSetEmptyOutputsMapWhenInputIsNull() {
154         DeploymentInfo deploymentInfo = new DeploymentInfoBuilder().withDeploymentOutputs(null).build();
155         assertThat(deploymentInfo.getOutputs()).isEmpty();
156     }
157
158     private void verifyDeploymentInfoConstruction(String workflowIdLastAction, String actionStatus,
159         DeploymentStatus expectedDeploymentStatus) {
160
161         Execution execution = new Execution();
162         execution.setWorkflowId(workflowIdLastAction);
163         execution.setStatus(actionStatus);
164         execution.setError(ERROR_MESSAGE);
165         DeploymentInfo deploymentInfo = new DeploymentInfoBuilder()
166             .fromExecution(execution)
167             .build();
168
169         assertThat(deploymentInfo.getLastAction()).isEqualTo(workflowIdLastAction);
170         assertThat(deploymentInfo.getActionStatus()).isEqualTo(actionStatus);
171         assertThat(deploymentInfo.getErrorMessage()).isEqualTo(ERROR_MESSAGE);
172         assertThat(deploymentInfo.getStatus()).isEqualTo(expectedDeploymentStatus);
173     }
174 }