From be3127220aed103d1c1bb495080d62593d61b835 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 4 Sep 2018 09:50:05 +0200 Subject: [PATCH] Maven resources refactoring Added resurces to java spring classpath directly Change-Id: I194748706f63cff1966111bbdb52538bd5a83855 Issue-ID: DCAEGEN2-606 Signed-off-by: pwielebs --- prh-app-server/pom.xml | 13 +++---- .../prh/configuration/CloudConfiguration.java | 8 ++--- .../services/prh/configuration/PrhAppConfig.java | 25 ++++++------- .../src/main/resources/application.properties | 17 --------- .../main/resources}/application.yaml | 4 +-- .../resources/{keystore.jks => keystore-local} | Bin .../keystore => src/main/resources/keystore-onap} | Bin prh-app-server/src/main/resources/keystore.jks.old | Bin 2272 -> 0 bytes .../main/resources}/prh_endpoints.json | 0 .../src/main/resources/scheduled-context.xml | 16 --------- .../prh/configuration/PrhAppConfigTest.java | 39 ++++++++++++--------- 11 files changed, 43 insertions(+), 79 deletions(-) delete mode 100644 prh-app-server/src/main/resources/application.properties rename prh-app-server/{config => src/main/resources}/application.yaml (83%) rename prh-app-server/src/main/resources/{keystore.jks => keystore-local} (100%) rename prh-app-server/{config/keystore => src/main/resources/keystore-onap} (100%) delete mode 100644 prh-app-server/src/main/resources/keystore.jks.old rename prh-app-server/{config => src/main/resources}/prh_endpoints.json (100%) delete mode 100644 prh-app-server/src/main/resources/scheduled-context.xml diff --git a/prh-app-server/pom.xml b/prh-app-server/pom.xml index 9ecf57b5..b04c06b9 100644 --- a/prh-app-server/pom.xml +++ b/prh-app-server/pom.xml @@ -19,8 +19,8 @@ ~ ============LICENSE_END========================================================= --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -41,8 +41,7 @@ - config - ${project.build.directory}/config + src/main/resources @@ -71,16 +70,12 @@ ${project.artifactId}.jar /opt - - ${project.build.directory}/config - /config - 8100 8433 - ["java", "-jar", "/opt/${project.artifactId}.jar"] + ["java", "-jar", "/opt/${project.artifactId}.jar"] diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java index 10626a32..e598b4b3 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/configuration/CloudConfiguration.java @@ -89,7 +89,8 @@ public class CloudConfiguration extends AppConfig { LOGGER.info("Received application configuration: {}", jsonObject); CloudConfigParser cloudConfigParser = new CloudConfigParser(jsonObject); dmaapPublisherCloudConfiguration = cloudConfigParser.getDmaapPublisherConfig(); - aaiClientCloudConfiguration = cloudConfigParser.getAaiClientConfig(); + aaiClientCloudConfiguration = ImmutableAaiClientConfiguration.copyOf(cloudConfigParser.getAaiClientConfig()) + .withAaiHeaders(aaiClientConfiguration.aaiHeaders()); dmaapConsumerCloudConfiguration = cloudConfigParser.getDmaapConsumerConfig(); } @@ -100,11 +101,10 @@ public class CloudConfiguration extends AppConfig { @Override public AaiClientConfiguration getAaiClientConfiguration() { - return Optional.ofNullable(ImmutableAaiClientConfiguration.copyOf(aaiClientCloudConfiguration) - .withAaiHeaders(aaiClientConfiguration.aaiHeaders())) - .orElse(ImmutableAaiClientConfiguration.copyOf(super.getAaiClientConfiguration())); + return Optional.ofNullable(aaiClientCloudConfiguration).orElse(super.getAaiClientConfiguration()); } + @Override public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { return Optional.ofNullable(dmaapConsumerCloudConfiguration).orElse(super.getDmaapConsumerConfiguration()); 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 index 03d24382..882e4d7e 100644 --- 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 @@ -36,7 +36,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.ServiceLoader; -import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotNull; import org.onap.dcaegen2.services.prh.config.AaiClientConfiguration; import org.onap.dcaegen2.services.prh.config.DmaapConsumerConfiguration; @@ -44,9 +43,11 @@ import org.onap.dcaegen2.services.prh.config.DmaapPublisherConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.Resource; /** * @author Przemysław Wąsala on 4/9/18 @@ -71,9 +72,8 @@ public abstract class PrhAppConfig implements Config { DmaapPublisherConfiguration dmaapPublisherConfiguration; - @NotEmpty - private String filepath; - + @Value("classpath:prh_endpoints.json") + private Resource resourceFile; @Override public DmaapConsumerConfiguration getDmaapConsumerConfiguration() { @@ -97,7 +97,8 @@ public abstract class PrhAppConfig implements Config { ServiceLoader.load(TypeAdapterFactory.class).forEach(gsonBuilder::registerTypeAdapterFactory); JsonParser parser = new JsonParser(); JsonObject jsonObject; - try (InputStream inputStream = getInputStream(filepath)) { + + try (InputStream inputStream = resourceFile.getInputStream()) { JsonElement rootElement = getJsonElement(parser, inputStream); if (rootElement.isJsonObject()) { jsonObject = rootElement.getAsJsonObject(); @@ -114,7 +115,7 @@ public abstract class PrhAppConfig implements Config { DmaapPublisherConfiguration.class); } } catch (IOException e) { - LOGGER.warn("Problem with file loading, file: {}", filepath, e); + LOGGER.warn("Problem with file loading, file ", e); } catch (JsonSyntaxException e) { LOGGER.warn("Problem with Json deserialization", e); } @@ -129,16 +130,12 @@ public abstract class PrhAppConfig implements Config { return gsonBuilder.create().fromJson(jsonObject, type); } - InputStream getInputStream(@NotNull String filepath) throws IOException { - return new BufferedInputStream(new FileInputStream(filepath)); - } - - String getFilepath() { - return this.filepath; + void setResourceFile(Resource resourceFile) { + this.resourceFile = resourceFile; } - public void setFilepath(String filepath) { - this.filepath = filepath; + InputStream getInputStream(@NotNull String filepath) throws IOException { + return new BufferedInputStream(new FileInputStream(filepath)); } public void setXOnapRequestId(String xonaprequestid) { diff --git a/prh-app-server/src/main/resources/application.properties b/prh-app-server/src/main/resources/application.properties deleted file mode 100644 index e343a360..00000000 --- a/prh-app-server/src/main/resources/application.properties +++ /dev/null @@ -1,17 +0,0 @@ -spring.profiles.active=prod -server.port=8433 -server.ssl.key-store-type=PKCS12 -server.ssl.key-store-password=nokiapnf -server.ssl.key-store=classpath:keystore.jks -server.ssl.key-password=nokiapnf -server.ssl.key-alias=tomcat-localhost -logging.level.root=ERROR -logging.level.org.springframework=ERROR -logging.level.org.springframework.data=ERROR -logging.level.org.onap.dcaegen2.services.prh=INFO -logging.level.org.springframework.web.reactive=WARN -logging.level.reactor.ipc.netty.http.client=WARN -app.filepath=config/prh_endpoints.json -app.xonaprequestid=requestID -app.xinvocationid=invocationID - diff --git a/prh-app-server/config/application.yaml b/prh-app-server/src/main/resources/application.yaml similarity index 83% rename from prh-app-server/config/application.yaml rename to prh-app-server/src/main/resources/application.yaml index 2e6f54df..88d1e401 100644 --- a/prh-app-server/config/application.yaml +++ b/prh-app-server/src/main/resources/application.yaml @@ -6,7 +6,7 @@ server: ssl: key-store-type: PKCS12 key-store-password: nokiapnf - key-store: classpath:keystore.jks + key-store: classpath:keystore-local key-password: nokiapnf keyAlias: tomcat-localhost logging: @@ -17,5 +17,3 @@ logging: org.springframework: ERROR org.springframework.data: ERROR org.springframework.web.reactive: WARN -app: - filepath: config/prh_endpoints.json \ No newline at end of file diff --git a/prh-app-server/src/main/resources/keystore.jks b/prh-app-server/src/main/resources/keystore-local similarity index 100% rename from prh-app-server/src/main/resources/keystore.jks rename to prh-app-server/src/main/resources/keystore-local diff --git a/prh-app-server/config/keystore b/prh-app-server/src/main/resources/keystore-onap similarity index 100% rename from prh-app-server/config/keystore rename to prh-app-server/src/main/resources/keystore-onap diff --git a/prh-app-server/src/main/resources/keystore.jks.old b/prh-app-server/src/main/resources/keystore.jks.old deleted file mode 100644 index 8a2b4f99223e670e986b09801d57e075551d3e2e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2272 zcmc(g*H@DX6UNgiA@l{LhF(PSh9ZK((vgnRn-b|Qkw7S-E(8tTB@jRqq&ETSQltrl zA|ir|g3GQHK>>pjk+QS}&v&u^!FMqizh};zGv}E(Glz^r1_%U#90&MkaEB8@yl~-X zf(c%@V1Ghb_;KRe>{Bx-5C{$d$go3z6TyBO0fvE= zD|k6Q>M->hxycV&u+#J$Yl{f8EGi9GiLbkyWLBM0#u_-h2a^%DEDA3^^Il9qgr*nA zBSLB77Fzn2=&fYREyKmwVKxn~ylg3n%iC?yolNXCGM_{fGovcVoJ{-DdpoZ1WSyGw zrd3@Z)24CZ3i@<<*zggTze|p?aP9L(p*`cmOx^t7D<+f23U>7bq-CSg0o07OV5%m4 zs|oOX85eJxNGYWoZ_heaY6{mq^QZb#6{@08bSm-9IwBNI<|&6e)+3){mM`YyA1uBi zmDmwV)oLP_hU7@|vZvXq=ikZ(1&w1nlRf&cX15D8pv~+ydZQk|D=jq~(-$98i)W!_ z`D1ZP1e-5C9~4JFz7rbS864CV2@uZjA9{DFW`+vXf{=*!14}^V80{Z2%TK+0#}pSZ zR8yYXy&Be3F^+n~s?V_lUmbm}1b{TrowdWgpHr*|2m^>OWkhbUm(4?e3G%O;vw2^xaK=*meyM#}+f|$U>z3=6b76bYsMm~+^46$v1N(GO1y~mH zsQtG}h(_xR{v2BenlA>ib0f&z<4sPMJumNs5}%DbIP;q?vP&Whn_W71P2`DdhBeY6 zlNSLbMj);{t7+ym#$alvvTRosXyNu6XDW%t-r*~@MOxu%cM6s>gKvxfN}VeO@0VHq zG9f&=R=%D$V^C*08{Qa$S1K?(F!YXE&E>LF`c0^HQTdbPAk*4Xiq*J)VIEL!_#C2< zv+(4tTQAx@K(srkdkQBrq7c`#MWd8y+gb|lfYzAipcKd847#|Rjf~Ev0#xbP%QJ9N zRIGFhX>8=f%%f8`B~~da-$?v3>wF;f5t$~PlyRU~vY#kYL_sG-YO76R&Y4{2ztnz5 z*nJZAT|BL|N)a{CjTA^WKvQv6Q_GWb&e~!gVEGlfg8jSQoApw)o=O#Z$ow89C}h=k zEc#L!OA>Hvt1ad>AKT4IJSt^ZU}(BL_)GsBlYpwYxc8)Zmwkyf@P`26eaO!n33pb} z0JK0LXc|CP2q0mR02>1807LLWY%Q_e z$M^FXSov5S@IiP&Bp%BHaGfYD9IQ@6f)|~Pi^OsQ948VxAKco;!b~5_2k@LQl8^l- zqiwA9ZLy+&@QDcJ;Ik#(@Ck_yJ1Ipw4craG1>*p5K@|2JppMl7Gyzpr4Oc;w>It6x zAO64EB7>#=YtZrkfRe#nAb<>JhmgTwP>GEdLAlwEP;?9IXTQpzw3Fi1EU2&cI?&8d z@)I-jnvX;6?GnG3RC$&`dYPLEMb%XuwT$HbkGSWWDw;t=X6>AV6oxH&{~UMERACSI zoIf?NSY}$e0naO#^NRF{$J@1;0_t;`u@!IV^a!8kScWF?g;u<~^GvwHri{jFOp z8buY_8y>=6M^l6x^1fFXC#q>3g~%u@Zjt4K!mN>f+?;b0TB*x{;4B~Jm?8VkzfjNc z<}#B+Bez1w_{*uQ-A4mb^aP{))Qz?PqhG6`W2YYe2srn6aLqa4b?e5P!t%`yRoplh z(JY2PA9}C)kLFCD$LVR?54qgBh9FQd2wWlwNC0BT!xiI%3BdSox5jXRK4-j%71{dT zOSN)4E>y1Rza}1U*|CXWP%88v4d*?M(%!|3x_8|(dN{Jwl6K=6B5@r m=W_2ZGQ_(EVta~0a513KsknEdUfwx#dr6dHc1OLI{(k{C57rX^ diff --git a/prh-app-server/config/prh_endpoints.json b/prh-app-server/src/main/resources/prh_endpoints.json similarity index 100% rename from prh-app-server/config/prh_endpoints.json rename to prh-app-server/src/main/resources/prh_endpoints.json diff --git a/prh-app-server/src/main/resources/scheduled-context.xml b/prh-app-server/src/main/resources/scheduled-context.xml deleted file mode 100644 index 82067c17..00000000 --- a/prh-app-server/src/main/resources/scheduled-context.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - 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 index 8ad2a94f..01866309 100644 --- 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 @@ -41,6 +41,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.dcaegen2.services.prh.integration.junit5.mockito.MockitoExtension; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; /** * @author Przemysław Wąsala on 4/9/18 @@ -78,8 +80,16 @@ class PrhAppConfigTest { private static PrhAppConfig prhAppConfig; private static AppConfig appConfig; - private static String filePath = Objects - .requireNonNull(PrhAppConfigTest.class.getClassLoader().getResource(PRH_ENDPOINTS)).getFile(); + private static InputStream inputStream; + + static { + try { + inputStream = Objects + .requireNonNull(PrhAppConfigTest.class.getClassLoader().getResource(PRH_ENDPOINTS)).openStream(); + } catch (IOException e) { + e.printStackTrace(); + } + } @BeforeEach void setUp() { @@ -88,17 +98,15 @@ class PrhAppConfigTest { } @Test - void whenApplicationWasStarted_FilePathIsSet() { + void whenApplicationWasStarted_FilePathIsSet() throws IOException { // // When // - prhAppConfig.setFilepath(filePath); + doReturn(inputStream).when(prhAppConfig).getInputStream(anyString()); // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(0)).initFileStreamReader(); - Assertions.assertEquals(filePath, prhAppConfig.getFilepath()); } @Test @@ -112,7 +120,7 @@ class PrhAppConfigTest { // // When // - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); prhAppConfig.initFileStreamReader(); appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration(); @@ -121,7 +129,6 @@ class PrhAppConfigTest { // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNotNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -136,12 +143,15 @@ class PrhAppConfigTest { } @Test - void whenFileIsNotExist_ThrowIoException() { + void whenFileIsNotExist_ThrowIoException() throws IOException { // // Given + InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( + StandardCharsets.UTF_8))); + Resource resource = spy(new InputStreamResource(inputStream)); // - filePath = "/temp.json"; - prhAppConfig.setFilepath(filePath); + when(resource.getInputStream()).thenThrow(new IOException()); + prhAppConfig.setResourceFile(resource); // // When // @@ -149,7 +159,6 @@ class PrhAppConfigTest { // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -167,14 +176,13 @@ class PrhAppConfigTest { // // When // - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); prhAppConfig.initFileStreamReader(); // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNotNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -190,7 +198,7 @@ class PrhAppConfigTest { InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( StandardCharsets.UTF_8))); // When - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); JsonElement jsonElement = mock(JsonElement.class); when(jsonElement.isJsonObject()).thenReturn(false); @@ -201,7 +209,6 @@ class PrhAppConfigTest { appConfig.aaiClientConfiguration = prhAppConfig.getAaiClientConfiguration(); // Then - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); -- 2.16.6