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