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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.pnfsimulator.simulatorconfig;
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;
29 import java.net.MalformedURLException;
31 import java.util.List;
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;
39 class SimulatorConfigServiceTest {
41 private static final String SAMPLE_ID = "sampleId";
42 private static final String SAMPLE_NEW_VES_URL = "http://localhost:8090/eventListener/v7";
44 private SimulatorConfigRepository repository;
47 private SimulatorConfigService service;
55 void testShouldReturnConfiguration() throws MalformedURLException {
56 List<SimulatorConfig> expectedConfig = getExpectedConfig();
57 when(repository.findAll()).thenReturn(expectedConfig);
59 SimulatorConfig configs = service.getConfiguration();
61 assertThat(configs).isNotNull();
65 void testShouldRaiseExceptionWhenNoConfigurationPresent() {
66 when(repository.findAll()).thenReturn(Lists.emptyList());
68 assertThatThrownBy(() -> service.getConfiguration())
69 .isInstanceOf(IllegalStateException.class)
70 .hasMessageContaining("No configuration found in db");
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();
79 when(repository.findAll()).thenReturn(expectedConfig);
80 when(repository.save(any(SimulatorConfig.class))).thenReturn(configWithUpdates);
82 SimulatorConfig updatedConfig = service.updateConfiguration(configWithUpdates);
84 assertThat(updatedConfig).isEqualToComparingFieldByField(configWithUpdates);
88 void testShouldRaiseExceptionWhenNoConfigInDbPresentOnUpdate() throws MalformedURLException {
89 when(repository.findAll()).thenReturn(Lists.emptyList());
91 SimulatorConfig configWithUpdates = new SimulatorConfig(SAMPLE_ID, new URL(SAMPLE_NEW_VES_URL));
93 assertThatThrownBy(() -> service.updateConfiguration(configWithUpdates))
94 .isInstanceOf(IllegalStateException.class)
95 .hasMessageContaining("No configuration found in db");
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);