Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / test / java / org / onap / so / openstack / utils / MsoHeatUtilsWithUpdateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
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
21 package org.onap.so.openstack.utils;
22
23 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
24 import static org.junit.Assert.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.verify;
29 import java.io.File;
30 import java.io.IOException;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Optional;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.mockito.Spy;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.so.TestDataSetup;
43 import org.onap.so.cloud.CloudConfig;
44 import org.onap.so.db.catalog.beans.CloudSite;
45 import org.onap.so.openstack.beans.HeatStatus;
46 import org.onap.so.openstack.beans.StackInfo;
47 import org.onap.so.openstack.exceptions.MsoException;
48 import org.springframework.core.env.Environment;
49 import com.fasterxml.jackson.core.JsonParseException;
50 import com.fasterxml.jackson.databind.JsonMappingException;
51 import com.woorea.openstack.base.client.OpenStackRequest;
52 import com.woorea.openstack.heat.Heat;
53 import com.woorea.openstack.heat.model.Stack;
54
55 @RunWith(MockitoJUnitRunner.class)
56 public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
57     @Mock
58     private CloudConfig cloudConfig;
59
60     @Mock
61     private Environment environment;
62
63     @Spy
64     @InjectMocks
65     private MsoHeatUtilsWithUpdate heatUtils;
66
67     private String cloudOwner;
68     private String cloudSiteId;
69     private String tenantId;
70     private String stackName;
71     private String heatTemplate;
72     private Map<String, Object> stackInputs;
73     private boolean pollForCompletion;
74     private int timeoutMinutes;
75
76     @Before
77     public void before() {
78         MockitoAnnotations.initMocks(this);
79
80         cloudOwner = "cloudOwner";
81         cloudSiteId = "cloudSiteId";
82         tenantId = "tenantId";
83         stackName = "stackName";
84         heatTemplate = "heatTemplate";
85         stackInputs = new HashMap<>();
86         pollForCompletion = true;
87         timeoutMinutes = 0;
88     }
89
90     @Test
91     public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
92         CloudSite cloudSite = new CloudSite();
93         Heat heatClient = new Heat("endpoint");
94         Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
95         Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
96
97         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
98         expectedStackInfo.setCanonicalName("stackName/id");
99
100         doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
101         doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
102         doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
103         doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
104         doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
105
106         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
107                 stackInputs, pollForCompletion, timeoutMinutes);
108
109         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
110     }
111
112     @Test
113     public void updateStackWithEnvironmentTest()
114             throws JsonParseException, JsonMappingException, IOException, MsoException {
115         String environmentString = "environmentString";
116
117         CloudSite cloudSite = new CloudSite();
118         Heat heatClient = new Heat("endpoint");
119         Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
120         Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
121
122         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
123         expectedStackInfo.setCanonicalName("stackName/id");
124
125         doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
126         doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
127
128         doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
129         doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
130         doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
131
132         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
133                 stackInputs, pollForCompletion, timeoutMinutes, environmentString);
134
135         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
136     }
137
138     @Test
139     public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
140         String environmentString = "environmentString";
141         Map<String, Object> files = new HashMap<>();
142         files.put("file1", new Object());
143
144         CloudSite cloudSite = new CloudSite();
145         Heat heatClient = new Heat("endpoint");
146         Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
147         Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
148
149         StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
150         expectedStackInfo.setCanonicalName("stackName/id");
151
152         doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
153         doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
154         doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
155         doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
156         doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
157
158         StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, cloudOwner, tenantId, stackName, heatTemplate,
159                 stackInputs, pollForCompletion, timeoutMinutes, environmentString, files);
160
161         assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
162     }
163 }