Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / ServiceDistributionBLTest.java
1 package org.openecomp.sdc.be.components;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.Mockito;
9 import org.mockito.MockitoAnnotations;
10 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
11 import org.openecomp.sdc.be.components.distribution.engine.DistributionEngine;
12 import org.openecomp.sdc.be.components.distribution.engine.INotificationData;
13 import org.openecomp.sdc.be.components.distribution.engine.NotificationDataImpl;
14 import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
15 import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
16 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
17 import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
18 import org.openecomp.sdc.be.config.ConfigurationManager;
19 import org.openecomp.sdc.be.dao.api.ActionStatus;
20 import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
21 import org.openecomp.sdc.be.impl.ComponentsUtils;
22 import org.openecomp.sdc.be.model.ComponentParametersView;
23 import org.openecomp.sdc.be.model.DistributionStatusEnum;
24 import org.openecomp.sdc.be.model.Service;
25 import org.openecomp.sdc.be.model.User;
26 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
27 import org.openecomp.sdc.common.api.ConfigurationSource;
28 import org.openecomp.sdc.common.impl.ExternalConfiguration;
29 import org.openecomp.sdc.common.impl.FSConfigurationSource;
30 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
31 import org.openecomp.sdc.exception.ResponseFormat;
32
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.anyString;
37 import static org.mockito.Mockito.when;
38
39 /**
40  * Created by chaya on 10/26/2017.
41  */
42 public class ServiceDistributionBLTest {
43
44     @InjectMocks
45     ServiceBusinessLogic bl = new ServiceBusinessLogic();
46
47     @Mock
48     ServiceDistributionValidation serviceDistributionValidation;
49
50     @Mock
51     HealthCheckBusinessLogic healthCheckBusinessLogic;
52
53     @Mock
54     ToscaOperationFacade toscaOperationFacade;
55
56     ComponentsUtils componentsUtils;
57
58     @Mock
59     DistributionEngine distributionEngine;
60
61     private Service serviceToActivate;
62     private ActivationRequestInformation activationRequestInformation;
63     private String WORKLOAD_CONTEXT = "vnfContext";
64     private String TENANT = "tenant";
65     private String DID = "distributionId";
66     private User modifier;
67
68
69     public ServiceDistributionBLTest() {
70     }
71
72     @Before
73     public void setup() {
74
75         ExternalConfiguration.setAppName("catalog-be");
76         MockitoAnnotations.initMocks(this);
77         // init Configuration
78         String appConfigDir = "src/test/resources/config/catalog-be";
79         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
80         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
81         componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
82         bl.setComponentsUtils(componentsUtils);
83         serviceToActivate = new Service();
84         serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
85         activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
86         when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
87                 .thenReturn(Either.left(serviceToActivate));
88         when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
89                 .thenReturn(new NotificationDataImpl());
90         modifier = new User();
91         modifier.setUserId("uid");
92         modifier.setFirstName("user");
93     }
94
95     @Test
96     public void testActivateServiceOnTenantValidationFails() {
97         int VALIDATION_FAIL_STATUS = 666;
98         when(serviceDistributionValidation.validateActivateServiceRequest
99                 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
100                 .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
101         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
102         assertTrue(stringResponseFormatEither.isRight());
103         assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
104     }
105
106     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
107     @Test
108     public void testDistributionAuthenticationFails() {
109         mockAllMethodsUntilDENotification();
110         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(),any(User.class)))
111                 .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
112         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
113         assertTrue(stringResponseFormatEither.isRight());
114         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
115         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
116     }
117
118     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
119     @Test
120     public void testDistributionUnknownHostFails() {
121         mockAllMethodsUntilDENotification();
122         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
123                 .thenReturn(ActionStatus.UNKNOWN_HOST);
124         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
125         assertTrue(stringResponseFormatEither.isRight());
126         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
127         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
128     }
129
130     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
131     @Test
132     public void testDistributionConnectionErrorFails() {
133         mockAllMethodsUntilDENotification();
134         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
135                 .thenReturn(ActionStatus.CONNNECTION_ERROR);
136         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
137         assertTrue(stringResponseFormatEither.isRight());
138         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
139         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
140     }
141
142     //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
143     @Test
144     public void testDistributionObjectNotFoundFails() {
145         mockAllMethodsUntilDENotification();
146         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
147                 .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
148         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
149         assertTrue(stringResponseFormatEither.isRight());
150         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
151         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
152     }
153
154     @Test
155     public void testDistributionGeneralFails() {
156         mockAllMethodsUntilDENotification();
157         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
158                 .thenReturn(ActionStatus.GENERAL_ERROR);
159         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
160         assertTrue(stringResponseFormatEither.isRight());
161         assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
162         assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
163     }
164
165     @Test
166     public void testDistributionOk() {
167         mockAllMethodsUntilDENotification();
168         ThreadLocalsHolder.setUuid(DID);
169         when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
170                 .thenReturn(ActionStatus.OK);
171         Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
172         assertTrue(stringResponseFormatEither.isLeft());
173         assertEquals(stringResponseFormatEither.left().value(), DID);
174     }
175
176     private void mockAllMethodsUntilDENotification() {
177         when(serviceDistributionValidation.validateActivateServiceRequest
178                 (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
179                 .thenReturn(Either.left(activationRequestInformation));
180         when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
181     }
182
183     private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
184         return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
185     }
186 }