From 47fe584b69f0631aa151c7e6a28378d5b712006c Mon Sep 17 00:00:00 2001 From: "Kajur, Harish (vk250x)" Date: Tue, 22 Jan 2019 17:11:02 -0500 Subject: Add the queries endpoint and files related to the queries endpoint Issue-ID: AAI-1864 Change-Id: I50054dbaa5b069f0cdbe5b0dc0ce8c06a189589a Signed-off-by: Kajur, Harish (vk250x) --- aai-schema-service/.gitignore | 2 +- aai-schema-service/pom.xml | 132 ++++++++++++++----- .../aai/schemaservice/query/QueryResource.java | 44 +++++++ .../onap/aai/schemaservice/query/QueryService.java | 68 ++++++++++ .../aai/schemaservice/web/JerseyConfiguration.java | 2 + .../onap/aai/schemaservice/SchemaServiceTest.java | 142 +++++++++++++++++++++ .../SchemaServiceTestConfiguration.java | 123 ++++++++++++++++++ .../src/test/resources/application-test.properties | 2 +- 8 files changed, 478 insertions(+), 37 deletions(-) create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryResource.java create mode 100644 aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryService.java create mode 100644 aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTest.java create mode 100644 aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTestConfiguration.java (limited to 'aai-schema-service') diff --git a/aai-schema-service/.gitignore b/aai-schema-service/.gitignore index 3b2d955..35183c9 100644 --- a/aai-schema-service/.gitignore +++ b/aai-schema-service/.gitignore @@ -14,4 +14,4 @@ bundleconfig-local/etc/logback.xml *.iml **/dbedgerules/** **/oxm/** -**/query/** +**/query/*.json diff --git a/aai-schema-service/pom.xml b/aai-schema-service/pom.xml index 658e5ec..3eeefd1 100644 --- a/aai-schema-service/pom.xml +++ b/aai-schema-service/pom.xml @@ -78,7 +78,7 @@ true - 0.68 + 0.44 yyyyMMdd'T'HHmmss'Z' @@ -407,6 +407,22 @@ spring-boot-test test + + org.springframework + spring-test + test + + + org.mockito + mockito-all + ${mockito.version} + test + + + org.hamcrest + hamcrest-junit + ${hamcrest.junit.version} + @@ -477,40 +493,6 @@ ${aai.build.directory}/lib/ false - - ${project.basedir}/../aai-schema/src/main/resources/ - - **/oxm/**/*.xml - **/dbedgerules/**/*.json - - ${project.basedir}/src/main/resources/schema - false - - - ${project.basedir}/../aai-queries/src/main/resources/schema - - **/query/**/*.json - - ${project.basedir}/src/main/resources/schema - false - - - ${project.basedir}/../aai-schema/src/main/resources/ - - **/oxm/**/*.xml - **/dbedgerules/**/*.json - - ${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/ - false - - - ${project.basedir}/../aai-queries/src/main/resources/schema - - **/query/**/*.json - - ${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/ - false - @@ -595,6 +577,86 @@ + + maven-resources-plugin + 2.7 + + + copy-oxm-edgerules-project + initialize + + copy-resources + + + ${project.basedir}/src/main/resources/schema + + + ${project.basedir}/../aai-schema/src/main/resources/ + + **/oxm/**/*.xml + **/dbedgerules/**/*.json + + + + + + + copy-queries-project + initialize + + copy-resources + + + ${project.basedir}/src/main/resources/schema + + + ${project.basedir}/../aai-queries/src/main/resources/schema + + **/query/**/*.json + + + + + + + copy-oxm-edgerules + initialize + + copy-resources + + + ${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/ + + + ${project.basedir}/../aai-schema/src/main/resources/ + + **/oxm/**/*.xml + **/dbedgerules/**/*.json + + + + + + + copy-queries + initialize + + copy-resources + + + ${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/ + + + ${project.basedir}/../aai-queries/src/main/resources/schema + + **/query/**/*.json + + + + + + + org.apache.maven.plugins maven-surefire-plugin diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryResource.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryResource.java new file mode 100644 index 0000000..0137d1d --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryResource.java @@ -0,0 +1,44 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T 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.aai.schemaservice.query; + +import org.springframework.beans.factory.annotation.Autowired; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +@Path("/v1") +public class QueryResource { + + private QueryService queryService; + + @Autowired + public QueryResource(QueryService queryService){ + this.queryService = queryService; + } + + @GET + @Path("/stored-queries") + public Response retrieveStoredQueries(){ + return Response.ok().entity(queryService.getStoredQueries()).build(); + } + +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryService.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryService.java new file mode 100644 index 0000000..badb09a --- /dev/null +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/query/QueryService.java @@ -0,0 +1,68 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T 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.aai.schemaservice.query; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; + +@Service +public class QueryService { + + private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(QueryService.class); + + private String queryLocation; + + private String storedQueriesContent; + + public QueryService(@Value("${schema.query.location}") String queryLocation){ + this.queryLocation = queryLocation; + } + + @PostConstruct + public void initialize() throws IOException { + + String fileName = queryLocation + File.separator + "stored-queries.json"; + LOGGER.debug("Loading the following stored queries file {}", fileName); + + StringBuilder contentBuilder = new StringBuilder(); + + try(Stream stream = Files.lines(Paths.get(fileName), StandardCharsets.UTF_8)){ + stream.forEach(s -> contentBuilder.append(s)); + } + + storedQueriesContent = contentBuilder.toString(); + + LOGGER.trace("Contents of the stored query file {}", storedQueriesContent); + } + + public String getStoredQueries(){ + return storedQueriesContent; + } +} diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java index bdd7946..9fbe3a5 100644 --- a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java +++ b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java @@ -23,6 +23,7 @@ import org.glassfish.jersey.server.ResourceConfig; import org.onap.aai.schemaservice.edges.EdgeResource; import org.onap.aai.schemaservice.healthcheck.EchoResource; import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource; +import org.onap.aai.schemaservice.query.QueryResource; import org.onap.aai.schemaservice.versions.VersionResource; import org.reflections.Reflections; import org.springframework.beans.factory.annotation.Autowired; @@ -53,6 +54,7 @@ public class JerseyConfiguration extends ResourceConfig { register(VersionResource.class); register(EchoResource.class); register(NodeSchemaResource.class); + register(QueryResource.class); register(EdgeResource.class); //Request Filters diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTest.java new file mode 100644 index 0000000..53a3e14 --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTest.java @@ -0,0 +1,142 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T 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.aai.schemaservice; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.schemaservice.config.PropertyPasswordConfiguration; +import org.onap.aai.util.AAIConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.*; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; + +import java.io.UnsupportedEncodingException; +import java.util.Base64; +import java.util.Collections; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SchemaServiceApp.class) +@TestPropertySource(locations = "classpath:application-test.properties") +@ContextConfiguration(initializers = PropertyPasswordConfiguration.class) +@Import(SchemaServiceTestConfiguration.class) +@RunWith(SpringRunner.class) +public class SchemaServiceTest { + + private HttpHeaders headers; + + private HttpEntity httpEntity; + + private String baseUrl; + + @Autowired + private RestTemplate restTemplate; + + private String authorization; + + @LocalServerPort + protected int randomPort; + + @BeforeClass + public static void setupConfig() throws AAIException { + System.setProperty("AJSC_HOME", "./"); + System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/"); + System.out.println("Current directory: " + System.getProperty("user.dir")); + } + + @Before + public void setup() throws AAIException, UnsupportedEncodingException { + + AAIConfig.init(); + headers = new HttpHeaders(); + + authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8")); + + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.add("Real-Time", "true"); + headers.add("X-FromAppId", "JUNIT"); + headers.add("X-TransactionId", "JUNIT"); + headers.add("Authorization", "Basic " + authorization); + httpEntity = new HttpEntity(headers); + baseUrl = "https://localhost:" + randomPort; + } + + @Test + public void testGetSchemaAndEdgeRules(){ + + headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_XML)); + headers.setContentType(MediaType.APPLICATION_XML); + headers.add("Real-Time", "true"); + headers.add("X-FromAppId", "JUNIT"); + headers.add("X-TransactionId", "JUNIT"); + headers.add("Authorization", "Basic " + authorization); + httpEntity = new HttpEntity(headers); + + ResponseEntity responseEntity; + + responseEntity = restTemplate.exchange( + baseUrl + "/aai/schema-service/v1/nodes?version=v15", + HttpMethod.GET, + httpEntity, + String.class + ); + assertThat(responseEntity.getStatusCodeValue(), is(200)); + + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + httpEntity = new HttpEntity(headers); + + responseEntity = restTemplate.exchange( + baseUrl + "/aai/schema-service/v1/edgerules?version=v15", + HttpMethod.GET, + httpEntity, + String.class + ); + + assertThat(responseEntity.getStatusCodeValue(), is(200)); + } + + @Test + public void testGetStoredQueriesSuccess(){ + + ResponseEntity responseEntity; + + responseEntity = restTemplate.exchange( + baseUrl + "/aai/schema-service/v1/stored-queries", + HttpMethod.GET, + httpEntity, + String.class + ); + assertThat(responseEntity.getStatusCodeValue(), is(200)); + + } +} diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTestConfiguration.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTestConfiguration.java new file mode 100644 index 0000000..5d4c187 --- /dev/null +++ b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTestConfiguration.java @@ -0,0 +1,123 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T 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.aai.schemaservice; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.util.ResourceUtils; +import org.springframework.web.client.ResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +import javax.net.ssl.SSLContext; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.KeyStore; + +@TestConfiguration +public class SchemaServiceTestConfiguration { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(SchemaServiceTestConfiguration.class); + + @Autowired + private Environment env; + + /** + * Create a RestTemplate bean, using the RestTemplateBuilder provided + * by the auto-configuration. + */ + @Bean + RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { + + char[] trustStorePassword = env.getProperty("server.ssl.trust-store-password").toCharArray(); + char[] keyStorePassword = env.getProperty("server.ssl.key-store-password").toCharArray(); + + String keyStore = env.getProperty("server.ssl.key-store"); + String trustStore = env.getProperty("server.ssl.trust-store"); + + SSLContextBuilder sslContextBuilder = SSLContextBuilder.create(); + + if(env.acceptsProfiles("two-way-ssl")){ + sslContextBuilder = sslContextBuilder.loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword); + } + + SSLContext sslContext = sslContextBuilder + .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword) + .build(); + + HttpClient client = HttpClients.custom() + .setSSLContext(sslContext) + .setSSLHostnameVerifier((s, sslSession) -> true) + .build(); + + RestTemplate restTemplate = builder + .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) + .build(); + + restTemplate.setErrorHandler(new ResponseErrorHandler() { + @Override + public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException { + if (clientHttpResponse.getStatusCode() != HttpStatus.OK) { + + logger.debug("Status code: " + clientHttpResponse.getStatusCode()); + + if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) { + logger.debug("Call returned a error 403 forbidden resposne "); + return true; + } + + if(clientHttpResponse.getRawStatusCode() % 100 == 5){ + logger.debug("Call returned a error " + clientHttpResponse.getStatusText()); + return true; + } + } + + return false; + } + + @Override + public void handleError(ClientHttpResponse clientHttpResponse) throws IOException { + } + }); + + return restTemplate; + } + + private KeyStore loadPfx(String file, char[] password) throws Exception { + KeyStore keyStore = KeyStore.getInstance("PKCS12"); + File key = ResourceUtils.getFile(file); + try (InputStream in = new FileInputStream(key)) { + keyStore.load(in, password); + } + return keyStore; + } +} diff --git a/aai-schema-service/src/test/resources/application-test.properties b/aai-schema-service/src/test/resources/application-test.properties index 2b3bd58..2e0cda1 100644 --- a/aai-schema-service/src/test/resources/application-test.properties +++ b/aai-schema-service/src/test/resources/application-test.properties @@ -14,7 +14,7 @@ jetty.threadPool.minThreads=8 server.tomcat.max-idle-time=60000 # If you get an application startup failure that the port is already taken # If thats not it, please check if the key-store file path makes sense -server.local.startpath=aai-schema-service/src/main/resources/ +server.local.startpath=src/main/resources/ server.basic.auth.location=${server.local.startpath}etc/auth/realm.properties server.port=8452 -- cgit 1.2.3-korg