From 3c2e8c9c4c32ad59be75a7500e628344c6dab5bf Mon Sep 17 00:00:00 2001 From: wasala Date: Thu, 12 Apr 2018 13:37:41 +0200 Subject: Already added loading config for AAI and DMAAP First of all we have defined a file in which we have pointer to configuration file for DMAAP and AAI After that added UT Change-Id: I7f9554a019b348a3460f5fa9d8dd3e4002a6686f Issue-ID: DCAEGEN2-407 Signed-off-by: wasala --- prh-app-server/config/application.yaml | 37 +---- prh-app-server/config/prh_endpoints.json | 38 ++++++ prh-app-server/pom.xml | 16 ++- .../org/onap/dcaegen2/services/prh/MainApp.java | 1 - .../services/prh/configuration/AppConfig.java | 37 +++++ .../services/prh/configuration/PrhAppConfig.java | 147 ++++++++++++++++++++ .../services/prh/tasks/DmaapConsumerTask.java | 6 + .../dcaegen2/services/prh/tasks/ScheduledTask.java | 2 +- .../services/prh/IT/ScheduledXmlContextITest.java | 78 +++++++++++ .../prh/configuration/PrhAppConfigTest.java | 150 +++++++++++++++++++++ .../services/prh/tasks/DmaapConsumerTaskSpy.java | 7 +- .../services/prh/tasks/ScheduleControllerSpy.java | 1 - .../prh/tasks/ScheduledXmlContextITest.java | 66 --------- .../src/test/resources/prh_endpoints.json | 38 ++++++ 14 files changed, 512 insertions(+), 112 deletions(-) create mode 100644 prh-app-server/config/prh_endpoints.json create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java create mode 100644 prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java create mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java create mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java delete mode 100644 prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledXmlContextITest.java create mode 100644 prh-app-server/src/test/resources/prh_endpoints.json (limited to 'prh-app-server') diff --git a/prh-app-server/config/application.yaml b/prh-app-server/config/application.yaml index 8b2e7ac7..0baa1f9d 100644 --- a/prh-app-server/config/application.yaml +++ b/prh-app-server/config/application.yaml @@ -11,39 +11,4 @@ logging: org.onap.dcaegen2.services.prh: INFO file: opt/log/application.log app: - configs: - aaiHttpClientConfiguration: - aaiHost: "dns.aai.host" - aaiHostPortNumber: 8080 - aaiProtocol: "https" - aaiUserName: "admin" - aaiUserPassword: "admin" - aaiIgnoreSSLCertificateErrors: true -#app: -# AAIHttpClientConfiguration: -# aaiHost: "localhost" -# aaiHostPortNumber: 8080 -# aaiProtocol: "http" -# aaiUserName: "admin" -# aaiUserPassword: "admin" -# aaiIgnoreSSLCertificateErrors: true -# dmaap-consumer-configuration: -# dmmaphost-name: "localhost" -# dmmapport-number: 2222 -# dmmaptopic-name: "temp" -# dmmapprotocol: "http" -# dmmapuserName: "admin" -# dmmapuserPassword: "admin" -# dmmapcontentType: "application/json" -# consumerId: "1" -# consumerGroup: "other" -# timeoutMS: 1000 -# message-limit: 1000 -# dmaap-producer-configuration: -# dmmaphostName: "localhost" -# dmmapportNumber: 2223 -# dmmaptopicName: "temp" -# dmmapprotocol: "http" -# dmmapuserName: "admin" -# dmmapuserPassword: "admin" -# dmmapcontentType: "application/json" + filepath: config/prh_endpoints.json diff --git a/prh-app-server/config/prh_endpoints.json b/prh-app-server/config/prh_endpoints.json new file mode 100644 index 00000000..dcfbab3f --- /dev/null +++ b/prh-app-server/config/prh_endpoints.json @@ -0,0 +1,38 @@ +{ + "configs": { + "dmaap": { + "dmaapConsumerConfiguration": { + "dmmapHostName": "localhost", + "dmmapPortNumber": 2222, + "dmmapTopicName": "temp", + "dmmapProtocol": "http", + "dmmapUserName": "admin", + "dmmapUserPassword": "admin", + "dmmapContentType": "application/json", + "consumerId": "1", + "consumerGroup": "other", + "timeoutMS": 1000, + "message-limit": 1000 + }, + "dmaapProducerConfiguration": { + "dmmapHostName": "localhost", + "dmmapPortNumber": 2223, + "dmmapTopicName": "temp", + "dmmapProtocol": "http", + "dmmapUserName": "admin", + "dmmapUserPassword": "admin", + "dmmapContentType": "application/json" + } + }, + "aai": { + "aaiHttpClientConfiguration": { + "aaiHost": "", + "aaiHostPortNumber": 8080, + "aaiProtocol": "https", + "aaiUserName": "admin", + "aaiUserPassword": "admin", + "aaiIgnoreSSLCertificateErrors": true + } + } + } +} \ No newline at end of file diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index a2d0aff5..23a12e1d 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -62,12 +62,12 @@ docker-maven-plugin 1.0.0 - - - build - - package - + + + build + + package + false @@ -122,6 +122,10 @@ com.spotify docker-maven-plugin + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java index 9a1fec28..2671669a 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/MainApp.java @@ -41,7 +41,6 @@ public class MainApp { SpringApplication.run(MainApp.class, args); } - @Bean TaskScheduler taskScheduler() { return new ConcurrentTaskScheduler(); diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java new file mode 100644 index 00000000..59cf40da --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/AppConfig.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.configuration; + +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.config.DmaapProducerConfiguration; + +/** + * @author Przemysław Wąsala on 4/9/18 + */ +public interface AppConfig { + + DmaapConsumerConfiguration getDmaapConsumerConfiguration() throws Exception; + + AAIHttpClientConfiguration getAAIHttpClientConfiguration() throws Exception; + + DmaapProducerConfiguration getDmaapProducerConfiguration() throws Exception; + +} diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java new file mode 100644 index 00000000..57335eff --- /dev/null +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfig.java @@ -0,0 +1,147 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.configuration; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.NullNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Optional; +import javax.validation.constraints.NotEmpty; +import org.onap.dcaegen2.services.config.AAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration; +import org.onap.dcaegen2.services.config.DmaapProducerConfiguration; +import org.onap.dcaegen2.services.config.ImmutableAAIHttpClientConfiguration; +import org.onap.dcaegen2.services.config.ImmutableDmaapConsumerConfiguration; +import org.onap.dcaegen2.services.config.ImmutableDmaapProducerConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author Przemysław Wąsala on 4/9/18 + */ +@Configuration +@EnableConfigurationProperties +@ConfigurationProperties("app") +public class PrhAppConfig implements AppConfig { + + private static final String CONFIG = "configs"; + private static final String AAI = "aai"; + private static final String DMAAP = "dmaap"; + private static final String AAI_CONFIG = "aaiHttpClientConfiguration"; + private static final String DMAAP_PRODUCER = "dmaapProducerConfiguration"; + private static final String DMAAP_CONSUMER = "dmaapConsumerConfiguration"; + + private static final Logger logger = LoggerFactory.getLogger(PrhAppConfig.class); + private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + + private ImmutableAAIHttpClientConfiguration immutableAAIHttpClientConfiguration; + + private ImmutableDmaapConsumerConfiguration immutableDmaapConsumerConfiguration; + + private ImmutableDmaapProducerConfiguration immutableDmaapProducerConfiguration; + + @NotEmpty + private String filepath; + + public void initFileStreamReader() { + + ObjectMapper jsonObjectMapper = new ObjectMapper().registerModule(new Jdk8Module()); + JsonNode jsonNode; + try (InputStream inputStream = getInputStream(filepath)) { + ObjectNode root = (ObjectNode) jsonObjectMapper.readTree(inputStream); + jsonNode = Optional.ofNullable(root.get(CONFIG).get(AAI).get(AAI_CONFIG)).orElse(NullNode.getInstance()); + immutableAAIHttpClientConfiguration = jsonObjectMapper + .treeToValue(jsonNode, ImmutableAAIHttpClientConfiguration.class); + jsonNode = Optional.ofNullable(root.get(CONFIG).get(DMAAP).get(DMAAP_CONSUMER)) + .orElse(NullNode.getInstance()); + immutableDmaapConsumerConfiguration = jsonObjectMapper + .treeToValue(jsonNode, ImmutableDmaapConsumerConfiguration.class); + jsonNode = Optional.ofNullable(root.get(CONFIG).get(DMAAP).get(DMAAP_PRODUCER)) + .orElse(NullNode.getInstance()); + immutableDmaapProducerConfiguration = jsonObjectMapper + .treeToValue(jsonNode, ImmutableDmaapProducerConfiguration.class); + } catch (FileNotFoundException e) { + logger + .error( + "Configuration PrhAppConfig initFileStreamReader()::FileNotFoundException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); + } catch (JsonParseException e) { + logger + .error( + "Configuration PrhAppConfig initFileStreamReader()::JsonParseException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); + } catch (JsonMappingException e) { + logger + .error( + "Configuration PrhAppConfig initFileStreamReader()::JsonMappingException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); + } catch (IOException e) { + logger + .error( + "Configuration PrhAppConfig initFileStreamReader()::IOException :: Execution Time - {}:{}", + dateTimeFormatter.format( + LocalDateTime.now()), e); + } + } + + InputStream getInputStream(String filepath) throws FileNotFoundException { + return new BufferedInputStream(new FileInputStream(filepath)); + } + + String getFilepath() { + return this.filepath; + } + + public void setFilepath(String filepath) { + this.filepath = filepath; + } + + @Override + public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { + return immutableDmaapConsumerConfiguration; + } + + @Override + public AAIHttpClientConfiguration getAAIHttpClientConfiguration() { + return immutableAAIHttpClientConfiguration; + } + + @Override + public DmaapProducerConfiguration getDmaapProducerConfiguration() { + return immutableDmaapProducerConfiguration; + } +} \ No newline at end of file diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java index 812f920c..2dc9b730 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTask.java @@ -21,8 +21,10 @@ package org.onap.dcaegen2.services.prh.tasks; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import org.onap.dcaegen2.services.prh.configuration.AppConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** @@ -35,11 +37,15 @@ public class DmaapConsumerTask implements DmaapTask { private static final Logger logger = LoggerFactory.getLogger(DmaapConsumerTask.class); private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + @Autowired + public AppConfig prhAppConfig; + @Override public void execute() { logger.debug("Start task DmaapConsumerTask::execute() :: Execution Time - {}", dateTimeFormatter.format( LocalDateTime.now())); + logger.debug("End task DmaapConsumerTask::execute() :: Execution Time - {}", dateTimeFormatter.format(LocalDateTime.now())); } diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTask.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTask.java index a8a0f0be..09bd5d8e 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTask.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/ScheduledTask.java @@ -52,7 +52,7 @@ public class ScheduledTask { dmaapConsumerTask.execute(); } catch (AAINotFoundException e) { logger - .warn("Task scheduledTaskAskingDMaaPOfConsumeEvent()::AAINotFoundException :: Execution Time - {}:{}", + .error("Task scheduledTaskAskingDMaaPOfConsumeEvent()::AAINotFoundException :: Execution Time - {}:{}", dateTimeFormatter.format( LocalDateTime.now()), e); } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java new file mode 100644 index 00000000..9cc1793a --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * PROJECT + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.IT; + +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension; +import org.onap.dcaegen2.services.prh.configuration.PrhAppConfig; +import org.onap.dcaegen2.services.prh.tasks.ScheduledTask; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; + +/** + * @author Przemysław Wąsala on 3/27/18 + */ + +@Configuration +@ComponentScan +@ExtendWith({MockitoExtension.class, SpringExtension.class}) +@ContextConfiguration(locations = {"classpath:scheduled-context.xml"}) +class ScheduledXmlContextITest extends AbstractTestNGSpringContextTests { + + private static final int WAIT_FOR_SCHEDULING = 1; + + @Autowired + private ScheduledTask scheduledTask; + + @Test + void testScheduling() { + final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); + executorService.scheduleWithFixedDelay(this::verifyDmaapConsumerTask, 0, WAIT_FOR_SCHEDULING, TimeUnit.SECONDS); + } + + private void verifyDmaapConsumerTask() { + verify(scheduledTask, atLeast(2)).scheduledTaskAskingDMaaPOfConsumeEvent(); + } + + +} + +@Configuration +class ServiceMockProvider { + @Bean + public PrhAppConfig getPrhAppConfig() { + return mock(PrhAppConfig.class); + } +} + + diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java new file mode 100644 index 00000000..a6361cfe --- /dev/null +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java @@ -0,0 +1,150 @@ +/*- + * ============LICENSE_START======================================================= + * PNF-REGISTRATION-HANDLER + * ================================================================================ + * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcaegen2.services.prh.configuration; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Objects; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentMatchers; +import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * @author Przemysław Wąsala on 4/9/18 + */ +@ExtendWith({MockitoExtension.class}) +class PrhAppConfigTest { + + private static final String PRH_ENDPOINTS = "prh_endpoints.json"; + private static final String jsonString = "{\"configs\":{\"aai\":{\"aaiHttpClientConfiguration\":{\"aaiHost\":\"\",\"aaiHostPortNumber\":8080,\"aaiIgnoreSSLCertificateErrors\":true,\"aaiProtocol\":\"https\",\"aaiUserName\":\"admin\",\"aaiUserPassword\":\"admin\"}},\"dmaap\":{\"dmaapConsumerConfiguration\":{\"consumerGroup\":\"other\",\"consumerId\":\"1\",\"dmmapContentType\":\"application/json\",\"dmmapHostName\":\"localhost\",\"dmmapPortNumber\":2222,\"dmmapProtocol\":\"http\",\"dmmapTopicName\":\"temp\",\"dmmapUserName\":\"admin\",\"dmmapUserPassword\":\"admin\",\"messageLimit\":1000,\"timeoutMS\":1000},\"dmaapProducerConfiguration\":{\"dmmapContentType\":\"application/json\",\"dmmapHostName\":\"localhost\",\"dmmapPortNumber\":2223,\"dmmapProtocol\":\"http\",\"dmmapTopicName\":\"temp\",\"dmmapUserName\":\"admin\",\"dmmapUserPassword\":\"admin\"}}}}"; + private static final String incorrectJsonString = "{\"configs\":{\"aai\":{\"aaiHttpClientConfiguration\":{\"aaiHost\":\"\",\"aaiHostPortNumber\":8080,\"aaiIgnoreSSLCertificateErrors\":true,\"aaiProtocol\":\"https\",\"aaiUserName\":\"admin\",\"aaiUserPassword\":\"admin\"}},\"dmaap\":{\"dmaapConsumerConfiguration\":{\"consumerGroup\":\"other\",\"consumerId\":\"1\",\"dmmapContentType\":\"application/json\",\"dmmapHostName\":\"localhost\",\"dmmapPortNumber\":2222,\"dmmapProtocol\":\"http\",\"dmmapTopicName\":\"temp\",\"dmmapUserName\":\"admin\",\"dmmapUserPassword\":\"admin\",\"messageLimit\":1000,\"timeoutMS\":1000},\"dmaapProducerConfiguration\":{\"dmmapContentType\":\"application/json\",\"dmmapHostName\":\"localhost\",\"dmmapPortNumber\":2223,\"dmmapProtocol\":\"http\",\"dmmaptopicName\":\"temp\",\"dmmapuserName\":\"admin\",\"dmmapuserPassword\":\"admin\"}}}}"; + private static PrhAppConfig prhAppConfig; + + private static String filePath = Objects + .requireNonNull(PrhAppConfigTest.class.getClassLoader().getResource(PRH_ENDPOINTS)).getFile(); + + @BeforeEach + public void setUp() { + prhAppConfig = spy(new PrhAppConfig()); + } + + @Test + public void whenApplicationWasStarted_FilePathIsSet() { + // + // When + // + prhAppConfig.setFilepath(filePath); + // + // Then + // + verify(prhAppConfig, times(1)).setFilepath(anyString()); + verify(prhAppConfig, times(0)).initFileStreamReader(); + Assertions.assertEquals(filePath, prhAppConfig.getFilepath()); + } + + @Test + public void whenTheConfigurationFits_GetAaiAndDmaapObjectRepresentationConfiguration() + throws FileNotFoundException { + // + // Given + // + InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( + StandardCharsets.UTF_8))); + // + // When + // + prhAppConfig.setFilepath(filePath); + doReturn(inputStream).when(prhAppConfig).getInputStream(any()); + prhAppConfig.initFileStreamReader(); + // + // Then + // + verify(prhAppConfig, times(1)).setFilepath(anyString()); + verify(prhAppConfig, times(1)).initFileStreamReader(); + Assertions.assertNotNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); + Assertions.assertNotNull(prhAppConfig.getDmaapProducerConfiguration()); + } + + @Test + public void whenFileIsNotExist_ThrowFileNotFoundExcepton() { + // + // Given + // + filePath = "/temp.json"; + prhAppConfig.setFilepath(filePath); + // + // When + // + prhAppConfig.initFileStreamReader(); + // + // Then + // + verify(prhAppConfig, times(1)).setFilepath(anyString()); + verify(prhAppConfig, times(1)).initFileStreamReader(); + Assertions.assertNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); + Assertions.assertNull(prhAppConfig.getDmaapProducerConfiguration()); + + } + + @Test + public void whenFileIsExistsButJsonIsIncorrect() throws FileNotFoundException { + // + // Given + // + InputStream inputStream = new ByteArrayInputStream((incorrectJsonString.getBytes( + StandardCharsets.UTF_8))); + // + // When + // + prhAppConfig.setFilepath(filePath); + doReturn(inputStream).when(prhAppConfig).getInputStream(any()); + prhAppConfig.initFileStreamReader(); + // + // Then + // + verify(prhAppConfig, times(1)).setFilepath(anyString()); + verify(prhAppConfig, times(1)).initFileStreamReader(); + Assertions.assertNotNull(prhAppConfig.getAAIHttpClientConfiguration()); + Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); + Assertions.assertNull(prhAppConfig.getDmaapProducerConfiguration()); + + } +} \ No newline at end of file diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java index aa5e052e..1f5b858a 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/DmaapConsumerTaskSpy.java @@ -1,4 +1,4 @@ -/*- +/* * ============LICENSE_START======================================================= * PROJECT * ================================================================================ @@ -20,7 +20,12 @@ package org.onap.dcaegen2.services.prh.tasks; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; +import org.junit.runners.Parameterized.UseParametersRunnerFactory; +import org.onap.dcaegen2.services.prh.configuration.AppConfig; +import org.onap.dcaegen2.services.prh.configuration.PrhAppConfig; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java index a5581767..d4975098 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduleControllerSpy.java @@ -40,5 +40,4 @@ public class ScheduleControllerSpy { public ScheduledTask registerSimpleDmaapConsumerTask() { return spy(new ScheduledTask(dmaapConsumerTaskSpy)); } - } diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledXmlContextITest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledXmlContextITest.java deleted file mode 100644 index ed80a0a1..00000000 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/tasks/ScheduledXmlContextITest.java +++ /dev/null @@ -1,66 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ -package org.onap.dcaegen2.services.prh.tasks; - -import static org.mockito.Mockito.atLeast; -import static org.mockito.Mockito.verify; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.onap.dcaegen2.services.prh.IT.junit5.mockito.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit.jupiter.SpringExtension; -import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; - -/** - * @author Przemysław Wąsala on 3/27/18 - */ - -@Configuration -@ComponentScan -@ExtendWith({MockitoExtension.class, SpringExtension.class}) -@ContextConfiguration(locations = {"classpath:scheduled-context.xml"}) -class ScheduledXmlContextITest extends AbstractTestNGSpringContextTests { - - private static final int WAIT_FOR_SCHEDULING = 1; - - @Autowired - private ScheduledTask scheduledTask; - - @Test - void testScheduling() { - final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); - executorService.scheduleWithFixedDelay(this::verifyDmaapConsumerTask, 0, WAIT_FOR_SCHEDULING, TimeUnit.SECONDS); - } - - private void verifyDmaapConsumerTask() { - verify(scheduledTask, atLeast(2)).scheduledTaskAskingDMaaPOfConsumeEvent(); - } - - -} - - diff --git a/prh-app-server/src/test/resources/prh_endpoints.json b/prh-app-server/src/test/resources/prh_endpoints.json new file mode 100644 index 00000000..c3e96d65 --- /dev/null +++ b/prh-app-server/src/test/resources/prh_endpoints.json @@ -0,0 +1,38 @@ +{ + "configs": { + "aai": { + "aaiHttpClientConfiguration": { + "aaiHost": "", + "aaiHostPortNumber": 8080, + "aaiIgnoreSSLCertificateErrors": true, + "aaiProtocol": "https", + "aaiUserName": "admin", + "aaiUserPassword": "admin" + } + }, + "dmaap": { + "dmaapConsumerConfiguration": { + "consumerGroup": "other", + "consumerId": "1", + "dmmapContentType": "application/json", + "dmmapHostName": "localhost", + "dmmapPortNumber": 2222, + "dmmapProtocol": "http", + "dmmapTopicName": "temp", + "dmmapUserName": "admin", + "dmmapUserPassword": "admin", + "messageLimit": 1000, + "timeoutMS": 1000 + }, + "dmaapProducerConfiguration": { + "dmmapContentType": "application/json", + "dmmapHostName": "localhost", + "dmmapPortNumber": 2223, + "dmmapProtocol": "http", + "dmmapTopicName": "temp", + "dmmapUserName": "admin", + "dmmapUserPassword": "admin" + } + } + } +} \ No newline at end of file -- cgit 1.2.3-korg