summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfigurationTest.java
blob: 905d18a9eb85b75411436d0dc988a753db5f4c27 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 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.dcaegen2.services.pmmapper.config;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.StatusCodes;
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.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

import org.onap.dcaegen2.services.pmmapper.exceptions.ReconfigurationException;
import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig;
import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
import org.onap.dcaegen2.services.pmmapper.utils.RequestSender;

import utils.ConfigUtils;
import java.util.ArrayList;
import utils.FileUtils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class DynamicConfigurationTest {
    private static final String VALID_MAPPER_CONFIG_FILE = "valid_mapper_config.json";

    private static ArrayList<Configurable> configurables;
    private DynamicConfiguration objUnderTest;
    private static String mapperConfig;
    private MapperConfig originalMapperConfig;
    private static ConfigHandler configHandler;

    @Mock
    private static RequestSender sender;

    @Mock
    private static EnvironmentConfig config;

    @BeforeAll
     static void setupBeforeAll() throws Exception {
        mapperConfig = FileUtils.getFileContents(VALID_MAPPER_CONFIG_FILE);
    }

    @BeforeEach
    void setup() throws Exception {
        configHandler = new ConfigHandler(sender, config);
        when(sender.send(any())).thenReturn(mapperConfig);
        originalMapperConfig = ConfigUtils.getMapperConfigFromFile(VALID_MAPPER_CONFIG_FILE);
        configurables = new ArrayList<>();
        objUnderTest = new DynamicConfiguration(configurables, originalMapperConfig);
    }

    @Test
    void testNoChangeResponse() throws Exception {
        originalMapperConfig = configHandler.getMapperConfig();
        objUnderTest.setConfigHandler(configHandler);
        HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
        objUnderTest.handleRequest(httpServerExchange);
        verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.OK);
        assertEquals(originalMapperConfig, objUnderTest.getOriginalConfig());
    }

    @Test
    void testApplyOriginalUponFailure() throws Exception {
        Configurable configurable = mock(Configurable.class);
        configurables.add(configurable);
        JsonObject modifiedConfig = new JsonParser().parse(mapperConfig).getAsJsonObject();
        modifiedConfig.addProperty("dmaap_dr_delete_endpoint","http://modified-delete-endpoint/1");
        when(sender.send(any())).thenReturn(modifiedConfig.toString());
        MapperConfig modifiedMapperConfig = configHandler.getMapperConfig();
        objUnderTest.setConfigHandler(configHandler);
        doAnswer(new Answer() {
            boolean failFirstReconfigure = true;
            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                if (failFirstReconfigure) {
                    failFirstReconfigure = false;
                    throw new ReconfigurationException("");
                }
                return null;
            }
        }).when(configurable).reconfigure(any());

        HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
        objUnderTest.handleRequest(httpServerExchange);
        assertEquals(originalMapperConfig, objUnderTest.getOriginalConfig());
        verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
        verify(configurable, times(1)).reconfigure(modifiedMapperConfig);
        verify(configurable, times(1)).reconfigure(originalMapperConfig);
    }

    @Test
    void testSuccessfulReconfiguration() throws Exception {
        Configurable configurable = mock(Configurable.class);
        configurables.add(configurable);
        JsonObject modifiedConfig = new JsonParser().parse(mapperConfig).getAsJsonObject();
        modifiedConfig.addProperty("dmaap_dr_delete_endpoint","http://modified-delete-endpoint/1");

        when(sender.send(any()))
                .thenReturn(modifiedConfig.toString());
        MapperConfig modifiedMapperConfig = configHandler.getMapperConfig();
        objUnderTest.setConfigHandler(configHandler);

        HttpServerExchange httpServerExchange = mock(HttpServerExchange.class, RETURNS_DEEP_STUBS);
        objUnderTest.handleRequest(httpServerExchange);
        assertEquals(modifiedMapperConfig, objUnderTest.getOriginalConfig());
        verify(httpServerExchange, times(1)).setStatusCode(StatusCodes.OK);
        verify(configurable, times(1)).reconfigure(modifiedMapperConfig);

    }

    @Test
    void testMapperConfigReconfiguration() throws Exception {
        JsonObject modifiedConfigJson = new JsonParser().parse(mapperConfig).getAsJsonObject();
        modifiedConfigJson.addProperty("dmaap_dr_delete_endpoint","http://modified-delete-endpoint/1");
        when(sender.send(any()))
                .thenReturn(modifiedConfigJson.toString());
        MapperConfig modifiedConfig = configHandler.getMapperConfig();
        originalMapperConfig.reconfigure(modifiedConfig);
        assertEquals(originalMapperConfig, modifiedConfig);
    }

    @Test
    void testMapperConfigReconfigurationNoChange() throws Exception {
        when(sender.send(any())).thenReturn(mapperConfig);
        MapperConfig inboundConfig = configHandler.getMapperConfig();
        originalMapperConfig.reconfigure(inboundConfig);
        assertEquals(originalMapperConfig, inboundConfig);
    }
}