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