summaryrefslogtreecommitdiffstats
path: root/mod2/catalog-service/src/test/java/org/onap/dcaegen2/platform/mod/web/DeploymentArtifactControllerTest.java
blob: c8942b15318ddb60a9dd0a743d779188f3749e25 (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
/*
 * ============LICENSE_START=======================================================
 *  org.onap.dcae
 *  ================================================================================
 *  Copyright (c) 2020 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.onap.dcaegen2.platform.mod.web;

import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifact;
import org.onap.dcaegen2.platform.mod.model.deploymentartifact.DeploymentArtifactStatus;
import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.BlueprintFileNameCreateException;
import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.DeploymentArtifactNotFound;
import org.onap.dcaegen2.platform.mod.model.restapi.DeploymentArtifactPatchRequest;
import org.onap.dcaegen2.platform.mod.objectmothers.BaseMsObjectMother;
import org.onap.dcaegen2.platform.mod.objectmothers.DeploymentArtifactObjectMother;
import org.onap.dcaegen2.platform.mod.objectmothers.MsInstanceObjectMother;
import org.onap.dcaegen2.platform.mod.web.controller.DeploymentArtifactController;
import org.onap.dcaegen2.platform.mod.web.service.deploymentartifact.DeploymentArtifactService;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.Arrays;
import java.util.List;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;

@WebMvcTest(DeploymentArtifactController.class)
class DeploymentArtifactControllerTest {

    @Autowired
    MockMvc mockMvc;

    @MockBean
    DeploymentArtifactService service;

    @BeforeEach
    void setUp() {
    }

    @Test
    void test_GenerateDeploymentArtifactEndpoint_returnsBlueprint() throws Exception{
        String url = String.format("/api/deployment-artifact/%s?user=%s", MsInstanceObjectMother.MS_INSTANCE_ID, MsInstanceObjectMother.USER);
        DeploymentArtifact response = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);

        Mockito.when(service.generateDeploymentArtifact(MsInstanceObjectMother.MS_INSTANCE_ID, MsInstanceObjectMother.USER)).thenReturn(response);

        mockMvc.perform(post(url))
                    .andExpect(MockMvcResultMatchers.status().isCreated())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.fileName").exists())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.content").exists());

        Mockito.verify(service, Mockito.times(1)).generateDeploymentArtifact(MsInstanceObjectMother.MS_INSTANCE_ID, MsInstanceObjectMother.USER);
    }

    @Test
    void test_RaiseExceptionIfBlueprintNameCanNotBeCreated() throws Exception{
        String url = String.format("/api/deployment-artifact/%s?user=%s", MsInstanceObjectMother.MS_INSTANCE_ID,
                MsInstanceObjectMother.USER);
        DeploymentArtifact response = DeploymentArtifactObjectMother.createDeploymentArtifactDAO(DeploymentArtifactStatus.IN_DEV);

        Mockito.when(service.generateDeploymentArtifact(MsInstanceObjectMother.MS_INSTANCE_ID, MsInstanceObjectMother.USER)).thenThrow(new BlueprintFileNameCreateException(""));

        mockMvc.perform(post(url))
                .andExpect(MockMvcResultMatchers.status().is4xxClientError());
    }

    @Test
    void test_GetAllDeploymentArtifactsShouldReturnList() throws Exception{
        List<DeploymentArtifact> daos = createDaos();
        Mockito.when(service.getAllDeploymentArtifacts()).thenReturn(daos);

        mockMvc.perform(get("/api/deployment-artifact"))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)));

    }

    private List<DeploymentArtifact> createDaos() {
        DeploymentArtifact dao1 = new DeploymentArtifact();
        dao1.setId("123");
        DeploymentArtifact dao2 = new DeploymentArtifact();
        dao2.setId("456");

        return Arrays.asList(dao1, dao2);
    }

    @Test
    void test_GetAllDeploymentArtifactTestShouldReturnAList() throws Exception{

        mockMvc.perform(get(DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL + DeploymentArtifactController.GET_STATUSES))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(DeploymentArtifactStatus.values().length)));
    }

    @Test
    void test_ifUserIsNullRaiseException() throws Exception{
        String id = "id-123";
        String user = "";

        mockMvc.perform(patch(DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL + "/" + id + "?user=" + user)
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(new DeploymentArtifactPatchRequest())))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }

    @Test
    void test_ChangeStatusOfDeploymentArtifact() throws Exception{

        String id = "id-123";
        String user = "user1";
        DeploymentArtifactPatchRequest partialDto = new DeploymentArtifactPatchRequest();
        partialDto.setStatus(DeploymentArtifactStatus.DEV_COMPLETE);

        mockMvc.perform(patch(DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL + "/" + id + "?user=" + user)
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(partialDto)))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.message").exists());

        Mockito.verify(service, Mockito.times(1)).updateDeploymentArtifact(id, partialDto, user);

    }

    @Test
    void test_deploymentArtifactIdNotFound() throws Exception{
        String wrongId = "wrong-id";
        DeploymentArtifactPatchRequest partialDto = new DeploymentArtifactPatchRequest();
        partialDto.setStatus(DeploymentArtifactStatus.DEV_COMPLETE);

        Mockito.doThrow(new DeploymentArtifactNotFound("")).when(service).
                updateDeploymentArtifact(wrongId, partialDto, "user-1");

        mockMvc.perform(patch(DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL + "/" + wrongId + "?user=" + "user-1")
                .contentType(MediaType.APPLICATION_JSON)
                .content(BaseMsObjectMother.asJsonString(partialDto)))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }

    @Test
    void test_deleteDeploymentArtifactEndpoint() throws Exception{
        String deploymentArtifactId = "id-123";
        String user = "user-1";
        mockMvc.perform(delete(DeploymentArtifactController.DEPLOYMENT_ARTIFACTS_BASE_URL + "/" + deploymentArtifactId + "?user=" + user))
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.message").exists());
        Mockito.verify(service, Mockito.times(1)).deleteDeploymentArtifact(deploymentArtifactId);
    }
}