c5d8d37d3937e6d3521f5c35638ca3a9289184cd
[sdc.git] /
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.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.ArgumentMatchers;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.components.kafka.KafkaHandler;
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.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.model.Service;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.be.resources.data.OperationalEnvironmentEntry;
39
40 import java.util.HashSet;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Set;
44 import java.util.function.Function;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
47
48 import static org.junit.jupiter.api.Assertions.assertEquals;
49 import static org.junit.jupiter.api.Assertions.assertThrows;
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     @Mock
69     private KafkaHandler kafkaHandler;
70     @Mock
71     private DistributionNotificationSender distributionNotificationSender;
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     @BeforeEach
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     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     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     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
143     public void testInit() throws Exception {
144         DistributionEngine testSubject;
145
146         // default test
147         testSubject = createTestSubject();
148         assertThrows(NullPointerException.class, () -> Deencapsulation.invoke(testSubject, "init"));
149     }
150
151     @Test
152     void testShutdown() throws Exception {
153         DistributionEngine testSubject;
154
155         // default test
156         testSubject = createTestSubject();
157         testSubject.shutdown();
158     }
159
160     @Test
161     void testValidateConfiguration() throws Exception {
162         when(kafkaHandler.isKafkaActive()).thenReturn(false);
163
164         DistributionEngine testSubject;
165                 DistributionEngineConfiguration deConfiguration = new DistributionEngineConfiguration();
166                 deConfiguration.setDistributionStatusTopic((new DistributionEngineConfiguration.DistributionStatusTopicConfig()));
167                 boolean result;
168
169                 // default test
170                 testSubject = createTestSubject();
171         testSubject.setKafkaHandler(kafkaHandler);
172                 result = Deencapsulation.invoke(testSubject, "validateConfiguration", deConfiguration);
173     }
174
175     @Test
176     void testIsValidServers() throws Exception {
177         DistributionEngine testSubject;
178         List<String> uebServers = null;
179         String methodName = "";
180         String paramName = "";
181         boolean result;
182
183         // test 1
184         testSubject = createTestSubject();
185         uebServers = null;
186         result = Deencapsulation.invoke(testSubject, "isValidServers",
187                 new Object[]{List.class, methodName, paramName});
188         assertEquals(false, result);
189     }
190
191     @Test
192     void testIsValidFqdn() throws Exception {
193         DistributionEngine testSubject;
194         String serverFqdn = "";
195         boolean result;
196
197         // default test
198         testSubject = createTestSubject();
199         result = Deencapsulation.invoke(testSubject, "isValidFqdn", new Object[]{serverFqdn});
200     }
201
202     @Test
203     void testIsValidParam() throws Exception {
204         DistributionEngine testSubject;
205         String paramValue = "";
206         String methodName = "";
207         String paramName = "";
208         boolean result;
209
210         // default test
211         testSubject = createTestSubject();
212         result = Deencapsulation.invoke(testSubject, "isValidParam",
213                 new Object[]{paramValue, methodName, paramName});
214     }
215
216     @Test
217     void testIsValidParam_1() throws Exception {
218         DistributionEngine testSubject;
219         List<String> paramValue = null;
220         String methodName = "";
221         String paramName = "";
222         boolean result;
223
224         // default test
225         testSubject = createTestSubject();
226         result = Deencapsulation.invoke(testSubject, "isValidParam",
227                 new Object[]{List.class, methodName, paramName});
228     }
229
230     @Test
231     void testIsValidObject() throws Exception {
232         DistributionEngine testSubject;
233         Object paramValue = null;
234         String methodName = "";
235         String paramName = "";
236         boolean result;
237
238         // test 1
239         testSubject = createTestSubject();
240         paramValue = null;
241         result = Deencapsulation.invoke(testSubject, "isValidObject",
242                 new Object[]{Object.class, methodName, paramName});
243         assertEquals(false, result);
244     }
245
246     @Test
247     void testGetEnvironmentErrorDescription() throws Exception {
248         DistributionEngine testSubject;
249         StorageOperationStatus status = null;
250         String result;
251
252         // default test
253         testSubject = createTestSubject();
254         result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
255                 StorageOperationStatus.DISTR_ENVIRONMENT_NOT_AVAILABLE);
256         result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
257                 StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND);
258         result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
259                 StorageOperationStatus.DISTR_ENVIRONMENT_SENT_IS_INVALID);
260         result = Deencapsulation.invoke(testSubject, "getEnvironmentErrorDescription",
261                 StorageOperationStatus.ARTIFACT_NOT_FOUND);
262     }
263
264     @Test
265     void testIsEnvironmentAvailable() throws Exception {
266         DistributionEngine testSubject;
267         String envName = "";
268         StorageOperationStatus result;
269
270         // test 1
271         testSubject = createTestSubject();
272         envName = null;
273         result = testSubject.isEnvironmentAvailable(envName);
274         assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_SENT_IS_INVALID, result);
275
276         // test 2
277         testSubject = createTestSubject();
278         envName = "mock";
279         result = testSubject.isEnvironmentAvailable(envName);
280         assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND, result);
281     }
282
283     //TODO Create test coverage for this method
284         /*@Test
285         public void testIsEnvironmentAvailable_1() throws Exception {
286                 DistributionEngine testSubject;
287                 StorageOperationStatus result;
288
289                 // default test
290                 testSubject = createTestSubject();
291                 result = testInstance.isEnvironmentAvailable();
292         }*/
293
294     @Test
295     void testDisableEnvironment() throws Exception {
296         DistributionEngine testSubject;
297         String envName = "";
298
299         // default test
300         testSubject = createTestSubject();
301         assertThrows(NullPointerException.class, () -> testSubject.disableEnvironment(envName));
302     }
303
304     @Test
305     void testBuildTopicName() throws Exception {
306         DistributionEngine testSubject;
307         String envName = "";
308         String result;
309
310         // default test
311         testSubject = createTestSubject();
312         result = Deencapsulation.invoke(testSubject, "buildTopicName", new Object[]{envName});
313     }
314
315     @Test
316     void testIsReadyForDistribution() throws Exception {
317         DistributionEngine testSubject;
318         Service service = null;
319         String envName = "";
320         StorageOperationStatus result;
321
322         // default test
323         testSubject = createTestSubject();
324         result = testSubject.isReadyForDistribution(envName);
325     }
326
327
328     @Test
329     void testGetEnvironmentById() throws Exception {
330         DistributionEngine testSubject;
331         String opEnvId = "";
332         OperationalEnvironmentEntry result;
333
334         // default test
335         when(environmentsEngine.getEnvironmentById(ArgumentMatchers.anyString())).thenReturn(new OperationalEnvironmentEntry());
336         result = testInstance.getEnvironmentById(opEnvId);
337     }
338
339     @Test
340     void testBuildServiceForDistribution() throws Exception {
341         Service service = new Service();
342         String distributionId = "";
343         String workloadContext = "";
344         INotificationData result;
345
346         // default test
347         //testSubject = createTestSubject();
348         when(serviceDistributionArtifactsBuilder.buildResourceInstanceForDistribution(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(new NotificationDataImpl());
349         result = testInstance.buildServiceForDistribution(service, distributionId, workloadContext);
350     }
351 }