diff options
author | Lusheng Ji <lji@research.att.com> | 2018-04-12 21:43:29 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-04-12 21:43:29 +0000 |
commit | fe084656d61212a7e4a5f6cc6f60b3eb6a146302 (patch) | |
tree | 94b7077f4a2ad0a0e4e469f39ffbbeb3cd220e21 | |
parent | ed8fc543198ec2a878f7e4e2966fa66a6c4986a9 (diff) | |
parent | 3c2e8c9c4c32ad59be75a7500e628344c6dab5bf (diff) |
Merge "Already added loading config for AAI and DMAAP"
21 files changed, 536 insertions, 103 deletions
@@ -410,27 +410,31 @@ <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> </dependency> - <dependency> - <groupId>org.apache.httpcomponents</groupId> - <artifactId>httpclient</artifactId> - <version>4.5.4</version> - </dependency> - - <dependency> - <groupId>com.google.guava</groupId> - <artifactId>guava</artifactId> - <version>10.0.1</version> - </dependency> - <dependency> - <groupId>org.apache.commons</groupId> - <artifactId>commons-lang3</artifactId> - <version>3.6</version> - </dependency> - <dependency> - <groupId>org.springframework</groupId> - <artifactId>spring-beans</artifactId> - <version>5.0.4.RELEASE</version> - </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.5.4</version> + </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <version>10.0.1</version> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-lang3</artifactId> + <version>3.6</version> + </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + <version>5.0.4.RELEASE</version> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jdk8</artifactId> + <version>2.6.3</version> + </dependency> <!-- LOGGING dependencies> --> <dependency> diff --git a/prh-aai-client/pom.xml b/prh-aai-client/pom.xml index eaed6096..95d758e8 100644 --- a/prh-aai-client/pom.xml +++ b/prh-aai-client/pom.xml @@ -40,7 +40,7 @@ </properties> <dependencies> - + <!-- DEVELOPMENT DEPENDENCIES --> <dependency> <groupId>org.immutables</groupId> @@ -61,14 +61,18 @@ <artifactId>spring-context</artifactId> <version>3.0.2.RELEASE</version> </dependency> - <dependency> - <groupId>com.google.guava</groupId> - <artifactId>guava</artifactId> - </dependency> + <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jdk8</artifactId> + </dependency> <!-- LOGGING DEPENDENCIES--> <dependency> diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java index 9f93f896..4b17b4b6 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/config/AAIHttpClientConfiguration.java @@ -20,15 +20,16 @@ package org.onap.dcaegen2.services.config; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; -import org.springframework.stereotype.Component; - import java.io.Serializable; +import org.springframework.stereotype.Component; @Component @Value.Immutable(prehash = true) -@Value.Style(stagedBuilder = true) +@Value.Style(builder = "new") +@JsonDeserialize(builder = ImmutableAAIHttpClientConfiguration.Builder.class) public abstract class AAIHttpClientConfiguration implements Serializable { private static final long serialVersionUID = 1L; diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java index a759aca5..76c2969a 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/service/config/AAIHttpClientConfigurationTest.java @@ -36,21 +36,21 @@ public class AAIHttpClientConfigurationTest { @BeforeAll public static void init() { - client = ImmutableAAIHttpClientConfiguration.builder() - .aaiHost(AAI_HOST) - .aaiHostPortNumber(PORT) - .aaiProtocol(PROTOCOL) - .aaiUserName(USER_NAME_PASSWORD) - .aaiUserPassword(USER_NAME_PASSWORD) - .aaiIgnoreSSLCertificateErrors(true) - .build(); + client = new ImmutableAAIHttpClientConfiguration.Builder() + .aaiHost(AAI_HOST) + .aaiHostPortNumber(PORT) + .aaiProtocol(PROTOCOL) + .aaiUserName(USER_NAME_PASSWORD) + .aaiUserPassword(USER_NAME_PASSWORD) + .aaiIgnoreSSLCertificateErrors(true) + .build(); } @Test public void testGetters_success() { - Assertions.assertEquals(AAI_HOST,client.aaiHost()); + Assertions.assertEquals(AAI_HOST, client.aaiHost()); Assertions.assertEquals(PORT, client.aaiHostPortNumber()); - Assertions.assertEquals(PROTOCOL,client.aaiProtocol()); + Assertions.assertEquals(PROTOCOL, client.aaiProtocol()); Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserName()); Assertions.assertEquals(USER_NAME_PASSWORD, client.aaiUserPassword()); Assertions.assertEquals(true, client.aaiIgnoreSSLCertificateErrors()); 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 @@ <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <executions> - <execution> - <goals> - <goal>build</goal> - </goals> - <phase>package</phase> - </execution> + <execution> + <goals> + <goal>build</goal> + </goals> + <phase>package</phase> + </execution> </executions> <configuration> <skipDockerBuild>false</skipDockerBuild> @@ -122,6 +122,10 @@ <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jdk8</artifactId> + </dependency> <!-- LOGGING DEPENDENCIES--> <dependency> 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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/tasks/ScheduledXmlContextITest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/IT/ScheduledXmlContextITest.java index ed80a0a1..9cc1793a 100644 --- 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/IT/ScheduledXmlContextITest.java @@ -17,9 +17,10 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.dcaegen2.services.prh.tasks; +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; @@ -28,7 +29,10 @@ 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; @@ -63,4 +67,12 @@ class ScheduledXmlContextITest extends AbstractTestNGSpringContextTests { } +@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 <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> 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/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 diff --git a/prh-dmaap-client/pom.xml b/prh-dmaap-client/pom.xml index fbb05cfb..94808cf8 100644 --- a/prh-dmaap-client/pom.xml +++ b/prh-dmaap-client/pom.xml @@ -44,6 +44,10 @@ <groupId>org.immutables</groupId> <artifactId>value</artifactId> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.datatype</groupId> + <artifactId>jackson-datatype-jdk8</artifactId> + </dependency> <!-- LOGGING DEPENDENCIES --> <dependency> diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java index c4acf3af..9b322c9c 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapConsumerConfiguration.java @@ -19,24 +19,31 @@ */ package org.onap.dcaegen2.services.config; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; +import org.springframework.stereotype.Component; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18 */ +@Component @Value.Immutable(prehash = true) -@Value.Style(visibility = Value.Style.ImplementationVisibility.PACKAGE) +@Value.Style(builder = "new") +@JsonDeserialize(builder = ImmutableDmaapConsumerConfiguration.Builder.class) public abstract class DmaapConsumerConfiguration implements DmaapCustomConfig { private static final long serialVersionUID = 1L; - + @Value.Parameter abstract String consumerId(); + @Value.Parameter abstract String consumerGroup(); + @Value.Parameter abstract Integer timeoutMS(); + @Value.Parameter abstract Integer messageLimit(); diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java index ee849338..60df5b05 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapCustomConfig.java @@ -20,42 +20,50 @@ package org.onap.dcaegen2.services.config; import java.io.Serializable; +import org.immutables.value.Value; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/28/18 */ public interface DmaapCustomConfig extends Serializable { - String dmmaphostName(); + @Value.Parameter + String dmmapHostName(); - Integer dmmapportNumber(); + @Value.Parameter + Integer dmmapPortNumber(); - String dmmaptopicName(); + @Value.Parameter + String dmmapTopicName(); - String dmmapprotocol(); + @Value.Parameter + String dmmapProtocol(); - String dmmapuserName(); + @Value.Parameter + String dmmapUserName(); - String dmmapuserPassword(); + @Value.Parameter + String dmmapUserPassword(); - String dmmapcontentType(); + @Value.Parameter + String dmmapContentType(); interface Builder<T extends DmaapCustomConfig, B extends Builder<T, B>> { - B dmmaphostName(String dmmaphostName); + B dmmapHostName(String dmmapHostName); - B dmmapportNumber(Integer dmmapportNumber); + B dmmapPortNumber(Integer dmmapPortNumber); - B dmmaptopicName(String dmmaptopicName); + B dmmapTopicName(String dmmapTopicName); - B dmmapprotocol(String dmmapprotocol); + B dmmapProtocol(String dmmapProtocol); - B dmmapuserName(String dmmapuserName); + B dmmapUserName(String dmmapUserName); - B dmmapuserPassword(String dmmapuserPassword); + B dmmapUserPassword(String dmmapUserPassword); - B dmmapcontentType(String dmmapcontentType); + B dmmapContentType(String dmmapContentType); T build(); } diff --git a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapProducerConfiguration.java b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapProducerConfiguration.java index b63d68bd..99cbbca3 100644 --- a/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapProducerConfiguration.java +++ b/prh-dmaap-client/src/main/java/org/onap/dcaegen2/services/config/DmaapProducerConfiguration.java @@ -19,19 +19,24 @@ */ package org.onap.dcaegen2.services.config; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; +import org.springframework.stereotype.Component; /** * @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18 */ +@Component @Value.Immutable(prehash = true) -@Value.Style(visibility = Value.Style.ImplementationVisibility.PACKAGE) +@Value.Style(builder = "new") +@JsonDeserialize(builder = ImmutableDmaapProducerConfiguration.Builder.class) public abstract class DmaapProducerConfiguration implements DmaapCustomConfig { private static final long serialVersionUID = 1L; interface Builder extends DmaapCustomConfig.Builder<DmaapProducerConfiguration, DmaapProducerConfiguration.Builder> { + } public static DmaapProducerConfiguration.Builder builder() { |