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