4ed0972890d3d77802433213bd8c32daab969e03
[integration.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulatorconfig;
22
23 import org.assertj.core.util.Lists;
24 import org.junit.jupiter.api.BeforeEach;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.util.List;
32
33 import static org.assertj.core.api.Java6Assertions.assertThat;
34 import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.Mockito.when;
37 import static org.mockito.MockitoAnnotations.initMocks;
38
39 class SimulatorConfigServiceTest {
40
41     private static final String SAMPLE_ID = "sampleId";
42     private static final String SAMPLE_NEW_VES_URL = "http://localhost:8090/eventListener/v7";
43     @Mock
44     private SimulatorConfigRepository repository;
45
46     @InjectMocks
47     private SimulatorConfigService service;
48
49     @BeforeEach
50     void resetMocks() {
51         initMocks(this);
52     }
53
54     @Test
55     void testShouldReturnConfiguration() throws MalformedURLException {
56         List<SimulatorConfig> expectedConfig = getExpectedConfig();
57         when(repository.findAll()).thenReturn(expectedConfig);
58
59         SimulatorConfig configs = service.getConfiguration();
60
61         assertThat(configs).isNotNull();
62     }
63
64     @Test
65     void testShouldRaiseExceptionWhenNoConfigurationPresent() {
66         when(repository.findAll()).thenReturn(Lists.emptyList());
67
68         assertThatThrownBy(() -> service.getConfiguration())
69                 .isInstanceOf(IllegalStateException.class)
70                 .hasMessageContaining("No configuration found in db");
71     }
72
73     @Test
74     void testShouldUpdateConfigurationWithVesUrl() throws MalformedURLException {
75         URL updatedUrl = new URL("http://localhost:8090/listener/v8");
76         SimulatorConfig configWithUpdates = new SimulatorConfig("sampleId", updatedUrl);
77         List<SimulatorConfig> expectedConfig = getExpectedConfig();
78
79         when(repository.findAll()).thenReturn(expectedConfig);
80         when(repository.save(any(SimulatorConfig.class))).thenReturn(configWithUpdates);
81
82         SimulatorConfig updatedConfig = service.updateConfiguration(configWithUpdates);
83
84         assertThat(updatedConfig).isEqualToComparingFieldByField(configWithUpdates);
85     }
86
87     @Test
88     void testShouldRaiseExceptionWhenNoConfigInDbPresentOnUpdate() throws MalformedURLException {
89         when(repository.findAll()).thenReturn(Lists.emptyList());
90
91         SimulatorConfig configWithUpdates = new SimulatorConfig(SAMPLE_ID, new URL(SAMPLE_NEW_VES_URL));
92
93         assertThatThrownBy(() -> service.updateConfiguration(configWithUpdates))
94                 .isInstanceOf(IllegalStateException.class)
95                 .hasMessageContaining("No configuration found in db");
96     }
97
98     private List<SimulatorConfig> getExpectedConfig() throws MalformedURLException {
99         URL sampleVesUrl = new URL("http://localhost:8080/eventListener/v7");
100         SimulatorConfig config = new SimulatorConfig(SAMPLE_ID, sampleVesUrl);
101         return Lists.newArrayList(config);
102     }
103
104 }