2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.platform.mod.web.service;
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;
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;
51 @ExtendWith(MockitoExtension.class)
52 class BlueprintDistributionServiceImplImplTest {
55 private BlueprintDistributionServiceImpl mockBlueprintDistributionServiceImpl = new BlueprintDistributionServiceImpl();
58 private DeploymentArtifactServiceImpl deploymentArtifactService;
61 private BlueprintDistributionUtils blueprintDistributionUtils;
64 private DeploymentArtifactGateway deploymentArtifactGateway;
67 private RestTemplate restTemplate;
69 DeploymentArtifact deploymentArtifact;
75 mockBlueprintDistributionServiceImpl.setDeploymentArtifactService(deploymentArtifactService);
76 mockBlueprintDistributionServiceImpl.setDeploymentArtifactGateway(deploymentArtifactGateway);
77 mockBlueprintDistributionServiceImpl.setBlueprintDistributionUtils(blueprintDistributionUtils);
78 mockBlueprintDistributionServiceImpl.setRestTemplate(restTemplate);
79 deploymentArtifact = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);
84 void test_distributeBlueprintReturnSucess() {
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);
91 ResponseEntity expected = mockBlueprintDistributionServiceImpl.distributeBlueprint(BP_DISTRIBUTION_MODEL_ID,BP_DISTRIBUTION_ENV);
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());
103 void test_distributeBlueprintReturnBadRequest() {
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");
112 ResponseEntity expected = mockBlueprintDistributionServiceImpl.distributeBlueprint(BP_DISTRIBUTION_MODEL_ID,BP_DISTRIBUTION_ENV);
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());