From 4ee5ac279b2ace0a95b72cb622cb524ac86e99f0 Mon Sep 17 00:00:00 2001 From: thcai Date: Tue, 27 Aug 2024 15:42:59 +0200 Subject: [PATCH] test --- .../controller/ConnectorController.java | 6 +- .../controller/ConsumerGroupController.java | 2 +- .../controller/NamespaceController.java | 4 +- .../controller/RoleBindingController.java | 4 +- .../ns4kafka/controller/SchemaController.java | 61 ++++--------- .../ns4kafka/controller/StreamController.java | 4 +- .../ns4kafka/controller/UserController.java | 2 +- .../controller/acl/AclController.java | 4 +- .../connect/ConnectClusterController.java | 5 +- .../generic/ResourceController.java | 4 +- .../quota/ResourceQuotaController.java | 4 +- .../controller/topic/TopicController.java | 8 +- .../ns4kafka/log/ConsoleLogListener.java | 3 +- .../com/michelin/ns4kafka/model/AuditLog.java | 1 + .../ns4kafka/service/SchemaService.java | 8 +- .../controller/SchemaControllerTest.java | 86 +------------------ .../ns4kafka/service/SchemaServiceTest.java | 35 +------- 17 files changed, 48 insertions(+), 193 deletions(-) diff --git a/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java b/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java index 24597153..a711d8b7 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/ConnectorController.java @@ -144,7 +144,7 @@ public Mono> apply(String namespace, @Valid @Body Connec } sendEventLog(connector, status, existingConnector.map(Connector::getSpec).orElse(null), - connector.getSpec()); + connector.getSpec(), ""); return Mono.just(formatHttpResponse(connectorService.createOrUpdate(connector), status)); }); @@ -180,7 +180,7 @@ public Mono> delete(String namespace, String connector, } Connector connectorToDelete = optionalConnector.get(); - sendEventLog(connectorToDelete, ApplyStatus.deleted, connectorToDelete.getSpec(), null); + sendEventLog(connectorToDelete, ApplyStatus.deleted, connectorToDelete.getSpec(), null, ""); return connectorService .delete(ns, optionalConnector.get()) @@ -264,7 +264,7 @@ public Flux importResources(String namespace, @QueryValue(defaultValu return unsynchronizedConnector; } - sendEventLog(unsynchronizedConnector, ApplyStatus.created, null, unsynchronizedConnector.getSpec()); + sendEventLog(unsynchronizedConnector, ApplyStatus.created, null, unsynchronizedConnector.getSpec(), ""); return connectorService.createOrUpdate(unsynchronizedConnector); }); diff --git a/src/main/java/com/michelin/ns4kafka/controller/ConsumerGroupController.java b/src/main/java/com/michelin/ns4kafka/controller/ConsumerGroupController.java index dec5960e..502ee721 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/ConsumerGroupController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/ConsumerGroupController.java @@ -86,7 +86,7 @@ public List resetOffsets(String namespace, St consumerGroupResetOffsets.getSpec().getMethod()); if (!dryrun) { - sendEventLog(consumerGroupResetOffsets, ApplyStatus.changed, null, consumerGroupResetOffsets.getSpec()); + sendEventLog(consumerGroupResetOffsets, ApplyStatus.changed, null, consumerGroupResetOffsets.getSpec(), ""); consumerGroupService.alterConsumerGroupOffsets(ns, consumerGroup, preparedOffsets); } diff --git a/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java b/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java index fb8a5141..52003a06 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/NamespaceController.java @@ -103,7 +103,7 @@ public HttpResponse apply(@Valid @Body Namespace namespace, } sendEventLog(namespace, status, existingNamespace.map(Namespace::getSpec).orElse(null), - namespace.getSpec()); + namespace.getSpec(), ""); return formatHttpResponse(namespaceService.createOrUpdate(namespace), status); } @@ -136,7 +136,7 @@ public HttpResponse delete(String namespace, @QueryValue(defaultValue = "f } var namespaceToDelete = optionalNamespace.get(); - sendEventLog(namespaceToDelete, ApplyStatus.deleted, namespaceToDelete.getSpec(), null); + sendEventLog(namespaceToDelete, ApplyStatus.deleted, namespaceToDelete.getSpec(), null, ""); namespaceService.delete(optionalNamespace.get()); return HttpResponse.noContent(); } diff --git a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java index 38abe5bb..43574da8 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/RoleBindingController.java @@ -89,7 +89,7 @@ public HttpResponse apply(String namespace, @Valid @Body RoleBindin } sendEventLog(roleBinding, status, existingRoleBinding.map(RoleBinding::getSpec).orElse(null), - roleBinding.getSpec()); + roleBinding.getSpec(), ""); roleBindingService.create(roleBinding); return formatHttpResponse(roleBinding, status); } @@ -116,7 +116,7 @@ public HttpResponse delete(String namespace, String name, } var roleBindingToDelete = roleBinding.get(); - sendEventLog(roleBindingToDelete, ApplyStatus.deleted, roleBindingToDelete.getSpec(), null); + sendEventLog(roleBindingToDelete, ApplyStatus.deleted, roleBindingToDelete.getSpec(), null, ""); roleBindingService.delete(roleBindingToDelete); return HttpResponse.noContent(); } diff --git a/src/main/java/com/michelin/ns4kafka/controller/SchemaController.java b/src/main/java/com/michelin/ns4kafka/controller/SchemaController.java index 949ab98d..13535c48 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/SchemaController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/SchemaController.java @@ -132,7 +132,7 @@ public Mono> apply(String namespace, @Valid @Body Schema sc oldSchemas.isEmpty() ? null : oldSchemas.stream() .max(Comparator.comparingInt( (Schema s) -> s.getSpec().getId())), - schema.getSpec()); + schema.getSpec(), ""); return formatHttpResponse(schema, status); }); @@ -163,13 +163,13 @@ public Mono> delete(String namespace, return Mono.error(new ResourceValidationException(SCHEMA, subject, invalidOwner(subject))); } - // delete all versions of the schema - if (version.isEmpty()) { - return schemaService.getLatestSubject(ns, subject) + return version + .map(v -> schemaService.getSubject(ns, subject, v)) + .orElseGet(() -> schemaService.getLatestSubject(ns, subject)) .map(Optional::of) .defaultIfEmpty(Optional.empty()) - .flatMap(latestSubjectOptional -> { - if (latestSubjectOptional.isEmpty()) { + .flatMap(subjectOptional -> { + if (subjectOptional.isEmpty()) { return Mono.just(HttpResponse.notFound()); } @@ -177,46 +177,15 @@ public Mono> delete(String namespace, return Mono.just(HttpResponse.noContent()); } - Schema schemaToDelete = latestSubjectOptional.get(); - sendEventLog(schemaToDelete, ApplyStatus.deleted, schemaToDelete.getSpec(), null); - - return schemaService - .deleteAllVersions(ns, subject) - .map(deletedVersionIds -> HttpResponse.noContent()); + return (version.isEmpty() ? schemaService.deleteAllVersions(ns, subject) : + schemaService.deleteVersion(ns, subject, version.get())) + .map(deletedVersionIds -> { + Schema deletedSchema = subjectOptional.get(); + sendEventLog(deletedSchema, ApplyStatus.deleted, deletedSchema.getSpec(), null, + version.map(v -> "").orElseGet(() -> String.valueOf(deletedVersionIds))); + return HttpResponse.noContent(); + }); }); - } - - // delete a specific version of the schema - return schemaService.getSubject(ns, subject, version.get()) - .map(Optional::of) - .defaultIfEmpty(Optional.empty()) - .flatMap(subjectOptional -> { - if (subjectOptional.isEmpty()) { - return Mono.just(HttpResponse.notFound()); - } - - if (dryrun) { - return Mono.just(HttpResponse.noContent()); - } - - Schema schemaToDelete = subjectOptional.get(); - return schemaService - .deleteVersion(ns, subject, version.get()) - .flatMap(optionalNewLatestSubject -> { - // there was only one version & it was deleted, so the schema is deleted in ns4 - if (optionalNewLatestSubject.isEmpty()) { - sendEventLog(schemaToDelete, ApplyStatus.deleted, - schemaToDelete.getSpec(), null); - } - // there were multiple versions & latest version was deleted, so the schema is changed in ns4 - if (optionalNewLatestSubject.isPresent() && optionalNewLatestSubject.get() - .getSpec().getVersion() < schemaToDelete.getSpec().getVersion()) { - sendEventLog(schemaToDelete, ApplyStatus.changed, - schemaToDelete.getSpec(), optionalNewLatestSubject.get().getSpec()); - } - return Mono.just(HttpResponse.noContent()); - }); - }); } /** @@ -259,7 +228,7 @@ public Mono> config(String namespace, @Pa .updateSubjectCompatibility(ns, latestSubjectOptional.get(), compatibility) .map(schemaCompatibility -> { sendEventLog(latestSubjectOptional.get(), ApplyStatus.changed, - latestSubjectOptional.get().getSpec().getCompatibility(), compatibility); + latestSubjectOptional.get().getSpec().getCompatibility(), compatibility, ""); return HttpResponse.ok(state); }); diff --git a/src/main/java/com/michelin/ns4kafka/controller/StreamController.java b/src/main/java/com/michelin/ns4kafka/controller/StreamController.java index d8c17001..66dc5d00 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/StreamController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/StreamController.java @@ -94,7 +94,7 @@ HttpResponse apply(String namespace, @Body @Valid KafkaStream strea } sendEventLog(stream, status, existingStream.map(KafkaStream::getMetadata).orElse(null), - stream.getMetadata()); + stream.getMetadata(), ""); return formatHttpResponse(streamService.create(stream), status); } @@ -126,7 +126,7 @@ HttpResponse delete(String namespace, String stream, @QueryValue(defaultVa } var streamToDelete = optionalStream.get(); - sendEventLog(streamToDelete, ApplyStatus.deleted, streamToDelete.getMetadata(), null); + sendEventLog(streamToDelete, ApplyStatus.deleted, streamToDelete.getMetadata(), null, ""); streamService.delete(ns, optionalStream.get()); return HttpResponse.noContent(); } diff --git a/src/main/java/com/michelin/ns4kafka/controller/UserController.java b/src/main/java/com/michelin/ns4kafka/controller/UserController.java index 90b60c4c..bb049e18 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/UserController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/UserController.java @@ -61,7 +61,7 @@ public HttpResponse resetPassword(String namespace, Stri .build()) .build(); - sendEventLog(response, ApplyStatus.changed, null, response.getSpec()); + sendEventLog(response, ApplyStatus.changed, null, response.getSpec(), ""); return HttpResponse.ok(response); } } diff --git a/src/main/java/com/michelin/ns4kafka/controller/acl/AclController.java b/src/main/java/com/michelin/ns4kafka/controller/acl/AclController.java index a9c0bcc6..19bcdab0 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/acl/AclController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/acl/AclController.java @@ -148,7 +148,7 @@ public HttpResponse apply(Authentication authentication, Str } sendEventLog(accessControlEntry, status, existingAcl.map(AccessControlEntry::getSpec).orElse(null), - accessControlEntry.getSpec()); + accessControlEntry.getSpec(), ""); return formatHttpResponse(aclService.create(accessControlEntry), status); } @@ -181,7 +181,7 @@ public HttpResponse delete(Authentication authentication, String namespace return HttpResponse.noContent(); } - sendEventLog(accessControlEntry, ApplyStatus.deleted, accessControlEntry.getSpec(), null); + sendEventLog(accessControlEntry, ApplyStatus.deleted, accessControlEntry.getSpec(), null, ""); aclService.delete(accessControlEntry); return HttpResponse.noContent(); diff --git a/src/main/java/com/michelin/ns4kafka/controller/connect/ConnectClusterController.java b/src/main/java/com/michelin/ns4kafka/controller/connect/ConnectClusterController.java index 91e5aad6..f2d3c61f 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/connect/ConnectClusterController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/connect/ConnectClusterController.java @@ -116,7 +116,8 @@ public Mono> apply(String namespace, @Body @Valid C } sendEventLog(connectCluster, status, - existingConnectCluster.map(ConnectCluster::getSpec).orElse(null), connectCluster.getSpec()); + existingConnectCluster.map(ConnectCluster::getSpec).orElse(null), + connectCluster.getSpec(), ""); return Mono.just(formatHttpResponse(connectClusterService.create(connectCluster), status)); }); @@ -161,7 +162,7 @@ public HttpResponse delete(String namespace, String connectCluster, } ConnectCluster connectClusterToDelete = optionalConnectCluster.get(); - sendEventLog(connectClusterToDelete, ApplyStatus.deleted, connectClusterToDelete.getSpec(), null); + sendEventLog(connectClusterToDelete, ApplyStatus.deleted, connectClusterToDelete.getSpec(), null, ""); connectClusterService.delete(connectClusterToDelete); return HttpResponse.noContent(); diff --git a/src/main/java/com/michelin/ns4kafka/controller/generic/ResourceController.java b/src/main/java/com/michelin/ns4kafka/controller/generic/ResourceController.java index d730cf95..95be7466 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/generic/ResourceController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/generic/ResourceController.java @@ -36,10 +36,10 @@ public HttpResponse formatHttpResponse(T body, ApplyStatus status) { * @param after the resource after the operation */ public void sendEventLog(MetadataResource resource, ApplyStatus operation, Object before, - Object after) { + Object after, String version) { AuditLog auditLog = new AuditLog(securityService.username().orElse(""), securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN), Date.from(Instant.now()), - resource.getKind(), resource.getMetadata(), operation, before, after); + resource.getKind(), resource.getMetadata(), operation, before, after, version); applicationEventPublisher.publishEvent(auditLog); } } diff --git a/src/main/java/com/michelin/ns4kafka/controller/quota/ResourceQuotaController.java b/src/main/java/com/michelin/ns4kafka/controller/quota/ResourceQuotaController.java index 991f3f2e..2ad7839f 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/quota/ResourceQuotaController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/quota/ResourceQuotaController.java @@ -98,7 +98,7 @@ public HttpResponse apply(String namespace, @Body @Valid Resource } sendEventLog(quota, status, resourceQuotaOptional.map(ResourceQuota::getSpec).orElse(null), - quota.getSpec()); + quota.getSpec(), ""); return formatHttpResponse(resourceQuotaService.create(quota), status); } @@ -125,7 +125,7 @@ public HttpResponse delete(String namespace, String name, } ResourceQuota resourceQuotaToDelete = resourceQuota.get(); - sendEventLog(resourceQuotaToDelete, ApplyStatus.deleted, resourceQuotaToDelete.getSpec(), null); + sendEventLog(resourceQuotaToDelete, ApplyStatus.deleted, resourceQuotaToDelete.getSpec(), null, ""); resourceQuotaService.delete(resourceQuotaToDelete); return HttpResponse.noContent(); } diff --git a/src/main/java/com/michelin/ns4kafka/controller/topic/TopicController.java b/src/main/java/com/michelin/ns4kafka/controller/topic/TopicController.java index 9c644e6a..17981622 100644 --- a/src/main/java/com/michelin/ns4kafka/controller/topic/TopicController.java +++ b/src/main/java/com/michelin/ns4kafka/controller/topic/TopicController.java @@ -144,7 +144,7 @@ public HttpResponse apply(String namespace, @Valid @Body Topic topic, return formatHttpResponse(topic, status); } - sendEventLog(topic, status, existingTopic.map(Topic::getSpec).orElse(null), topic.getSpec()); + sendEventLog(topic, status, existingTopic.map(Topic::getSpec).orElse(null), topic.getSpec(), ""); return formatHttpResponse(topicService.create(topic), status); } @@ -178,7 +178,7 @@ public HttpResponse delete(String namespace, String topic, } Topic topicToDelete = optionalTopic.get(); - sendEventLog(topicToDelete, ApplyStatus.deleted, topicToDelete.getSpec(), null); + sendEventLog(topicToDelete, ApplyStatus.deleted, topicToDelete.getSpec(), null, ""); topicService.delete(optionalTopic.get()); return HttpResponse.noContent(); @@ -214,7 +214,7 @@ public List importResources(String namespace, @QueryValue(defaultValue = return unsynchronizedTopics .stream() .map(topic -> { - sendEventLog(topic, ApplyStatus.created, null, topic.getSpec()); + sendEventLog(topic, ApplyStatus.created, null, topic.getSpec(), ""); return topicService.create(topic); }) .toList(); @@ -256,7 +256,7 @@ public List deleteRecords(String namespace, String topic, if (dryrun) { deletedRecords = recordsToDelete; } else { - sendEventLog(optionalTopic.get(), ApplyStatus.deleted, null, null); + sendEventLog(optionalTopic.get(), ApplyStatus.deleted, null, null, ""); deletedRecords = topicService.deleteRecords(optionalTopic.get(), recordsToDelete); } diff --git a/src/main/java/com/michelin/ns4kafka/log/ConsoleLogListener.java b/src/main/java/com/michelin/ns4kafka/log/ConsoleLogListener.java index a3f569ca..9bc093a3 100644 --- a/src/main/java/com/michelin/ns4kafka/log/ConsoleLogListener.java +++ b/src/main/java/com/michelin/ns4kafka/log/ConsoleLogListener.java @@ -17,12 +17,13 @@ public class ConsoleLogListener implements ApplicationEventListener { @Override public void onApplicationEvent(AuditLog event) { - log.info("{} {} {} {} {} in namespace {} on cluster {}.", + log.info("{} {} {} {}{} {} in namespace {} on cluster {}.", event.isAdmin() ? "Admin" : "User", event.getUser(), event.getOperation(), event.getKind(), event.getMetadata().getName(), + event.getVersion().isEmpty() ? "" : " version " + event.getVersion(), event.getMetadata().getNamespace(), event.getMetadata().getCluster() ); diff --git a/src/main/java/com/michelin/ns4kafka/model/AuditLog.java b/src/main/java/com/michelin/ns4kafka/model/AuditLog.java index f9174148..c3e3bb75 100644 --- a/src/main/java/com/michelin/ns4kafka/model/AuditLog.java +++ b/src/main/java/com/michelin/ns4kafka/model/AuditLog.java @@ -23,4 +23,5 @@ public class AuditLog { private ApplyStatus operation; private Object before; private Object after; + private String version; } diff --git a/src/main/java/com/michelin/ns4kafka/service/SchemaService.java b/src/main/java/com/michelin/ns4kafka/service/SchemaService.java index 4da64c72..9a9d3a37 100644 --- a/src/main/java/com/michelin/ns4kafka/service/SchemaService.java +++ b/src/main/java/com/michelin/ns4kafka/service/SchemaService.java @@ -246,17 +246,13 @@ public Mono deleteAllVersions(Namespace namespace, String subject) { * @param version The version of the schema to delete * @return The latest subject after deletion */ - public Mono> deleteVersion(Namespace namespace, String subject, String version) { + public Mono deleteVersion(Namespace namespace, String subject, String version) { return schemaRegistryClient .deleteSubjectVersion(namespace.getMetadata().getCluster(), subject, version, false) .flatMap(softDeletedVersionIds -> schemaRegistryClient .deleteSubjectVersion(namespace.getMetadata().getCluster(), subject, Integer.toString(softDeletedVersionIds), true) - .flatMap(hardDeletedVersionId -> getLatestSubject(namespace, subject) - .map(Optional::of) - .defaultIfEmpty(Optional.empty()) - .flatMap(Mono::just) - )); + ); } /** diff --git a/src/test/java/com/michelin/ns4kafka/controller/SchemaControllerTest.java b/src/test/java/com/michelin/ns4kafka/controller/SchemaControllerTest.java index f08a360d..39228326 100644 --- a/src/test/java/com/michelin/ns4kafka/controller/SchemaControllerTest.java +++ b/src/test/java/com/michelin/ns4kafka/controller/SchemaControllerTest.java @@ -514,10 +514,9 @@ void shouldDeleteAllSchemaVersions() { } @Test - void shouldDeleteOldSchemaVersionAndNotSendEventLog() { + void shouldDeleteSchemaVersion() { Namespace namespace = buildNamespace(); Schema schema1 = buildSchema(); - Schema schema2 = buildSchemaV2(); when(namespaceService.findByName("myNamespace")) .thenReturn(Optional.of(namespace)); @@ -526,88 +525,7 @@ void shouldDeleteOldSchemaVersionAndNotSendEventLog() { when(schemaService.getSubject(namespace, "prefix.subject-value", "1")) .thenReturn(Mono.just(schema1)); when(schemaService.deleteVersion(namespace, "prefix.subject-value", "1")) - .thenReturn(Mono.just(Optional.of(schema2))); - - StepVerifier.create(schemaController.delete("myNamespace", "prefix.subject-value", Optional.of("1"), false)) - .consumeNextWith(response -> assertEquals(HttpStatus.NO_CONTENT, response.getStatus())) - .verifyComplete(); - - verify(applicationEventPublisher, never()).publishEvent(any()); - } - - @Test - void shouldDeleteLatestSchemaVersionBySpecifyingLatest() { - Namespace namespace = buildNamespace(); - Schema schema1 = buildSchema(); - Schema schema2 = buildSchemaV2(); - - when(namespaceService.findByName("myNamespace")) - .thenReturn(Optional.of(namespace)); - when(schemaService.isNamespaceOwnerOfSubject(namespace, "prefix.subject-value")) - .thenReturn(true); - when(schemaService.getSubject(namespace, "prefix.subject-value", "latest")) - .thenReturn(Mono.just(schema2)); - when(schemaService.deleteVersion(namespace, "prefix.subject-value", "latest")) - .thenReturn(Mono.just(Optional.of(schema1))); - when(securityService.username()) - .thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) - .thenReturn(false); - doNothing().when(applicationEventPublisher).publishEvent(any()); - - StepVerifier.create(schemaController.delete("myNamespace", "prefix.subject-value", - Optional.of("latest"), false)) - .consumeNextWith(response -> assertEquals(HttpStatus.NO_CONTENT, response.getStatus())) - .verifyComplete(); - - verify(applicationEventPublisher).publishEvent(any()); - } - - @Test - void shouldDeleteLatestSchemaVersionBySpecifyingVersion() { - Namespace namespace = buildNamespace(); - Schema schema1 = buildSchema(); - Schema schema2 = buildSchemaV2(); - - when(namespaceService.findByName("myNamespace")) - .thenReturn(Optional.of(namespace)); - when(schemaService.isNamespaceOwnerOfSubject(namespace, "prefix.subject-value")) - .thenReturn(true); - when(schemaService.getSubject(namespace, "prefix.subject-value", "2")) - .thenReturn(Mono.just(schema2)); - when(schemaService.deleteVersion(namespace, "prefix.subject-value", "2")) - .thenReturn(Mono.just(Optional.of(schema1))); - when(securityService.username()) - .thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) - .thenReturn(false); - doNothing().when(applicationEventPublisher).publishEvent(any()); - - StepVerifier.create(schemaController.delete("myNamespace", "prefix.subject-value", Optional.of("2"), false)) - .consumeNextWith(response -> assertEquals(HttpStatus.NO_CONTENT, response.getStatus())) - .verifyComplete(); - - verify(applicationEventPublisher).publishEvent(any()); - } - - @Test - void shouldDeleteLastSchemaVersion() { - Namespace namespace = buildNamespace(); - Schema schema = buildSchema(); - - when(namespaceService.findByName("myNamespace")) - .thenReturn(Optional.of(namespace)); - when(schemaService.isNamespaceOwnerOfSubject(namespace, "prefix.subject-value")) - .thenReturn(true); - when(schemaService.getSubject(namespace, "prefix.subject-value", "1")) - .thenReturn(Mono.just(schema)); - when(schemaService.deleteVersion(namespace, "prefix.subject-value", "1")) - .thenReturn(Mono.just(Optional.empty())); - when(securityService.username()) - .thenReturn(Optional.of("test-user")); - when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)) - .thenReturn(false); - doNothing().when(applicationEventPublisher).publishEvent(any()); + .thenReturn(Mono.just(1)); StepVerifier.create(schemaController.delete("myNamespace", "prefix.subject-value", Optional.of("1"), false)) .consumeNextWith(response -> assertEquals(HttpStatus.NO_CONTENT, response.getStatus())) diff --git a/src/test/java/com/michelin/ns4kafka/service/SchemaServiceTest.java b/src/test/java/com/michelin/ns4kafka/service/SchemaServiceTest.java index 8dca5c65..d3c1df6b 100644 --- a/src/test/java/com/michelin/ns4kafka/service/SchemaServiceTest.java +++ b/src/test/java/com/michelin/ns4kafka/service/SchemaServiceTest.java @@ -19,7 +19,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -296,9 +295,8 @@ void shouldDeleteSchemaAllVersions() { } @Test - void shouldDeleteSchemaSpecificVersionReturnLatestSubject() { + void shouldDeleteSchemaSpecificVersion() { Namespace namespace = buildNamespace(); - Schema schema = buildSchema(); SchemaResponse schemaResponse = buildSchemaResponse("prefix.schema-one-value"); SchemaCompatibilityResponse compatibilityResponse = buildCompatibilityResponse(); @@ -308,37 +306,8 @@ void shouldDeleteSchemaSpecificVersionReturnLatestSubject() { when(schemaRegistryClient.deleteSubjectVersion(namespace.getMetadata().getCluster(), "prefix.schema-one-value", "2", true)).thenReturn(Mono.just(2)); - when(schemaRegistryClient.getSubject(namespace.getMetadata().getCluster(), - "prefix.schema-one-value", "latest")).thenReturn(Mono.just(schemaResponse)); - when(schemaRegistryClient.getCurrentCompatibilityBySubject(any(), any())).thenReturn( - Mono.just(compatibilityResponse)); - StepVerifier.create(schemaService.deleteVersion(namespace, "prefix.schema-one-value", "2")) - .consumeNextWith(optionalSchema -> { - Schema mySchema = optionalSchema.orElse(schema); - assertEquals("prefix.schema-one-value", mySchema.getMetadata().getName()); - assertEquals("local", mySchema.getMetadata().getCluster()); - assertEquals("myNamespace", mySchema.getMetadata().getNamespace()); - assertEquals(1, mySchema.getSpec().getVersion()); - }) - .verifyComplete(); - } - - @Test - void shouldDeleteSchemaSpecificVersionReturnEmpty() { - Namespace namespace = buildNamespace(); - - when(schemaRegistryClient.deleteSubjectVersion(namespace.getMetadata().getCluster(), - "prefix.schema-one", "1", false)).thenReturn(Mono.just(1)); - - when(schemaRegistryClient.deleteSubjectVersion(namespace.getMetadata().getCluster(), - "prefix.schema-one", "1", true)).thenReturn(Mono.just(1)); - - when(schemaRegistryClient.getSubject(namespace.getMetadata().getCluster(), - "prefix.schema-one", "latest")).thenReturn(Mono.empty()); - - StepVerifier.create(schemaService.deleteVersion(namespace, "prefix.schema-one", "1")) - .consumeNextWith(optionalSchema -> assertEquals(Optional.empty(), optionalSchema)) + .consumeNextWith(version -> assertEquals(2, version)) .verifyComplete(); }