diff options
Diffstat (limited to 'aai-schema-service')
8 files changed, 478 insertions, 37 deletions
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 @@ <maven.skip.tests>true</maven.skip.tests> - <jacoco.line.coverage.limit>0.68</jacoco.line.coverage.limit> + <jacoco.line.coverage.limit>0.44</jacoco.line.coverage.limit> <!-- This will be used for the docker images as the default format of maven build has issues --> <maven.build.timestamp.format>yyyyMMdd'T'HHmmss'Z'</maven.build.timestamp.format> @@ -407,6 +407,22 @@ <artifactId>spring-boot-test</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <version>${mockito.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-junit</artifactId> + <version>${hamcrest.junit.version}</version> + </dependency> </dependencies> <build> <resources> @@ -477,40 +493,6 @@ <targetPath>${aai.build.directory}/lib/</targetPath> <filtering>false</filtering> </resource> - <resource> - <directory>${project.basedir}/../aai-schema/src/main/resources/</directory> - <includes> - <include>**/oxm/**/*.xml</include> - <include>**/dbedgerules/**/*.json</include> - </includes> - <targetPath>${project.basedir}/src/main/resources/schema</targetPath> - <filtering>false</filtering> - </resource> - <resource> - <directory>${project.basedir}/../aai-queries/src/main/resources/schema</directory> - <includes> - <include>**/query/**/*.json</include> - </includes> - <targetPath>${project.basedir}/src/main/resources/schema</targetPath> - <filtering>false</filtering> - </resource> - <resource> - <directory>${project.basedir}/../aai-schema/src/main/resources/</directory> - <includes> - <include>**/oxm/**/*.xml</include> - <include>**/dbedgerules/**/*.json</include> - </includes> - <targetPath>${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/</targetPath> - <filtering>false</filtering> - </resource> - <resource> - <directory>${project.basedir}/../aai-queries/src/main/resources/schema</directory> - <includes> - <include>**/query/**/*.json</include> - </includes> - <targetPath>${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/</targetPath> - <filtering>false</filtering> - </resource> </resources> <pluginManagement> <plugins> @@ -596,6 +578,86 @@ </executions> </plugin> <plugin> + <artifactId>maven-resources-plugin</artifactId> + <version>2.7</version> + <executions> + <execution> + <id>copy-oxm-edgerules-project</id> + <phase>initialize</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.basedir}/src/main/resources/schema</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../aai-schema/src/main/resources/</directory> + <includes> + <include>**/oxm/**/*.xml</include> + <include>**/dbedgerules/**/*.json</include> + </includes> + </resource> + </resources> + </configuration> + </execution> + <execution> + <id>copy-queries-project</id> + <phase>initialize</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.basedir}/src/main/resources/schema</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../aai-queries/src/main/resources/schema</directory> + <includes> + <include>**/query/**/*.json</include> + </includes> + </resource> + </resources> + </configuration> + </execution> + <execution> + <id>copy-oxm-edgerules</id> + <phase>initialize</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../aai-schema/src/main/resources/</directory> + <includes> + <include>**/oxm/**/*.xml</include> + <include>**/dbedgerules/**/*.json</include> + </includes> + </resource> + </resources> + </configuration> + </execution> + <execution> + <id>copy-queries</id> + <phase>initialize</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>${project.build.directory}/swm/package/nix/dist_files/opt/app/${project.artifactId}/appconfig/schema/</outputDirectory> + <resources> + <resource> + <directory>${project.basedir}/../aai-queries/src/main/resources/schema</directory> + <includes> + <include>**/query/**/*.json</include> + </includes> + </resource> + </resources> + </configuration> + </execution> + </executions> + </plugin> + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> 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<String> 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 |