Skip to content

Commit

Permalink
Fix yaml format display when getting multiple resources (#97)
Browse files Browse the repository at this point in the history
need tests

Co-authored-by: Christian Lefebvre <christian.lefebvre@worldline.com>
  • Loading branch information
piif and Christian Lefebvre authored Jan 26, 2024
1 parent 451abde commit 056ffe6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/michelin/kafkactl/GetSubcommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Integer onAuthSuccess() throws IOException {

// List resources based on parameters
if (resourceName.isEmpty() || apiResources.size() > 1) {
return resourceService.listAll(apiResources, namespace, commandSpec);
return resourceService.listAll(apiResources, namespace, output, commandSpec);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public class ResourceService {
* @param commandSpec The command that triggered the action
* @return A map of resource type and list of resources
*/
public int listAll(List<ApiResource> apiResources, String namespace, CommandLine.Model.CommandSpec commandSpec) {
public int listAll(List<ApiResource> apiResources, String namespace,
String output, CommandLine.Model.CommandSpec commandSpec) {
// Get a single kind of resources
if (apiResources.size() == 1) {
try {
List<Resource> resources = listResourcesWithType(apiResources.get(0), namespace);
if (!resources.isEmpty()) {
formatService.displayList(resources.get(0).getKind(), resources, TABLE, commandSpec);
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
} else {
commandSpec.commandLine().getOut()
.println("No " + formatService.prettifyKind(apiResources.get(0).getKind()).toLowerCase()
Expand All @@ -87,7 +88,7 @@ public int listAll(List<ApiResource> apiResources, String namespace, CommandLine
try {
List<Resource> resources = listResourcesWithType(apiResource, namespace);
if (!resources.isEmpty()) {
formatService.displayList(resources.get(0).getKind(), resources, TABLE, commandSpec);
formatService.displayList(resources.get(0).getKind(), resources, output, commandSpec);
}
return 0;
} catch (HttpClientResponseException exception) {
Expand Down
10 changes: 6 additions & 4 deletions src/test/java/com/michelin/kafkactl/GetSubcommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void shouldGetAllTopicsSuccess() {
.thenReturn(true);
when(apiResourcesService.getResourceDefinitionByName(any()))
.thenReturn(Optional.of(apiResource));
when(resourceService.listAll(any(), any(), any()))
when(resourceService.listAll(any(), any(), any(), any()))
.thenReturn(0);

CommandLine cmd = new CommandLine(getSubcommand);
Expand All @@ -191,7 +191,8 @@ void shouldGetAllTopicsSuccess() {

int code = cmd.execute("topics");
assertEquals(0, code);
verify(resourceService).listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
verify(resourceService)
.listAll(Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec());
}

@Test
Expand Down Expand Up @@ -220,13 +221,14 @@ void shouldGetAll() {

when(apiResourcesService.listResourceDefinitions())
.thenReturn(List.of(apiResource, nonNamespacedApiResource));
when(resourceService.listAll(any(), any(), any()))
when(resourceService.listAll(any(), any(), any(), any()))
.thenReturn(0);

CommandLine cmd = new CommandLine(getSubcommand);

int code = cmd.execute("all");
assertEquals(0, code);
verify(resourceService).listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
verify(resourceService)
.listAll(Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ void shouldListAllWhenOneNamespacedApiResource() {
when(namespacedClient.list(any(), any(), any()))
.thenReturn(Collections.singletonList(resource));

int actual = resourceService.listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
int actual = resourceService.listAll(
Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec()
);

assertEquals(0, actual);
verify(formatService).displayList("Topic", Collections.singletonList(resource), TABLE, cmd.getCommandSpec());
Expand Down Expand Up @@ -125,7 +127,9 @@ void shouldListAllWhenOneNonNamespacedApiResource() {
when(nonNamespacedClient.list(any(), any()))
.thenReturn(Collections.singletonList(resource));

int actual = resourceService.listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
int actual = resourceService.listAll(
Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec()
);

assertEquals(0, actual);
verify(formatService).displayList("Topic", Collections.singletonList(resource), TABLE, cmd.getCommandSpec());
Expand All @@ -150,7 +154,9 @@ void shouldListAllWhenEmptyResponse() {
.synchronizable(true)
.build();

int actual = resourceService.listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
int actual = resourceService.listAll(
Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec()
);

assertEquals(0, actual);
assertTrue(sw.toString().contains("No topic to display."));
Expand All @@ -174,7 +180,9 @@ void shouldNotListAllWhenHttpClientResponseException() {
when(namespacedClient.list(any(), any(), any()))
.thenThrow(exception);

int actual = resourceService.listAll(Collections.singletonList(apiResource), "namespace", cmd.getCommandSpec());
int actual = resourceService.listAll(
Collections.singletonList(apiResource), "namespace", TABLE, cmd.getCommandSpec()
);

assertEquals(1, actual);
verify(formatService).displayError(exception, cmd.getCommandSpec());
Expand Down Expand Up @@ -227,7 +235,7 @@ void shouldListAllWhenMultipleApiResources() {
.thenReturn(Collections.singletonList(connectorResource));

int actual =
resourceService.listAll(List.of(apiResourceOne, apiResourceTwo), "namespace", cmd.getCommandSpec());
resourceService.listAll(List.of(apiResourceOne, apiResourceTwo), "namespace", TABLE, cmd.getCommandSpec());

assertEquals(0, actual);
verify(formatService).displayList("Topic", Collections.singletonList(topicResource), TABLE,
Expand Down Expand Up @@ -274,7 +282,7 @@ void shouldListAllWhenMultipleApiResourcesAndException() {
.thenThrow(exception);

int actual =
resourceService.listAll(List.of(apiResourceOne, apiResourceTwo), "namespace", cmd.getCommandSpec());
resourceService.listAll(List.of(apiResourceOne, apiResourceTwo), "namespace", TABLE, cmd.getCommandSpec());

assertEquals(1, actual);
verify(formatService).displayList("Topic", Collections.singletonList(topicResource), TABLE,
Expand Down

0 comments on commit 056ffe6

Please sign in to comment.