Skip to content

Commit

Permalink
Improve error message namespace mismatch (#105)
Browse files Browse the repository at this point in the history
* Improve error message namespace mismatch

* Fix unit test
  • Loading branch information
loicgreffier authored Feb 24, 2024
1 parent 1856be3 commit 084dd74
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.michelin.kafkactl.services.FormatService.TABLE;
import static com.michelin.kafkactl.utils.constants.ConstantKind.RESOURCE_DEFINITION;

import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.parents.AuthenticatedCommand;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -44,7 +44,7 @@ public Integer onAuthSuccess() {
List<Resource> resources = apiResourcesService.listResourceDefinitions()
.stream()
.map(apiResource -> Resource.builder()
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name(apiResource.getKind())
.build())
.spec(Map.of("names", String.join(",", apiResource.getNames()),
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/michelin/kafkactl/ConfigSubcommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static com.michelin.kafkactl.utils.constants.ConstantKind.CONTEXT;

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.services.ConfigService;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Integer call() throws IOException {

String currentContextName = configService.getCurrentContextName();
Resource currentContextAsResource = Resource.builder()
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name(currentContextName != null ? currentContextName : StringUtils.EMPTY_STRING)
.build())
.spec(specs)
Expand All @@ -102,7 +102,7 @@ public Integer call() throws IOException {
specs.put("token", userContext.getDefinition().getUserToken());

Resource currentContextAsResource = Resource.builder()
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name(userContext.getName())
.build())
.spec(specs)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/michelin/kafkactl/ConnectorsSubcommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import static com.michelin.kafkactl.utils.constants.ConstantKind.CONNECTOR;

import com.michelin.kafkactl.models.ApiResource;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.parents.AuthenticatedCommand;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -72,7 +72,7 @@ public Integer onAuthSuccess() {
List<Resource> changeConnectorResponses = connectors.stream()
// Prepare request object
.map(connector -> Resource.builder()
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.namespace(namespace)
.name(connector)
.build())
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/michelin/kafkactl/DeleteSubcommand.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.michelin.kafkactl;

import com.michelin.kafkactl.models.ApiResource;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.parents.DryRunCommand;
import com.michelin.kafkactl.services.FileService;
Expand Down Expand Up @@ -105,7 +105,7 @@ private List<Resource> parseResources(String namespace) {
}
// Generate a single resource with minimum details from input
return List.of(Resource.builder()
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name(config.nameConfig.name)
.namespace(namespace)
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.michelin.kafkactl;

import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.parents.DryRunCommand;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -84,7 +84,7 @@ public Integer onAuthSuccess() throws IOException {
Resource consumerGroupResetOffset = Resource.builder()
.apiVersion("v1")
.kind("ConsumerGroupResetOffsets")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.namespace(namespace)
.name(group)
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@ReflectiveAccess
@NoArgsConstructor
@AllArgsConstructor
public class ObjectMeta {
public class Metadata {
private String name;
private String namespace;
private String cluster;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/michelin/kafkactl/models/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class Resource {
private String apiVersion;
private String kind;
private ObjectMeta metadata;
private Metadata metadata;
@JsonInclude(value = JsonInclude.Include.NON_ABSENT)
private Map<String, Object> spec;
private Object status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ protected void validateNamespace(List<Resource> resources) {
if (!namespaceMismatch.isEmpty()) {
String invalid = namespaceMismatch
.stream()
.map(resource -> resource.getKind() + "/" + resource.getMetadata().getName())
.map(resource -> "\"" + resource.getKind() + "/" + resource.getMetadata().getName() + "\"")
.distinct()
.collect(Collectors.joining(", "));
throw new CommandLine.ParameterException(commandSpec.commandLine(),
"Namespace mismatch between Kafkactl and YAML document " + invalid + ".");
"Namespace mismatch between Kafkactl configuration and YAML resource(s): " + invalid + ".");
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/test/java/com/michelin/kafkactl/ApplySubcommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.models.ApiResource;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.services.ApiResourcesService;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -107,7 +107,7 @@ void shouldNotApplyWhenInvalidResources() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.build())
.spec(Collections.emptyMap())
Expand Down Expand Up @@ -135,7 +135,7 @@ void shouldNotApplyWhenNamespaceMismatch() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand All @@ -155,15 +155,16 @@ void shouldNotApplyWhenNamespaceMismatch() {

int code = cmd.execute("-f", "topic.yml");
assertEquals(2, code);
assertTrue(sw.toString().contains("Namespace mismatch between Kafkactl and YAML document Topic/prefix.topic."));
assertTrue(sw.toString().contains("Namespace mismatch between Kafkactl configuration and YAML resource(s): "
+ "\"Topic/prefix.topic\"."));
}

@Test
void shouldNotApplyWhenHttpClientResponseException() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -195,7 +196,7 @@ void shouldApply() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -240,7 +241,7 @@ void shouldApplyDryRun() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.build())
.spec(Collections.emptyMap())
Expand Down Expand Up @@ -288,7 +289,7 @@ void shouldApplySchema() {
Resource resource = Resource.builder()
.kind("Schema")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.schema")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -338,7 +339,7 @@ void shouldApplyInlineSchema() {
Resource resource = Resource.builder()
.kind("Schema")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.schema")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -385,7 +386,7 @@ void shouldNotApplySchemaWhenNotExist() {
Resource resource = Resource.builder()
.kind("Schema")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.schema")
.namespace("namespace")
.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.models.ApiResource;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.services.ApiResourcesService;
import com.michelin.kafkactl.services.FormatService;
Expand Down Expand Up @@ -94,7 +94,7 @@ void shouldChangeState() {
Resource resource = Resource.builder()
.kind("Connector")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.connector")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -122,7 +122,7 @@ void shouldChangeStateOfAll() {
Resource resource = Resource.builder()
.kind("Connector")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.connector")
.namespace("namespace")
.build())
Expand Down
17 changes: 9 additions & 8 deletions src/test/java/com/michelin/kafkactl/DeleteSubcommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import com.michelin.kafkactl.config.KafkactlConfig;
import com.michelin.kafkactl.models.ApiResource;
import com.michelin.kafkactl.models.ObjectMeta;
import com.michelin.kafkactl.models.Metadata;
import com.michelin.kafkactl.models.Resource;
import com.michelin.kafkactl.services.ApiResourcesService;
import com.michelin.kafkactl.services.FileService;
Expand Down Expand Up @@ -101,7 +101,7 @@ void shouldNotDeleteByFileWhenInvalidResources() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -147,7 +147,7 @@ void shouldNotDeleteByFileWhenNamespaceMismatch() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand All @@ -163,7 +163,8 @@ void shouldNotDeleteByFileWhenNamespaceMismatch() {

int code = cmd.execute("-f", "topic");
assertEquals(2, code);
assertTrue(sw.toString().contains("Namespace mismatch between Kafkactl and YAML document Topic/prefix.topic."));
assertTrue(sw.toString().contains("Namespace mismatch between Kafkactl configuration and YAML resource(s): "
+ "\"Topic/prefix.topic\"."));
}

@Test
Expand All @@ -177,7 +178,7 @@ void shouldDeleteByFileSuccess() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -247,7 +248,7 @@ void shouldDeleteByFileDryRunSuccess() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.build())
.spec(Collections.emptyMap())
Expand Down Expand Up @@ -289,7 +290,7 @@ void shouldDeleteByFileFail() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down Expand Up @@ -329,7 +330,7 @@ void shouldNotDeleteByFileWhenHttpClientResponseException() {
Resource resource = Resource.builder()
.kind("Topic")
.apiVersion("v1")
.metadata(ObjectMeta.builder()
.metadata(Metadata.builder()
.name("prefix.topic")
.namespace("namespace")
.build())
Expand Down
Loading

0 comments on commit 084dd74

Please sign in to comment.