- provide endpoint that allows consumers to check if schema creation is necessary
- this can be used for the schema creation job to only run when the schema changed
Issue-ID: AAI-4084
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
Change-Id: I831f8c134b548f460eda26570d45f99f28e048a4
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
+ <!-- only used to have access to the WebTestClient -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-webflux</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<resources>
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2024 Deutsche Telekom. 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.nodeschema;
+
+import java.util.Map;
+
+import lombok.Data;
+import lombok.Builder;
+import lombok.extern.jackson.Jacksonized;
+
+@Data
+@Builder
+@Jacksonized
+public class ChecksumResponse {
+ private final Map<SchemaVersion, Long> checksumMap;
+}
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2024 Deutsche Telekom. 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.nodeschema;
+
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.zip.CRC32;
+import java.util.zip.Checksum;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import org.springframework.web.bind.annotation.RestController;
+
+@Path("/v1")
+@RestController
+public class NodeSchemaChecksumResource {
+
+ private final NodeSchemaService nodeSchemaService;
+ private final ChecksumResponse checksumResponse;
+
+ public NodeSchemaChecksumResource(NodeSchemaService nodeSchemaService, SchemaVersions schemaVersions) {
+ this.nodeSchemaService = nodeSchemaService;
+ Map<SchemaVersion, Long> checksumMap = schemaVersions.getVersions().stream()
+ .collect(Collectors.toMap(
+ version -> version,
+ version -> getChecksumForSchemaVersion(version))
+ );
+ checksumResponse = new ChecksumResponse(checksumMap);
+ }
+
+ @GET
+ @Path("/nodes/checksums")
+ @Produces({"application/json"})
+ public Response getChecksumByVersion(@Context HttpHeaders headers, @Context UriInfo info) {
+ return Response.ok(checksumResponse).build();
+ }
+
+ private Long getChecksumForSchemaVersion(SchemaVersion version) {
+ Optional<String> optionalSchema = nodeSchemaService.fetch(version.toString());
+ if (optionalSchema.isPresent()) {
+ return getCRC32Checksum(optionalSchema.get().getBytes());
+ }
+ return 0L;
+ }
+
+ private long getCRC32Checksum(byte[] bytes) {
+ Checksum crc32 = new CRC32();
+ crc32.update(bytes, 0, bytes.length);
+ return crc32.getValue();
+ }
+
+}
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.NodeSchemaChecksumResource;
import org.onap.aai.schemaservice.nodeschema.NodeSchemaResource;
import org.onap.aai.schemaservice.query.QueryResource;
import org.onap.aai.schemaservice.versions.VersionResource;
register(NodeSchemaResource.class);
register(QueryResource.class);
register(EdgeResource.class);
+ register(NodeSchemaChecksumResource.class);
// Request Filters
registerFilters(ContainerRequestFilter.class);
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = SchemaServiceApp.class)
-@TestPropertySource(locations = "classpath:application-test.properties")
+// @TestPropertySource(locations = "classpath:application-test.properties")
@ContextConfiguration(initializers = PropertyPasswordConfiguration.class)
@Import(SchemaServiceTestConfiguration.class)
public class SchemaServiceTest {
--- /dev/null
+package org.onap.aai.schemaservice;
+
+import java.time.Duration;
+import java.util.Collections;
+
+import org.onap.aai.schemaservice.nodeschema.SchemaVersions;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Lazy;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.reactive.server.WebTestClient;
+
+@TestConfiguration
+public class WebClientConfiguration {
+
+ @Lazy
+ @Bean
+ WebTestClient webTestClient(@LocalServerPort int port) {
+ return WebTestClient.bindToServer()
+ .baseUrl("http://localhost:" + port + "/aai/schema-service")
+ .responseTimeout(Duration.ofSeconds(300))
+ .defaultHeaders(headers -> {
+ headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
+ headers.set("X-FromAppId", "test");
+ headers.set("X-TransactionId", "someTransaction");
+ headers.setBasicAuth("AAI", "AAI");
+ })
+ .build();
+ }
+}
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2024 Deutsche Telekom. 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.nodeschema;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.onap.aai.exceptions.AAIException;
+import org.onap.aai.schemaservice.WebClientConfiguration;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Import;
+import org.springframework.test.web.reactive.server.WebTestClient;
+
+import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
+
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@Import(WebClientConfiguration.class)
+public class NodeSchemaChecksumResourceTest {
+
+ @Autowired
+ WebTestClient client;
+
+ @Autowired
+ SchemaVersions schemaVersions;
+
+ @BeforeAll
+ 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"));
+ }
+
+ @Test
+ public void thatChecksumsCanBeRetrieved() {
+ ChecksumResponse response = client.get()
+ .uri("/v1/nodes/checksums")
+ .exchange()
+ .expectStatus().isOk()
+ .returnResult(ChecksumResponse.class)
+ .getResponseBody()
+ .blockFirst();
+ assertEquals(schemaVersions.getVersions().size(), response.getChecksumMap().size());
+ }
+
+}
spring.application.name=aai-schema-service
spring.jersey.type=filter
+spring.main.allow-bean-definition-overriding=true
+
server.servlet.context-path=${schema.uri.base.path}
spring.autoconfigure.exclude=\
# Schema Version Related Attributes
schema.uri.base.path=/aai/schema-service
# Lists all of the versions in the schema
-schema.version.list=v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30
+schema.version.list=v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30
# Specifies from which version should the depth parameter to default to zero
schema.version.depth.start=v10
# Specifies from which version should the related link be displayed in response payload