diff options
author | JoeOLeary <joseph.o.leary@est.tech> | 2019-08-21 11:48:37 +0000 |
---|---|---|
committer | JoeOLeary <joseph.o.leary@est.tech> | 2019-08-21 11:48:37 +0000 |
commit | e86ac5c697f8415552e52edf8f8ef834feb53079 (patch) | |
tree | b691eb6ed6e1abaf34520a094139cde2e5d77b2a /src/test/java/org/onap | |
parent | d7844f2ef5830aaa1b0d6cec51e49bcce484d7c9 (diff) |
Reduce technical debt
* Improve code coverage
* Remove sonar smells
Issue-ID: DCAEGEN2-1731
Change-Id: Iefc7c18dc9daf1d60a498db4c4c5660d8acca779
Signed-off-by: JoeOLeary <joseph.o.leary@est.tech>
Diffstat (limited to 'src/test/java/org/onap')
7 files changed, 103 insertions, 47 deletions
diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java index ef508f3..1772452 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java @@ -18,11 +18,12 @@ * ============LICENSE_END========================================================= */ package org.onap.dcaegen2.pmmapper.messagerouter; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; + +import org.onap.dcaegen2.services.pmmapper.exceptions.RequestFailure; import reactor.test.StepVerifier; import java.util.Arrays; import java.util.List; @@ -31,7 +32,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.onap.dcaegen2.services.pmmapper.exceptions.MRPublisherException; import org.onap.dcaegen2.services.pmmapper.messagerouter.VESPublisher; import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig; import org.onap.dcaegen2.services.pmmapper.model.Event; @@ -79,7 +79,7 @@ public class VESPublisherTest { Event event = mock(Event.class); List<Event> events = Arrays.asList(event,event,event); when(event.getVes()).thenReturn(ves); - when(sender.send("POST",topicURL,ves,"base64encoded")).thenThrow(Exception.class); + when(sender.send("POST",topicURL,ves,"base64encoded")).thenThrow(RequestFailure.class); Flux<Event> flux = sut.publish(events); diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java index 11215b3..46994b3 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/AppTest.java @@ -20,12 +20,17 @@ package org.onap.dcaegen2.services.pmmapper; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpResponse.response; +import static utils.ConfigUtils.getMapperConfigFromFile; import java.io.IOException; import java.nio.file.Files; @@ -37,6 +42,12 @@ import java.util.List; import com.google.gson.Gson; import io.undertow.server.HttpServerExchange; import io.undertow.util.StatusCodes; +import org.junit.jupiter.api.BeforeEach; +import org.onap.dcaegen2.services.pmmapper.config.ConfigHandler; +import org.onap.dcaegen2.services.pmmapper.exceptions.CBSServerError; +import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException; +import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException; + import org.onap.dcaegen2.services.pmmapper.utils.XMLValidator; import reactor.core.publisher.Flux; @@ -66,23 +77,76 @@ class AppTest { static MockServerClient client; private static EventMetadata eventMetadata; + private static MapperConfig mapperConfig; + private static ConfigHandler configHandler; private static final Path dataDirectory = Paths.get("src/test/resources/mapper_test/mapping_data/"); private static final Path metadata = Paths.get("src/test/resources/valid_metadata.json"); + private static final Path template = Paths.get("src/main/resources/mapping.ftl"); private static final Path schema = Paths.get("src/main/resources/measCollec_plusString.xsd"); + private static final String config = "valid_mapper_config.json"; + + private App objUnderTest; @BeforeAll - public static void setup() { + static void setup() { mockServer = startClientAndServer(35454); client = new MockServerClient("127.0.0.1", 35454); + mapperConfig = getMapperConfigFromFile(config); } @AfterAll - public static void teardown() { + static void teardown() { mockServer.stop(); } + @BeforeEach + void beforeEach() { + configHandler = mock(ConfigHandler.class); + } + + @Test + void testDisabledHTTPServer() throws Exception { + + MapperConfig mockConfig = Mockito.spy(mapperConfig); + when(mockConfig.getEnableHttp()).thenReturn(false); + when(configHandler.getMapperConfig()).thenReturn(mockConfig); + objUnderTest = new App(template, schema, 0, 0, configHandler); + objUnderTest.start(); + assertEquals(1, objUnderTest.getApplicationServer().getListenerInfo().size()); + assertEquals("https", objUnderTest.getApplicationServer().getListenerInfo().get(0).getProtcol()); + objUnderTest.stop(); + } + + @Test + void testEnabledHTTPServer() throws Exception { + MapperConfig mockConfig = Mockito.spy(mapperConfig); + when(mockConfig.getEnableHttp()).thenReturn(true); + when(configHandler.getMapperConfig()).thenReturn(mockConfig); + objUnderTest = new App(template, schema, 0, 0, configHandler); + objUnderTest.start(); + assertEquals(2, objUnderTest.getApplicationServer().getListenerInfo().size()); + assertEquals("http", objUnderTest.getApplicationServer().getListenerInfo().get(0).getProtcol()); + objUnderTest.stop(); + } + + @Test + void testConfigFailure() throws EnvironmentConfigException, CBSServerError, MapperConfigException { + when(configHandler.getMapperConfig()).thenThrow(MapperConfigException.class); + assertThrows(IllegalStateException.class, () -> new App(template, schema, 0, 0, configHandler)); + + } + + @Test + void testServerCreationFailure() throws EnvironmentConfigException, CBSServerError, MapperConfigException { + MapperConfig mockConfig = Mockito.spy(mapperConfig); + when(mockConfig.getKeyStorePath()).thenReturn("not_a_file"); + when(configHandler.getMapperConfig()).thenReturn(mockConfig); + assertThrows(IllegalStateException.class, () -> new App(template, schema, 0, 0, configHandler)); + + } + @Test void testHandleBackPressureNullValue() { assertThrows(NullPointerException.class, () -> App.handleBackPressure(null)); @@ -110,7 +174,7 @@ class AppTest { } @Test - public void testFilterByFileType_success() { + void testFilterByFileType_success() { Event mockEvent = Mockito.mock(Event.class); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -123,7 +187,7 @@ class AppTest { } @Test - public void testFilterByFileType_NonXML() { + void testFilterByFileType_NonXML() { Event mockEvent = Mockito.mock(Event.class); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -136,7 +200,7 @@ class AppTest { } @Test - public void testFilterByFileType_throwException() { + void testFilterByFileType_throwException() { Event mockEvent = Mockito.mock(Event.class); MeasFilterHandler mockFilter = Mockito.mock(MeasFilterHandler.class); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -148,7 +212,7 @@ class AppTest { } @Test - public void testValidateXML_success() throws IOException { + void testValidateXML_success() throws IOException { XMLValidator mockValidator = new XMLValidator(schema); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -164,7 +228,7 @@ class AppTest { } @Test - public void testValidateXML_failure() throws IOException { + void testValidateXML_failure() throws IOException { XMLValidator mockValidator = new XMLValidator(schema); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -180,7 +244,7 @@ class AppTest { } @Test - public void testValidateXML_throwException() { + void testValidateXML_throwException() { Event mockEvent = Mockito.mock(Event.class); XMLValidator mockValidator = Mockito.mock(XMLValidator.class); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -192,7 +256,7 @@ class AppTest { } @Test - public void testFilter_success() { + void testFilter_success() { Event mockEvent = Mockito.mock(Event.class); List<Event> mockEvents = Arrays.asList(mockEvent); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); @@ -201,7 +265,7 @@ class AppTest { } @Test - public void testFilter_throwException() { + void testFilter_throwException() { HttpRequest req = HttpRequest.request(); client.when(req).respond( response().withStatusCode(200)); @@ -222,36 +286,33 @@ class AppTest { } @Test - public void testSplit_empty_success() { + void testSplit_empty_success() { Event mockEvent = Mockito.mock(Event.class); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); MeasConverter mockMeasConverter = Mockito.mock(MeasConverter.class); Flux<List<Event>> splitResult = App.split(new MeasSplitter(mockMeasConverter), mockEvent, mockConfig); - splitResult.equals(Flux.<List<Event>>empty()); + assertEquals(splitResult, Flux.<List<Event>>empty()); } @Test - public void testSplit_success() { + void testSplit_success() { Event mockEvent = Mockito.mock(Event.class); List<Event> mockEvents = Arrays.asList(mockEvent,mockEvent); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); MeasSplitter mockSplitter = Mockito.mock(MeasSplitter.class); Mockito.when(mockSplitter.split(mockEvent)).thenReturn(mockEvents); - Flux<List<Event>> splitResult = App.split(mockSplitter, mockEvent, mockConfig); - - splitResult.equals(Flux.just(mockEvents)); + assertEquals(splitResult.toString(), Flux.just(mockEvents).toString()); } @Test - public void testMapping_empty_success() { + void testMapping_empty_success() { Event mockEvent = Mockito.mock(Event.class); MeasConverter mockMeasConverter = Mockito.mock(MeasConverter.class); List<Event> mockEvents = Arrays.asList(mockEvent); MapperConfig mockConfig = Mockito.mock(MapperConfig.class); Path mappingTemplate = Paths.get("src/main/resources/mapping.ftl"); Flux<List<Event>> mappingResult = App.map(new Mapper(mappingTemplate,mockMeasConverter), mockEvents, mockConfig); - mappingResult.equals(Flux.<List<Event>>empty()); + assertEquals(mappingResult, Flux.<List<Event>>empty()); } - } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java index 92d2c93..2cbfffa 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/config/ConfigHandlerTests.java @@ -45,6 +45,7 @@ import org.onap.dcaegen2.services.pmmapper.exceptions.CBSConfigException; import org.onap.dcaegen2.services.pmmapper.exceptions.CBSServerError; import org.onap.dcaegen2.services.pmmapper.exceptions.EnvironmentConfigException; import org.onap.dcaegen2.services.pmmapper.exceptions.MapperConfigException; +import org.onap.dcaegen2.services.pmmapper.exceptions.RequestFailure; import org.onap.dcaegen2.services.pmmapper.utils.EnvironmentConfig; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; import org.onap.dcaegen2.services.pmmapper.utils.RequestSender; @@ -99,6 +100,9 @@ class ConfigHandlerTests { MapperConfig actualConfig = getMapperConfig(); JsonObject expectedConfigJson = gson.fromJson(validMapperConfig, JsonObject.class); MapperConfig expectedConfig = gson.fromJson(expectedConfigJson, MapperConfig.class); + assertEquals(expectedConfig.getPublisherTopicUrl(), actualConfig.getPublisherTopicUrl()); + assertEquals(expectedConfig.getPublisherUserName(), actualConfig.getPublisherUserName()); + assertEquals(expectedConfig.getPublisherPassword(), actualConfig.getPublisherPassword()); assertEquals(expectedConfig, actualConfig); assertTrue(logAppender.list.get(1).getMessage().contains("Received pm-mapper configuration from ConfigBinding Service")); logAppender.stop(); @@ -106,7 +110,7 @@ class ConfigHandlerTests { @Test void configbinding_server_error() throws Exception { - when(sender.send(anyString())).thenThrow(CBSServerError.class); + when(sender.send(anyString())).thenThrow(RequestFailure.class); assertThrows(CBSServerError.class, this::getMapperConfig); } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java index 031a3c8..34b71f4 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java @@ -213,9 +213,7 @@ class MeasFilterHandlerTest { when(exchange.getRequestPath()) .thenReturn(invalidFiletypes.toString()); - invalidFiletypes.forEach(c -> { - assertFalse(objUnderTest.filterByFileType(event)); - }); + invalidFiletypes.forEach(c -> assertFalse(objUnderTest.filterByFileType(event))); } @@ -227,7 +225,7 @@ class MeasFilterHandlerTest { private Event generateEvent(String inputPath, Filter filter) { String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml")); - Event event = new Event(exchange, inputXml, metaData, new HashMap<String, String>(), ""); + Event event = new Event(exchange, inputXml, metaData, new HashMap<>(), ""); event.setMeasCollecFile(converter.convert(inputXml)); event.setFilter(filter); return event; diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java index 7a8602e..f623d57 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/mapping/MapperTest.java @@ -20,7 +20,7 @@ package org.onap.dcaegen2.services.pmmapper.mapping; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; @@ -37,7 +37,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.List; import org.everit.json.schema.Schema; @@ -49,15 +48,12 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; -import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.onap.dcaegen2.services.pmmapper.exceptions.MappingException; -import org.onap.dcaegen2.services.pmmapper.exceptions.XMLParseException; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; import org.onap.dcaegen2.services.pmmapper.model.MeasCollecFile; import org.onap.dcaegen2.services.pmmapper.utils.MeasConverter; -import org.onap.logging.ref.slf4j.ONAPLogAdapter; import org.powermock.reflect.Whitebox; import utils.EventUtils; @@ -141,9 +137,9 @@ class MapperTest { void testMapEvents() throws IOException { List<Event> events = getValidEvents(); List<Event> expectedEvents = objUnderTest.mapEvents(events); - expectedEvents.forEach(event->{ + expectedEvents.forEach(event -> { when(converter.convert(any(MeasCollecFile.class))).thenReturn(event.getBody()); - assertTrue(event.getVes() != null); + assertNotNull(event.getVes()); }); } diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java index 54c5091..5028464 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java @@ -43,7 +43,7 @@ import io.undertow.server.HttpServerExchange; import utils.EventUtils;
@ExtendWith(MockitoExtension.class)
-public class MeasSplitterTest {
+class MeasSplitterTest {
private static final String baseDir = "src/test/resources/split_test/";
private MeasSplitter objUnderTest;
private MeasConverter converter;
@@ -57,33 +57,31 @@ public class MeasSplitterTest { MapperConfig config;
@BeforeEach
- public void setup() {
+ void setup() {
converter = new MeasConverter();
objUnderTest = new MeasSplitter(converter);
}
- public void setupBaseEvent() {
+ void setupBaseEvent() {
Mockito.when(event.getHttpServerExchange()).thenReturn(exchange);
Mockito.when(event.getMetadata()).thenReturn(meta);
- Mockito.when(event.getMdc()).thenReturn(new HashMap<String, String>());
+ Mockito.when(event.getMdc()).thenReturn(new HashMap<>());
Mockito.when(event.getMetadata()).thenReturn(meta);
Mockito.when(event.getPublishIdentity()).thenReturn("");
}
@Test
- public void no_measData() {
+ void no_measData() {
String inputPath = baseDir + "no_measdata";
String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml"));
Mockito.when(event.getBody()).thenReturn(inputXml);
- Assertions.assertThrows(NoSuchElementException.class, ()->{
- objUnderTest.split(event);
- });
+ Assertions.assertThrows(NoSuchElementException.class, () -> objUnderTest.split(event));
}
@Test
- public void typeA_returns_only_one_event() throws JAXBException {
+ void typeA_returns_only_one_event() throws JAXBException {
String inputPath = baseDir + "meas_results_typeA";
String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml"));
MeasCollecFile measToBeSplit = converter.convert(inputXml);
@@ -97,7 +95,7 @@ public class MeasSplitterTest { }
@Test
- public void typeC_returns_multiple_events() throws JAXBException {
+ void typeC_returns_multiple_events() throws JAXBException {
String inputPath = baseDir + "meas_results_typeC";
String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml"));
setupBaseEvent();
@@ -111,7 +109,7 @@ public class MeasSplitterTest { for (int i = 0; i < splitEvents.size(); i++) {
String measInfoId = splitEvents.get(i).getMeasCollecFile()
.getMeasData().get(0).getMeasInfo().get(0).getMeasInfoId();
- Assertions.assertTrue(measInfoId.equals("measInfoId"+(i+1)));
+ Assertions.assertEquals(measInfoId, "measInfoId" + (i + 1));
}
}
}
diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java index 34a2277..c9f2998 100644 --- a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java +++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSenderTests.java @@ -70,7 +70,6 @@ public class RequestSenderTests { String uuidRegex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"; ListAppender<ILoggingEvent> logAppender = LoggingUtils.getLogListAppender(RequestSender.class); HttpRequest req = HttpRequest.request(); - client.when(req .withHeader(ONAPLogConstants.Headers.REQUEST_ID, uuidRegex) .withHeader(ONAPLogConstants.Headers.INVOCATION_ID, uuidRegex)) |