Add checksum endpoint to schema-service 90/139690/4
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Sun, 8 Dec 2024 19:41:19 +0000 (20:41 +0100)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Mon, 9 Dec 2024 07:39:06 +0000 (08:39 +0100)
- 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

aai-schema-service/pom.xml
aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java [new file with mode: 0644]
aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java [new file with mode: 0644]
aai-schema-service/src/main/java/org/onap/aai/schemaservice/web/JerseyConfiguration.java
aai-schema-service/src/test/java/org/onap/aai/schemaservice/SchemaServiceTest.java
aai-schema-service/src/test/java/org/onap/aai/schemaservice/WebClientConfiguration.java [new file with mode: 0644]
aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResourceTest.java [new file with mode: 0644]
aai-schema-service/src/test/resources/application.properties [moved from aai-schema-service/src/test/resources/application-test.properties with 96% similarity]

index f69b2dd..3d02be5 100644 (file)
             <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>
diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/ChecksumResponse.java
new file mode 100644 (file)
index 0000000..bf61eb8
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * ============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;
+}
diff --git a/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java b/aai-schema-service/src/main/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResource.java
new file mode 100644 (file)
index 0000000..e3b57df
--- /dev/null
@@ -0,0 +1,78 @@
+/**
+ * ============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();
+    }
+
+}
index 299c813..c0740f1 100644 (file)
@@ -32,6 +32,7 @@ import javax.ws.rs.container.ContainerResponseFilter;
 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;
@@ -58,6 +59,7 @@ public class JerseyConfiguration extends ResourceConfig {
         register(NodeSchemaResource.class);
         register(QueryResource.class);
         register(EdgeResource.class);
+        register(NodeSchemaChecksumResource.class);
 
         // Request Filters
         registerFilters(ContainerRequestFilter.class);
index 49c3c33..f2f91cb 100644 (file)
@@ -49,7 +49,7 @@ import org.springframework.web.client.RestTemplate;
 @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 {
diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/WebClientConfiguration.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/WebClientConfiguration.java
new file mode 100644 (file)
index 0000000..613e3d9
--- /dev/null
@@ -0,0 +1,32 @@
+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();
+  }
+}
diff --git a/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResourceTest.java b/aai-schema-service/src/test/java/org/onap/aai/schemaservice/nodeschema/NodeSchemaChecksumResourceTest.java
new file mode 100644 (file)
index 0000000..b22940d
--- /dev/null
@@ -0,0 +1,65 @@
+/**
+ * ============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());
+  }
+
+}
@@ -1,6 +1,8 @@
 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=\
@@ -40,7 +42,7 @@ schema.ingest.file=${server.local.startpath}/application.properties
 # 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