3e70a3229fef1f06c0d03b2039d48856da69bc1d
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / distribution / engine / DistributionEngineTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.openecomp.sdc.be.components.distribution.engine;
22
23 import mockit.Deencapsulation;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.ArgumentMatchers;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.components.utils.OperationalEnvironmentBuilder;
32 import org.openecomp.sdc.be.config.Configuration;
33 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
34 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionStatusTopicConfig;
35 import org.openecomp.sdc.be.dao.api.ActionStatus;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.model.User;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
40
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.function.Function;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verifyNoInteractions;
54 import static org.mockito.Mockito.when;
55
56 public class DistributionEngineTest{
57
58         public static final String DISTRIBUTION_ID = "distId";
59         public static final String ENV_ID = "envId";
60         public static final String USER_ID = "userId";
61         public static final String MODIFIER = "modifier";
62
63         @InjectMocks
64         private DistributionEngine testInstance;
65
66         @Mock
67         private EnvironmentsEngine environmentsEngine;
68
69         @Mock
70         private DistributionNotificationSender distributionNotificationSender;
71         
72         @Mock
73         private ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder;
74         
75         private DummyDistributionConfigurationManager distributionEngineConfigurationMock;
76
77         private Map<String, OperationalEnvironmentEntry> envs;
78
79     private User modifier = new User();
80
81     private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class);
82
83         @Before
84         public void setUpMock() throws Exception {
85                 MockitoAnnotations.openMocks(this);
86                 distributionEngineConfigurationMock = new DummyDistributionConfigurationManager();
87                 envs = getEnvs(ENV_ID);
88         modifier.setUserId(USER_ID);
89         modifier.setFirstName(MODIFIER);
90         modifier.setLastName(MODIFIER);
91         when(environmentContext.getDefaultValue()).thenReturn("General_Revenue-Bearing");
92         when(distributionEngineConfigurationMock.getConfiguration().getEnvironmentContext())
93                                 .thenReturn(environmentContext);
94         }
95
96     @Test
97     public void notifyService() throws Exception {
98         NotificationDataImpl notificationData = new NotificationDataImpl();
99         Service service = new Service();
100         when(environmentsEngine.getEnvironmentById(ENV_ID)).thenReturn(envs.get(ENV_ID));
101         when(distributionEngineConfigurationMock.getConfigurationMock().getDistributionNotifTopicName()).thenReturn("topic");
102         when(distributionNotificationSender.sendNotification(eq("topic-ENVID"), eq(DISTRIBUTION_ID), any(EnvironmentMessageBusData.class),
103                 any(NotificationDataImpl.class), any(Service.class), any(User.class)))
104         .thenReturn(ActionStatus.OK);
105         ActionStatus actionStatus = testInstance.notifyService(DISTRIBUTION_ID, service, notificationData, ENV_ID, modifier);
106         assertEquals(ActionStatus.OK, actionStatus);
107     }
108
109     @Test
110     public void notifyService_couldNotResolveEnvironment() throws Exception {
111         when(environmentsEngine.getEnvironments()).thenReturn(envs);
112         ActionStatus actionStatus = testInstance.notifyService(DISTRIBUTION_ID, new Service(), new NotificationDataImpl(), "someNonExisitngEnv", modifier);
113         assertEquals(ActionStatus.DISTRIBUTION_ENVIRONMENT_NOT_AVAILABLE, actionStatus);
114         verifyNoInteractions(distributionNotificationSender);
115     }
116
117     @Test
118     public void notifyService_failedWhileSendingNotification() throws Exception {
119         NotificationDataImpl notificationData = new NotificationDataImpl();
120         Service service = new Service();
121         when(environmentsEngine.getEnvironmentById(ENV_ID)).thenReturn(envs.get(ENV_ID));
122         when(distributionEngineConfigurationMock.getConfigurationMock().getDistributionNotifTopicName()).thenReturn("topic");
123         when(distributionNotificationSender.sendNotification(eq("topic-ENVID"), eq(DISTRIBUTION_ID), any(EnvironmentMessageBusData.class),
124                 any(NotificationDataImpl.class), any(Service.class), any(User.class)))
125                 .thenReturn(ActionStatus.GENERAL_ERROR);
126         ActionStatus actionStatus = testInstance.notifyService(DISTRIBUTION_ID, service, notificationData, ENV_ID, modifier);
127         assertEquals(ActionStatus.GENERAL_ERROR, actionStatus);
128     }
129
130         private Map<String, OperationalEnvironmentEntry> getEnvs(String... environmentIds) {
131                 Set<String> uebAddress = new HashSet<>();
132                 uebAddress.add("someAddress");
133                 return Stream.of(environmentIds)
134                                 .map(id -> new OperationalEnvironmentBuilder().setEnvId(id).setDmaapUebAddress(uebAddress).build())
135                                 .collect(Collectors.toMap(OperationalEnvironmentEntry::getEnvironmentId, Function.identity()));
136         }
137
138         private DistributionEngine createTestSubject() {
139                 return new DistributionEngine();
140         }
141
142         @Test(expected=NullPointerException.class)
143         public void testInit() throws Exception {
144                 DistributionEngine testSubject;
145
146                 // default test
147                 testSubject = createTestSubject();
148                 Deencapsulation.invoke(testSubject, "init");
149         }
150
151         @Test
152         public void testShutdown() throws Exception {
153                 DistributionEngine testSubject;
154
155                 // default test
156                 testSubject = createTestSubject();
157                 testSubject.shutdown();
158         }
159
160         @Test
161         public void testValidateConfiguration() throws Exception {
162                 DistributionEngine testSubject;
163                 DistributionEngineConfiguration deConfiguration = new DistributionEngineConfiguration();
164                 deConfiguration.setDistributionStatusTopic((new DistributionStatusTopicConfig()));
165                 boolean result;
166
167                 // default test
168                 testSubject = createTestSubject();
169                 result = Deencapsulation.invoke(testSubject, "validateConfiguration", deConfiguration);
170         }
171
172         @Test
173         public void testIsValidServers() throws Exception {
174                 DistributionEngine testSubject;
175                 List<String> uebServers = null;
176                 String methodName = "";
177                 String paramName = "";
178                 boolean result;
179
180                 // test 1
181                 testSubject = createTestSubject();
182                 uebServers = null;
183                 result = Deencapsulation.invoke(testSubject, "isValidServers",
184                                 new Object[] { List.class, methodName, paramName });
185                 Assert.assertEquals(false, result);
186         }
187
188         @Test
189         public void testIsValidFqdn() throws Exception {
190                 DistributionEngine testSubject;
191                 String serverFqdn = "";
192                 boolean result;
193
194                 // default test
195                 testSubject = createTestSubject();
196                 result = Deencapsulation.invoke(testSubject, "isValidFqdn", new Object[] { serverFqdn });
197         }
198
199         @Test
200         public void testIsValidParam() throws Exception {
201                 DistributionEngine testSubject;
202                 String paramValue = "";
203                 String methodName = "";
204                 String paramName = "";
205                 boolean result;
206
207                 // default test
208                 testSubject = createTestSubject();
209                 result = Deencapsulation.invoke(testSubject, "isValidParam",
210                                 new Object[] { paramValue, methodName, paramName });
211         }
212
213         @Test
214         public void testIsValidParam_1() throws Exception {
215                 DistributionEngine testSubject;
216                 List<String> paramValue = null;
217                 String methodName = "";
218                 String paramName = "";
219                 boolean result;
220
221                 // default test
222                 testSubject = createTestSubject();
223                 result = Deencapsulation.invoke(testSubject, "isValidParam",
224                                 new Object[] { List.class, methodName, paramName });
225         }
226
227         @Test
228         public void testIsValidObject() throws Exception {
229                 DistributionEngine testSubject;
230                 Object paramValue = null;
231                 String methodName = "";
232                 String paramName = "";
233                 boolean result;
234
235                 // test 1
236                 testSubject = createTestSubject();
237                 paramValue = null;
238                 result = Deencapsulation.invoke(testSubject, "isValidObject",
239                                 new Object[] { Object.class, methodName, paramName });
240                 Assert.assertEquals(false, result);
241         }
242
243         @Test
244         public void testGetEnvironmentErrorDescription() throws Exception {
245                 DistributionEngine testSubject;
246                 StorageOperationStatus status = null;
247                 String result;
248
249                 // default test
250                 testSubject = createTestSubject();
251                 result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
252                                  StorageOperationStatus.DISTR_ENVIRONMENT_NOT_AVAILABLE);
253                 result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
254                                  StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND);
255                 result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
256                                  StorageOperationStatus.DISTR_ENVIRONMENT_SENT_IS_INVALID);
257                 result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
258                                  StorageOperationStatus.ARTIFACT_NOT_FOUND);
259         }
260
261         @Test
262         public void testIsEnvironmentAvailable() throws Exception {
263                 DistributionEngine testSubject;
264                 String envName = "";
265                 StorageOperationStatus result;
266
267                 // test 1
268                 testSubject = createTestSubject();
269                 envName = null;
270                 result = testSubject.isEnvironmentAvailable(envName);
271                 Assert.assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_SENT_IS_INVALID, result);
272
273                 // test 2
274                 testSubject = createTestSubject();
275                 envName = "mock";
276                 result = testSubject.isEnvironmentAvailable(envName);
277                 Assert.assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND, result);
278         }
279
280         //TODO Create test coverage for this method
281         /*@Test
282         public void testIsEnvironmentAvailable_1() throws Exception {
283                 DistributionEngine testSubject;
284                 StorageOperationStatus result;
285
286                 // default test
287                 testSubject = createTestSubject();
288                 result = testInstance.isEnvironmentAvailable();
289         }*/
290
291         @Test(expected=NullPointerException.class)
292         public void testDisableEnvironment() throws Exception {
293                 DistributionEngine testSubject;
294                 String envName = "";
295
296                 // default test
297                 testSubject = createTestSubject();
298                 testSubject.disableEnvironment(envName);
299         }
300
301         @Test
302         public void testBuildTopicName() throws Exception {
303                 DistributionEngine testSubject;
304                 String envName = "";
305                 String result;
306
307                 // default test
308                 testSubject = createTestSubject();
309                 result = Deencapsulation.invoke(testSubject, "buildTopicName", new Object[] { envName });
310         }
311
312         @Test
313         public void testIsReadyForDistribution() throws Exception {
314                 DistributionEngine testSubject;
315                 Service service = null;
316                 String envName = "";
317                 StorageOperationStatus result;
318
319                 // default test
320                 testSubject = createTestSubject();
321                 result = testSubject.isReadyForDistribution(envName);
322         }
323
324
325         @Test
326         public void testGetEnvironmentById() throws Exception {
327                 DistributionEngine testSubject;
328                 String opEnvId = "";
329                 OperationalEnvironmentEntry result;
330
331                 // default test
332                 when(environmentsEngine.getEnvironmentById(ArgumentMatchers.anyString())).thenReturn(new OperationalEnvironmentEntry());
333                 result = testInstance.getEnvironmentById(opEnvId);
334         }
335
336         @Test
337         public void testBuildServiceForDistribution() throws Exception {
338                 Service service = new Service();
339                 String distributionId = "";
340                 String workloadContext = "";
341                 INotificationData result;
342
343                 // default test
344                 //testSubject = createTestSubject();
345                 when(serviceDistributionArtifactsBuilder.buildResourceInstanceForDistribution(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(new NotificationDataImpl());
346                 result = testInstance.buildServiceForDistribution(service, distributionId, workloadContext);
347         }
348 }