summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/ServiceDistributionBLTest.java
blob: 28f97ad8582113fcbc7a80bbf310e1356671ced4 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package org.openecomp.sdc.be.components;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.auditing.impl.AuditingManager;
import org.openecomp.sdc.be.components.distribution.engine.DistributionEngine;
import org.openecomp.sdc.be.components.distribution.engine.INotificationData;
import org.openecomp.sdc.be.components.distribution.engine.NotificationDataImpl;
import org.openecomp.sdc.be.components.health.HealthCheckBusinessLogic;
import org.openecomp.sdc.be.components.impl.ActivationRequestInformation;
import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
import org.openecomp.sdc.be.components.validation.ServiceDistributionValidation;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.externalapi.servlet.representation.ServiceDistributionReqInfo;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DistributionStatusEnum;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.openecomp.sdc.common.util.ThreadLocalsHolder;
import org.openecomp.sdc.exception.ResponseFormat;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

/**
 * Created by chaya on 10/26/2017.
 */
public class ServiceDistributionBLTest {

    @InjectMocks
    ServiceBusinessLogic bl = new ServiceBusinessLogic();

    @Mock
    ServiceDistributionValidation serviceDistributionValidation;

    @Mock
    HealthCheckBusinessLogic healthCheckBusinessLogic;

    @Mock
    ToscaOperationFacade toscaOperationFacade;

    ComponentsUtils componentsUtils;

    @Mock
    DistributionEngine distributionEngine;

    private Service serviceToActivate;
    private ActivationRequestInformation activationRequestInformation;
    private String WORKLOAD_CONTEXT = "vnfContext";
    private String TENANT = "tenant";
    private String DID = "distributionId";
    private User modifier;


    public ServiceDistributionBLTest() {
    }

    @Before
    public void setup() {

        ExternalConfiguration.setAppName("catalog-be");
        MockitoAnnotations.initMocks(this);
        // init Configuration
        String appConfigDir = "src/test/resources/config/catalog-be";
        ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
        ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
        componentsUtils = new ComponentsUtils(Mockito.mock(AuditingManager.class));
        bl.setComponentsUtils(componentsUtils);
        serviceToActivate = new Service();
        serviceToActivate.setDistributionStatus(DistributionStatusEnum.DISTRIBUTION_NOT_APPROVED);
        activationRequestInformation = new ActivationRequestInformation(serviceToActivate, WORKLOAD_CONTEXT, TENANT);
        when(toscaOperationFacade.getToscaElement(anyString(), any(ComponentParametersView.class)))
                .thenReturn(Either.left(serviceToActivate));
        when(distributionEngine.buildServiceForDistribution(any(Service.class), anyString(), anyString()))
                .thenReturn(new NotificationDataImpl());
        modifier = new User();
        modifier.setUserId("uid");
        modifier.setFirstName("user");
    }

    @Test
    public void testActivateServiceOnTenantValidationFails() {
        int VALIDATION_FAIL_STATUS = 666;
        when(serviceDistributionValidation.validateActivateServiceRequest
                (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
                .thenReturn(Either.right(new ResponseFormat(VALIDATION_FAIL_STATUS)));
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals((int) stringResponseFormatEither.right().value().getStatus(), VALIDATION_FAIL_STATUS);
    }

    //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
    @Test
    public void testDistributionAuthenticationFails() {
        mockAllMethodsUntilDENotification();
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(),any(User.class)))
                .thenReturn(ActionStatus.AUTHENTICATION_ERROR);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
        assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
    }

    //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
    @Test
    public void testDistributionUnknownHostFails() {
        mockAllMethodsUntilDENotification();
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
                .thenReturn(ActionStatus.UNKNOWN_HOST);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
        assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
    }

    //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
    @Test
    public void testDistributionConnectionErrorFails() {
        mockAllMethodsUntilDENotification();
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
                .thenReturn(ActionStatus.CONNNECTION_ERROR);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
        assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
    }

    //TODO see if we want to add ActionStatus.AUTHENTICATION_ERROR to error-configuration.yaml
    @Test
    public void testDistributionObjectNotFoundFails() {
        mockAllMethodsUntilDENotification();
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
                .thenReturn(ActionStatus.OBJECT_NOT_FOUND);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
        assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
    }

    @Test
    public void testDistributionGeneralFails() {
        mockAllMethodsUntilDENotification();
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), any(User.class)))
                .thenReturn(ActionStatus.GENERAL_ERROR);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isRight());
        assertEquals(502, (int) stringResponseFormatEither.right().value().getStatus());
        assertEquals("SVC4676", stringResponseFormatEither.right().value().getMessageId());
    }

    @Test
    public void testDistributionOk() {
        mockAllMethodsUntilDENotification();
        ThreadLocalsHolder.setUuid(DID);
        when(distributionEngine.notifyService(anyString(),any(Service.class), any(INotificationData.class), anyString(), anyString(), any(User.class)))
                .thenReturn(ActionStatus.OK);
        Either<String, ResponseFormat> stringResponseFormatEither = callActivateServiceOnTenantWIthDefaults();
        assertTrue(stringResponseFormatEither.isLeft());
        assertEquals(stringResponseFormatEither.left().value(), DID);
    }

    private void mockAllMethodsUntilDENotification() {
        when(serviceDistributionValidation.validateActivateServiceRequest
                (anyString(), anyString(),any(User.class), any(ServiceDistributionReqInfo.class)))
                .thenReturn(Either.left(activationRequestInformation));
        when(healthCheckBusinessLogic.isDistributionEngineUp()).thenReturn(true);
    }

    private Either<String, ResponseFormat> callActivateServiceOnTenantWIthDefaults() {
        return bl.activateServiceOnTenantEnvironment("serviceId", "envId", modifier, new ServiceDistributionReqInfo("workload"));
    }
}