2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.distribution.engine;
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;
40 import java.util.HashSet;
41 import java.util.List;
44 import java.util.function.Function;
45 import java.util.stream.Collectors;
46 import java.util.stream.Stream;
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;
56 public class DistributionEngineTest {
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";
64 private DistributionEngine testInstance;
67 private EnvironmentsEngine environmentsEngine;
69 private KafkaHandler kafkaHandler;
71 private DistributionNotificationSender distributionNotificationSender;
73 private ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder;
75 private DummyDistributionConfigurationManager distributionEngineConfigurationMock;
77 private Map<String, OperationalEnvironmentEntry> envs;
79 private User modifier = new User();
81 private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class);
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);
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);
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);
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);
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()));
138 private DistributionEngine createTestSubject() {
139 return new DistributionEngine();
143 public void testInit() throws Exception {
144 DistributionEngine testSubject;
147 testSubject = createTestSubject();
148 assertThrows(NullPointerException.class, () -> Deencapsulation.invoke(testSubject, "init"));
152 void testShutdown() throws Exception {
153 DistributionEngine testSubject;
156 testSubject = createTestSubject();
157 testSubject.shutdown();
161 void testValidateConfiguration() throws Exception {
162 when(kafkaHandler.isKafkaActive()).thenReturn(false);
164 DistributionEngine testSubject;
165 DistributionEngineConfiguration deConfiguration = new DistributionEngineConfiguration();
166 deConfiguration.setDistributionStatusTopic((new DistributionEngineConfiguration.DistributionStatusTopicConfig()));
170 testSubject = createTestSubject();
171 testSubject.setKafkaHandler(kafkaHandler);
172 result = Deencapsulation.invoke(testSubject, "validateConfiguration", deConfiguration);
176 void testIsValidServers() throws Exception {
177 DistributionEngine testSubject;
178 List<String> uebServers = null;
179 String methodName = "";
180 String paramName = "";
184 testSubject = createTestSubject();
186 result = Deencapsulation.invoke(testSubject, "isValidServers",
187 new Object[]{List.class, methodName, paramName});
188 assertEquals(false, result);
192 void testIsValidFqdn() throws Exception {
193 DistributionEngine testSubject;
194 String serverFqdn = "";
198 testSubject = createTestSubject();
199 result = Deencapsulation.invoke(testSubject, "isValidFqdn", new Object[]{serverFqdn});
203 void testIsValidParam() throws Exception {
204 DistributionEngine testSubject;
205 String paramValue = "";
206 String methodName = "";
207 String paramName = "";
211 testSubject = createTestSubject();
212 result = Deencapsulation.invoke(testSubject, "isValidParam",
213 new Object[]{paramValue, methodName, paramName});
217 void testIsValidParam_1() throws Exception {
218 DistributionEngine testSubject;
219 List<String> paramValue = null;
220 String methodName = "";
221 String paramName = "";
225 testSubject = createTestSubject();
226 result = Deencapsulation.invoke(testSubject, "isValidParam",
227 new Object[]{List.class, methodName, paramName});
231 void testIsValidObject() throws Exception {
232 DistributionEngine testSubject;
233 Object paramValue = null;
234 String methodName = "";
235 String paramName = "";
239 testSubject = createTestSubject();
241 result = Deencapsulation.invoke(testSubject, "isValidObject",
242 new Object[]{Object.class, methodName, paramName});
243 assertEquals(false, result);
247 void testGetEnvironmentErrorDescription() throws Exception {
248 DistributionEngine testSubject;
249 StorageOperationStatus status = null;
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);
265 void testIsEnvironmentAvailable() throws Exception {
266 DistributionEngine testSubject;
268 StorageOperationStatus result;
271 testSubject = createTestSubject();
273 result = testSubject.isEnvironmentAvailable(envName);
274 assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_SENT_IS_INVALID, result);
277 testSubject = createTestSubject();
279 result = testSubject.isEnvironmentAvailable(envName);
280 assertEquals(StorageOperationStatus.DISTR_ENVIRONMENT_NOT_FOUND, result);
283 //TODO Create test coverage for this method
285 public void testIsEnvironmentAvailable_1() throws Exception {
286 DistributionEngine testSubject;
287 StorageOperationStatus result;
290 testSubject = createTestSubject();
291 result = testInstance.isEnvironmentAvailable();
295 void testDisableEnvironment() throws Exception {
296 DistributionEngine testSubject;
300 testSubject = createTestSubject();
301 assertThrows(NullPointerException.class, () -> testSubject.disableEnvironment(envName));
305 void testBuildTopicName() throws Exception {
306 DistributionEngine testSubject;
311 testSubject = createTestSubject();
312 result = Deencapsulation.invoke(testSubject, "buildTopicName", new Object[]{envName});
316 void testIsReadyForDistribution() throws Exception {
317 DistributionEngine testSubject;
318 Service service = null;
320 StorageOperationStatus result;
323 testSubject = createTestSubject();
324 result = testSubject.isReadyForDistribution(envName);
329 void testGetEnvironmentById() throws Exception {
330 DistributionEngine testSubject;
332 OperationalEnvironmentEntry result;
335 when(environmentsEngine.getEnvironmentById(ArgumentMatchers.anyString())).thenReturn(new OperationalEnvironmentEntry());
336 result = testInstance.getEnvironmentById(opEnvId);
340 void testBuildServiceForDistribution() throws Exception {
341 Service service = new Service();
342 String distributionId = "";
343 String workloadContext = "";
344 INotificationData result;
347 //testSubject = createTestSubject();
348 when(serviceDistributionArtifactsBuilder.buildResourceInstanceForDistribution(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(new NotificationDataImpl());
349 result = testInstance.buildServiceForDistribution(service, distributionId, workloadContext);