summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcaegen2/services/pmmapper/config/DynamicConfigurationTest.java
blob: c900942ecb56ab41e826d5820a193617bbc65e74 (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) 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.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import org.onap.dcaegen2.services.pmmapper.exceptions.ReconfigurationException;
import org.onap.dcaegen2.services.pmmapper.model.EnvironmentConfig;
import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
import org.onap.dcaegen2.services.pmmapper.utils.RequestSender;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

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;

@RunWith(PowerMockRunner.class)
@PrepareForTest({DynamicConfiguration.class, EnvironmentConfig.class})
public class DynamicConfigurationTest {
    private static Path VALID_CONFIG_PATH = Paths.get("src/test/resources/valid_mapper_config.json");

    private static ArrayList<Configurable> configurables;
    private DynamicConfiguration objUnderTest;
    private static String config;
    private MapperConfig originalMapperConfig;

    @Mock
    private RequestSender sender;

    @BeforeClass()
    public static void setupBeforeClass() throws Exception {
        config = new String(Files.readAllBytes(VALID_CONFIG_PATH));
    }

    @Before
    public void setup() throws Exception {
        configurables = new ArrayList<>();
        PowerMockito.mockStatic(EnvironmentConfig.class);
        PowerMockito.when(EnvironmentConfig.getCBSHostName()).thenReturn("");
        PowerMockito.when(EnvironmentConfig.getCBSPort()).thenReturn(1);
        PowerMockito.when(EnvironmentConfig.getServiceName()).thenReturn("");

        when(sender.send(any())).thenReturn(config);
        ConfigHandler configHandler = new ConfigHandler(sender);
        originalMapperConfig = configHandler.getMapperConfig();
        objUnderTest = new DynamicConfiguration(configurables, originalMapperConfig);
    }

    @Test
    public void testNoChangeResponse() throws Exception {
        ConfigHandler configHandler = new ConfigHandler(sender);
        originalMapperConfig = configHandler.getMapperConfig();
        objUnderTest.setConfigHandler(configHandler);

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

    @Test
    public void testApplyOriginalUponFailure() throws Exception {
        ConfigHandler configHandler = new ConfigHandler(sender);
        Configurable configurable = mock(Configurable.class);
        configurables.add(configurable);
        JsonObject modifiedConfig = new JsonParser().parse(config).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
    public void testSuccessfulReconfiguration() throws Exception {
        ConfigHandler configHandler = new ConfigHandler(sender);
        Configurable configurable = mock(Configurable.class);
        configurables.add(configurable);
        JsonObject modifiedConfig = new JsonParser().parse(config).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
    public void testMapperConfigReconfiguration() throws Exception {
        ConfigHandler configHandler = new ConfigHandler(sender);
        JsonObject modifiedConfigJson = new JsonParser().parse(config).getAsJsonObject();
        modifiedConfigJson.addProperty("dmaap_dr_delete_endpoint","http://modified-delete-endpoint/1");
        String newConfig =  modifiedConfigJson.toString();

        when(sender.send(any())).thenReturn(config,newConfig);

        MapperConfig originalConfig = configHandler.getMapperConfig();
        MapperConfig modifiedConfig = configHandler.getMapperConfig();

        originalConfig.reconfigure(modifiedConfig);
        assertEquals(originalConfig, modifiedConfig);
    }
}