Merge "adding catalog-service for mod2"
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / deploymentartifact / DeploymentArtifactStatusChangeHandlerTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2020 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.onap.dcaegen2.platform.mod.web.service.deploymentartifact;
22
23 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
24 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
25 import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.StatusChangeNotValidException;
26 import org.onap.dcaegen2.platform.mod.objectmothers.DeploymentArtifactObjectMother;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.jupiter.MockitoExtension;
32
33 import java.util.List;
34
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
37 import static org.mockito.Mockito.*;
38
39 @ExtendWith(MockitoExtension.class)
40 class DeploymentArtifactStatusChangeHandlerTest {
41
42     DeploymentArtifactStatusChangeHandler artifactStatusChangeHandler;
43
44     @Mock
45     DeploymentArtifactService deploymentArtifactService;
46
47     @BeforeEach
48     void setUp() {
49         artifactStatusChangeHandler = new DeploymentArtifactStatusChangeHandler();
50         artifactStatusChangeHandler.setDeploymentArtifactService(deploymentArtifactService);
51     }
52
53     @Test
54     void test_DevCompleteToNotNeeded() throws Exception{
55         //arrange
56         List<DeploymentArtifact> mockDeploymentArticats = DeploymentArtifactObjectMother.createMockDeploymentArtifactsWithDifferentStatuses(true);
57         DeploymentArtifact givenDAO = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.DEV_COMPLETE);
58         String msInstaneId = givenDAO.getMsInstanceInfo().getId();
59
60         when(deploymentArtifactService.findByMsInstanceId(msInstaneId)).thenReturn(mockDeploymentArticats);
61
62         //act
63         artifactStatusChangeHandler.handleStatusChange(DeploymentArtifactStatus.NOT_NEEDED, givenDAO);
64
65         assertThat(givenDAO.getStatus()).isEqualTo(DeploymentArtifactStatus.NOT_NEEDED);
66 //        verify(msInstanceStatusChangeHandler, times(1))
67 //                .updateStatusBasedOnDeploymentArtifactsStatuses(msInstaneId);
68
69     }
70
71     @Test
72     void test_ValidateIfArtifactWithDevCompleteStatusNotFoundForTheSameInstance() throws Exception{
73         //arrange
74         DeploymentArtifact givenDAO = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
75         String msInstaneId = givenDAO.getMsInstanceInfo().getId();
76
77         List<DeploymentArtifact> mockDeploymentArticats = DeploymentArtifactObjectMother.createMockDeploymentArtifactsWithDifferentStatuses(false);
78         when(deploymentArtifactService.findByMsInstanceId(msInstaneId)).thenReturn(mockDeploymentArticats);
79
80         //act
81         artifactStatusChangeHandler.handleStatusChange(DeploymentArtifactStatus.DEV_COMPLETE, givenDAO);
82
83         //assert
84         assertThat(givenDAO.getStatus()).isEqualTo(DeploymentArtifactStatus.DEV_COMPLETE);
85         verify(deploymentArtifactService, times(1)).
86                 findByMsInstanceId(givenDAO.getMsInstanceInfo().getId());
87 //        verify(msInstanceStatusChangeHandler, times(1))
88 //                .updateStatusBasedOnDeploymentArtifactsStatuses(msInstaneId);
89
90     }
91
92     @Test
93     void DoesntValidateIfArtifactWithDevCompleteStatusAlreadyExistsForTheSameInstance() throws Exception{
94         //arrange
95         DeploymentArtifact givenDAO = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
96         List<DeploymentArtifact> mockDeploymentArticats = DeploymentArtifactObjectMother.createMockDeploymentArtifactsWithDifferentStatuses(true);
97         when(deploymentArtifactService.findByMsInstanceId("id-123")).thenReturn(mockDeploymentArticats);
98
99         //act/assert
100         assertThatExceptionOfType(StatusChangeNotValidException.class).isThrownBy(
101                 () -> artifactStatusChangeHandler.handleStatusChange(DeploymentArtifactStatus.DEV_COMPLETE, givenDAO)
102         );
103
104     }
105
106 }