aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-acelement/src/test/java/org/onap/policy/clamp/acm/element/rest/AcElementControllerTest.java
blob: c40cdeabc05e3f99a1c941dd6f420a10268cb3b6 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.acm.element.rest;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.ws.rs.core.Response;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.onap.policy.clamp.acm.element.main.parameters.AcElement;
import org.onap.policy.clamp.acm.element.main.rest.AcElementController;
import org.onap.policy.clamp.acm.element.service.ConfigService;
import org.onap.policy.clamp.common.acm.exception.AutomationCompositionRuntimeException;
import org.onap.policy.clamp.models.acm.messages.rest.element.ElementConfig;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


@ExtendWith(SpringExtension.class)
@WebMvcTest(value = AcElementController.class)
@EnableConfigurationProperties(value = AcElement.class)
class AcElementControllerTest {

    private static final Coder CODER = new StandardCoder();
    private static final String ELEMENT_CONFIG_YAML = "src/test/resources/config.json";
    private static final String RETRIEVE_CONFIG = "/v2/config";
    private static final String ACTIVATE_CONFIG = "/v2/activate";
    private static final String DEACTIVATE_CONFIG = "/v2/deactivate";
    private static ElementConfig config;


    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private ConfigService configService;

    @Autowired
    private WebApplicationContext context;

    /**
     * Read input element config json.
     * @throws CoderException in case of error.
     */
    @BeforeAll
    static void setupParams() throws CoderException {
        config = CODER.decode(new File(ELEMENT_CONFIG_YAML), ElementConfig.class);
    }

    /**
     * Mock service layer in Controller.
     */
    @BeforeEach
    void mockServiceClass() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
        when(configService.getElementConfig()).thenReturn(config);
    }

    /**
     * Test endpoint for retrieving element config.
     * @throws Exception in case of error.
     */
    @Test
    void retrieveElementConfig() throws Exception {
        var requestBuilder = MockMvcRequestBuilders.get(RETRIEVE_CONFIG)
                .accept(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isOk())
                .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
                .andExpect(jsonPath("$.elementType", is("STARTER")));
    }

    /**
     * Test endpoint for activating element config.
     * @throws Exception in case of error.
     */
    @Test
    void activateConfig() throws Exception {
        //Mocking successful activation of element config
        doNothing().when(configService).activateElement(config);

        var requestBuilder = MockMvcRequestBuilders.post(ACTIVATE_CONFIG)
                .accept(MediaType.APPLICATION_JSON_VALUE)
                .content(getElementConfigJson())
                .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isCreated());

        doThrow(new AutomationCompositionRuntimeException(Response.Status.CONFLICT, "service manager already running"))
                .when(configService).activateElement(any());

        //Activate Invalid config, expects HTTP status CONFLICT
        requestBuilder = MockMvcRequestBuilders.post(ACTIVATE_CONFIG).accept(MediaType.APPLICATION_JSON_VALUE)
                .content(getInvalidJson())
                .contentType(MediaType.APPLICATION_JSON_VALUE);

        mockMvc.perform(requestBuilder).andExpect(status().isConflict())
                .andExpect(result -> assertEquals("service manager already running",
                        result.getResolvedException().getMessage()));
    }

    /**
     * Test endpoint for deactivating element config.
     * @throws Exception in case of error.
     */
    @Test
    void deActivateConfig() throws Exception {

        //Mocking successful deactivation of element config
        doNothing().when(configService).deleteConfig();

        var requestBuilder = MockMvcRequestBuilders.delete(DEACTIVATE_CONFIG);

        mockMvc.perform(requestBuilder).andExpect(status().isNoContent());
    }

    private String getInvalidJson() {
        return new JSONObject().toString();
    }

    private String getElementConfigJson() throws IOException {
        return FileUtils.readFileToString(new File(ELEMENT_CONFIG_YAML), StandardCharsets.UTF_8);
    }

}