aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/test/java/org/onap/so/openstack/utils/MsoHeatUtilsWithUpdateTest.java
blob: c252f61e7f6f436a6687ca1e43cb68c3182d3101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.openstack.utils;

import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.onap.so.TestDataSetup;
import org.onap.so.cloud.CloudConfig;
import org.onap.so.cloud.CloudSite;
import org.onap.so.openstack.beans.HeatStatus;
import org.onap.so.openstack.beans.StackInfo;
import org.onap.so.openstack.exceptions.MsoException;
import org.springframework.core.env.Environment;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.woorea.openstack.base.client.OpenStackRequest;
import com.woorea.openstack.heat.Heat;
import com.woorea.openstack.heat.StackResource;
import com.woorea.openstack.heat.StackResource.UpdateStack;
import com.woorea.openstack.heat.model.Stack;
import com.woorea.openstack.heat.model.UpdateStackParam;

@RunWith(MockitoJUnitRunner.class)
public class MsoHeatUtilsWithUpdateTest extends TestDataSetup {
	@Mock
	private CloudConfig cloudConfig;
	
	@Mock
	private Environment environment;
	
	@Spy
	@InjectMocks
	private MsoHeatUtilsWithUpdate heatUtils;
	
	private String cloudSiteId;
	private String tenantId;
	private String stackName;
	private String heatTemplate;
	private Map<String, Object> stackInputs;
	private boolean pollForCompletion;
	private int timeoutMinutes;
	
	@Before
	public void before() {
		MockitoAnnotations.initMocks(this);
		
		cloudSiteId = "cloudSiteId";
		tenantId = "tenantId";
		stackName = "stackName";
		heatTemplate = "heatTemplate";
		stackInputs = new HashMap<>();
		pollForCompletion = true;
		timeoutMinutes = 0;
	}
	
	@Test
	public void updateStackTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
		CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
		Heat heatClient = new Heat("endpoint");
		Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
		Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
		
		StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
		expectedStackInfo.setCanonicalName("stackName/id");
		
		doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
		doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
		doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
		doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
		doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		
		StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName, 
				heatTemplate, stackInputs, pollForCompletion, timeoutMinutes);
		
		assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
	}
	
	@Test
	public void updateStackWithEnvironmentTest() throws JsonParseException, JsonMappingException, IOException, MsoException {
		String environmentString = "environmentString";
		
		CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
		Heat heatClient = new Heat("endpoint");
		Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
		Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
		
		StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
		expectedStackInfo.setCanonicalName("stackName/id");
		
		doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
		doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
		doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
		doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
		doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		
		StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName, 
				heatTemplate, stackInputs, pollForCompletion, timeoutMinutes, environmentString);
		
		assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
	}
	
	@Test
	public void updateStackWithFilesTest() throws MsoException, JsonParseException, JsonMappingException, IOException {
		String environmentString = "environmentString";
		Map<String, Object> files = new HashMap<>();
		files.put("file1", new Object());
		
		CloudSite cloudSite = mapper.readValue(new File(RESOURCE_PATH + "CloudSite.json"), CloudSite.class);
		Heat heatClient = new Heat("endpoint");
		Stack heatStack = mapper.readValue(new File(RESOURCE_PATH + "HeatStack.json"), Stack.class);
		Stack updateStack = mapper.readValue(new File(RESOURCE_PATH + "UpdateStack.json"), Stack.class);
		
		StackInfo expectedStackInfo = new StackInfo("stackName", HeatStatus.UPDATED, "stackStatusReason", null);
		expectedStackInfo.setCanonicalName("stackName/id");
		
		doReturn(Optional.of(cloudSite)).when(cloudConfig).getCloudSite(isA(String.class));
		doReturn(heatClient).when(heatUtils).getHeatClient(isA(CloudSite.class), isA(String.class));
		doReturn(heatStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		doReturn(null).when(heatUtils).executeAndRecordOpenstackRequest(isA(OpenStackRequest.class));
		doReturn("0").when(environment).getProperty(isA(String.class), isA(String.class));
		doReturn(updateStack).when(heatUtils).queryHeatStack(isA(Heat.class), isA(String.class));
		
		StackInfo actualStackInfo = heatUtils.updateStack(cloudSiteId, tenantId, stackName, 
				heatTemplate, stackInputs, pollForCompletion, timeoutMinutes , environmentString, files);
		
		assertThat(actualStackInfo, sameBeanAs(expectedStackInfo));
	}
}