Distributing Blueprint to DCAE Dashboard Issue-ID: DCAEGEN2-2385>
[dcaegen2/platform.git] / mod2 / catalog-service / src / test / java / org / onap / dcaegen2 / platform / mod / web / service / BlueprintDistributionServiceImplImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  org.onap.dcae
4  *  ================================================================================
5  *  Copyright (c) 2021 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;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.times;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.onap.dcaegen2.platform.mod.objectmothers.BlueprintDistributionObjectMother.BP_DISTRIBUTION_ENV;
29 import static org.onap.dcaegen2.platform.mod.objectmothers.BlueprintDistributionObjectMother.BP_DISTRIBUTION_MODEL_ID;
30 import static org.onap.dcaegen2.platform.mod.objectmothers.BlueprintDistributionObjectMother.BP_DISTRIBUTION_PWD;
31 import static org.onap.dcaegen2.platform.mod.objectmothers.BlueprintDistributionObjectMother.BP_DISTRIBUTION_USER;
32
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.Spy;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
40 import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
41 import org.onap.dcaegen2.platform.mod.objectmothers.DeploymentArtifactObjectMother;
42 import org.onap.dcaegen2.platform.mod.util.BlueprintDistributionUtils;
43 import org.onap.dcaegen2.platform.mod.web.service.blueprintdistributionservice.BlueprintDistributionServiceImpl;
44 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactGateway;
45 import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactServiceImpl;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.web.client.RestTemplate;
50
51 @ExtendWith(MockitoExtension.class)
52 class BlueprintDistributionServiceImplImplTest {
53
54     @Spy
55     private BlueprintDistributionServiceImpl mockBlueprintDistributionServiceImpl = new BlueprintDistributionServiceImpl();
56
57     @Mock
58     private DeploymentArtifactServiceImpl deploymentArtifactService;
59
60     @Mock
61     private BlueprintDistributionUtils blueprintDistributionUtils;
62
63     @Mock
64     private DeploymentArtifactGateway deploymentArtifactGateway;
65
66     @Mock
67     private RestTemplate restTemplate;
68
69     DeploymentArtifact deploymentArtifact;
70
71
72
73     @BeforeEach
74     void initialize(){
75         mockBlueprintDistributionServiceImpl.setDeploymentArtifactService(deploymentArtifactService);
76         mockBlueprintDistributionServiceImpl.setDeploymentArtifactGateway(deploymentArtifactGateway);
77         mockBlueprintDistributionServiceImpl.setBlueprintDistributionUtils(blueprintDistributionUtils);
78         mockBlueprintDistributionServiceImpl.setRestTemplate(restTemplate);
79         deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
80     }
81
82
83     @Test
84     void test_distributeBlueprintReturnSucess() {
85
86         when(deploymentArtifactService.findDeploymentArtifactById(BP_DISTRIBUTION_MODEL_ID)).thenReturn(deploymentArtifact);
87         when(blueprintDistributionUtils.getBlueprintDashboardURL(BP_DISTRIBUTION_ENV)).thenReturn("/url");
88         when(blueprintDistributionUtils.getBlueprintDashboardUserName(BP_DISTRIBUTION_ENV)).thenReturn(BP_DISTRIBUTION_USER);
89         when(blueprintDistributionUtils.getBlueprintDashboardPassword(BP_DISTRIBUTION_ENV)).thenReturn(BP_DISTRIBUTION_PWD);
90
91         ResponseEntity expected = mockBlueprintDistributionServiceImpl.distributeBlueprint(BP_DISTRIBUTION_MODEL_ID,BP_DISTRIBUTION_ENV);
92
93         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.OK);
94         verify(deploymentArtifactService, times(1)).findDeploymentArtifactById(BP_DISTRIBUTION_MODEL_ID);
95         verify(blueprintDistributionUtils, times(2)).getBlueprintDashboardURL(BP_DISTRIBUTION_ENV);
96         verify(blueprintDistributionUtils, times(1)).getBlueprintDashboardUserName(BP_DISTRIBUTION_ENV);
97         verify(blueprintDistributionUtils, times(1)).getBlueprintDashboardPassword(BP_DISTRIBUTION_ENV);
98         verify(deploymentArtifactGateway, times(1)).save(any());
99
100     }
101
102     @Test
103     void test_distributeBlueprintReturnBadRequest() {
104
105         when(deploymentArtifactService.findDeploymentArtifactById(BP_DISTRIBUTION_MODEL_ID)).thenReturn(deploymentArtifact);
106         when(blueprintDistributionUtils.getBlueprintDashboardURL(BP_DISTRIBUTION_ENV)).thenReturn("/url");
107         when(blueprintDistributionUtils.getBlueprintDashboardUserName(BP_DISTRIBUTION_ENV)).thenReturn(BP_DISTRIBUTION_USER);
108         when(blueprintDistributionUtils.getBlueprintDashboardPassword(BP_DISTRIBUTION_ENV)).thenReturn(BP_DISTRIBUTION_PWD);
109         when(restTemplate.postForObject(blueprintDistributionUtils.getBlueprintDashboardURL(BP_DISTRIBUTION_ENV),
110             HttpEntity.class,String.class)).thenReturn("Bad request");
111
112         ResponseEntity expected = mockBlueprintDistributionServiceImpl.distributeBlueprint(BP_DISTRIBUTION_MODEL_ID,BP_DISTRIBUTION_ENV);
113
114         assertThat(expected.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
115         verify(deploymentArtifactService, times(1)).findDeploymentArtifactById(BP_DISTRIBUTION_MODEL_ID);
116         verify(blueprintDistributionUtils, times(3)).getBlueprintDashboardURL(BP_DISTRIBUTION_ENV);
117         verify(blueprintDistributionUtils, times(1)).getBlueprintDashboardUserName(BP_DISTRIBUTION_ENV);
118         verify(blueprintDistributionUtils, times(1)).getBlueprintDashboardPassword(BP_DISTRIBUTION_ENV);
119         verify(deploymentArtifactGateway, times(1)).save(any());
120
121     }
122
123
124
125 }