aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/lifecycle/CertificationRequestTest.java
blob: 70833f40b654b62ebc7480f72b8982672741cfed (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.lifecycle;

import fj.data.Either;
import org.apache.http.HttpStatus;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.exception.ResponseFormat;

import java.util.ArrayList;
import java.util.List;

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

@RunWith(MockitoJUnitRunner.Silent.class)
public class CertificationRequestTest extends LifecycleTestBase {

    @Mock
    private ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder;
    @Mock
    private ServiceBusinessLogic serviceBusinessLogic;
    @Mock
    private CapabilityOperation capabilityOperation;
    @Mock
    private ToscaExportHandler toscaExportUtils;

    private CertificationRequestTransition rfcObj;


    @Before
    public void setup() {
        super.setup();
        rfcObj = new CertificationRequestTransition(componentsUtils, toscaElementLifecycleOperation, serviceBusinessLogic, toscaOperationFacade,
            janusGraphDao);
        rfcObj.setConfigurationManager(configurationManager);
    }

    @Test
    public void testVFCMTStateValidation(){
        Either<? extends Component, ResponseFormat> changeStateResult;
        Resource resource = createResourceVFCMTObject();

        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();

        User user = new User();
        user.setUserId("cs0008");
        user.setFirstName("Carlos");
        user.setLastName("Santana");
        user.setRole(Role.TESTER.name());

        changeStateResult = rfcObj.changeState(ComponentTypeEnum.RESOURCE, resource, serviceBusinessLogic, user, owner, false, false);
        assertTrue(changeStateResult.isLeft());
    }

    @Test
    public void testCheckoutStateValidation() {
        Either<? extends Component, ResponseFormat> changeStateResult;
        Resource resource = createResourceObject();

        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();
        changeStateResult = rfcObj.changeState(ComponentTypeEnum.RESOURCE, resource, serviceBusinessLogic, user, owner, false, false);
        assertTrue(changeStateResult.isLeft());

        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
        changeStateResult = rfcObj.changeState(ComponentTypeEnum.RESOURCE, resource, serviceBusinessLogic, user, owner, false, false);
        assertTrue(changeStateResult.isLeft());
    }

    @Test
    public void testAlreadyRfc() {
        Either<Resource, ResponseFormat> changeStateResult;
        Resource resource = createResourceObject();

        resource.setLifecycleState(LifecycleStateEnum.READY_FOR_CERTIFICATION);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();
        Either<Boolean, ResponseFormat> validateBeforeTransition = rfcObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
        assertTrue(validateBeforeTransition.isRight());
        changeStateResult = Either.right(validateBeforeTransition.right().value());
        assertResponse(changeStateResult, ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());

    }

    @Test
    public void testCertificationInProgress() {
        Either<Resource, ResponseFormat> changeStateResult;
        Resource resource = createResourceObject();

        resource.setLifecycleState(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();
        Either<Boolean, ResponseFormat> validateBeforeTransition = rfcObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
        assertTrue(validateBeforeTransition.isRight());
        changeStateResult = Either.right(validateBeforeTransition.right().value());
        assertResponse(changeStateResult, ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());

    }

    @Test
    public void testAlreadyCertified() {
        Either<Resource, ResponseFormat> changeStateResult;
        Resource resource = createResourceObject();

        resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();
        Either<Boolean, ResponseFormat> validateBeforeTransition = rfcObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.CERTIFIED);
        assertTrue(validateBeforeTransition.isRight());
        changeStateResult = Either.right(validateBeforeTransition.right().value());
        assertResponse(changeStateResult, ActionStatus.COMPONENT_ALREADY_CERTIFIED, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
    }

    @Test
    public void testVSPIsArchivedValidation(){
        Either<? extends Component, ResponseFormat> changeStateResult;
        Resource resource = createResourceObject();
        resource.setVspArchived(true);

        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();

        User user = new User();
        user.setUserId("cs0008");
        user.setFirstName("Carlos");
        user.setLastName("Santana");
        user.setRole(Role.TESTER.name());

        changeStateResult = rfcObj.changeState(ComponentTypeEnum.RESOURCE, resource, serviceBusinessLogic, user, owner, false, false);
        assertTrue(changeStateResult.isRight());
        changeStateResult = Either.right(changeStateResult.right().value());
        assertResponse(changeStateResult, ActionStatus.ARCHIVED_ORIGINS_FOUND, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
    }


    @Test
    public void testValidateAllResourceInstanceCertified_SuccessWithoutRI() {
        Resource resource = new Resource();
        Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified = rfcObj.validateAllResourceInstanceCertified(resource);
        assertTrue(validateAllResourceInstanceCertified.isLeft());
    }

    @Test
    public void testValidateAllResourceInstanceCertified_SuccessWithCertifiedResources() {
        Resource resource = new Resource();
        List<ComponentInstance> riList = new ArrayList<>();
        ComponentInstance ri = new ComponentInstance();
        ri.setComponentVersion("2.0");
        riList.add(ri);
        resource.setComponentInstances(riList);

        Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified = rfcObj.validateAllResourceInstanceCertified(resource);
        assertTrue(validateAllResourceInstanceCertified.isLeft());
    }

    @Test
    public void testValidateAllResourceInstanceCertified_FailWithUnCertifiedResourcesMinorVersion() {
        Resource resource = createVFWithRI("0.3");

        simulateCertifiedVersionExistForRI();

        Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified = rfcObj.validateAllResourceInstanceCertified(resource);

        assertTrue(validateAllResourceInstanceCertified.isRight());
        ResponseFormat responseFormat = validateAllResourceInstanceCertified.right().value();
        assertEquals((int) responseFormat.getStatus(), HttpStatus.SC_FORBIDDEN);
        assertEquals("SVC4559", responseFormat.getMessageId());

    }

    @Test
    public void testValidateAllResourceInstanceCertified_FailWithUnCertifiedResourcesMajorVersion() {
        Resource resource = createVFWithRI("1.3");

        simulateCertifiedVersionExistForRI();

        Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified = rfcObj.validateAllResourceInstanceCertified(resource);

        assertTrue(validateAllResourceInstanceCertified.isRight());
        ResponseFormat responseFormat = validateAllResourceInstanceCertified.right().value();
        assertEquals((int) responseFormat.getStatus(), HttpStatus.SC_FORBIDDEN);
        assertEquals("SVC4559", responseFormat.getMessageId());

    }

    @Test
    public void testDeploymentArtifactRestriction() {
        Either<? extends Component, ResponseFormat> changeStateResult;
        Service service = createServiceObject();
        service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);

        Either<User, ResponseFormat> ownerResponse = rfcObj.getComponentOwner(service, ComponentTypeEnum.SERVICE);
        assertTrue(ownerResponse.isLeft());
        User owner = ownerResponse.left().value();

        Either<Service, ResponseFormat> result = Either.left(service);
        Either<ToscaElement, StorageOperationStatus> reqCertRes = Either.left(ModelConverter.convertToToscaElement(service));
        Either<Either<ArtifactDefinition, Operation>, ResponseFormat> resultArtifacts = Either.left(Either.left(new ArtifactDefinition()));
        when(serviceBusinessLogic.generateHeatEnvArtifacts(service, owner, false, true)).thenReturn(result);
        when(serviceBusinessLogic.generateVfModuleArtifacts(service, owner, false, true)).thenReturn(result);
        when(serviceBusinessLogic.populateToscaArtifacts(service, owner, true, false, false)).thenReturn(resultArtifacts);
        when(toscaElementLifecycleOperation.requestCertificationToscaElement(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(reqCertRes);
        changeStateResult = rfcObj.changeState(ComponentTypeEnum.SERVICE, service, serviceBusinessLogic, user, owner, false, true);
        assertTrue(changeStateResult.isLeft());
    }

    private void simulateCertifiedVersionExistForRI() {
        Component dummyResource = new Resource();
        Either<Component, StorageOperationStatus> result = Either.left(dummyResource);
        Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(Either.left(dummyResource));
        Mockito.when(toscaOperationFacade.findLastCertifiedToscaElementByUUID(Mockito.any(Component.class))).thenReturn(result);
    }

    private Resource createVFWithRI(String riVersion) {
        Resource resource = new Resource();
        List<ComponentInstance> riList = new ArrayList<>();
        ComponentInstance ri = new ComponentInstance();

        ri.setComponentVersion(riVersion);
        ri.setComponentUid("someUniqueId");
        riList.add(ri);
        resource.setComponentInstances(riList);
        return resource;
    }

}