Target REST API returns OK with empty list when no tags (#1786)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com
This commit is contained in:
Avgustin Marinov
2024-07-26 08:43:20 +03:00
committed by GitHub
parent 859cd130f2
commit 950ed398cc
3 changed files with 12 additions and 20 deletions

View File

@@ -724,7 +724,6 @@ public interface MgmtTargetRestApi {
@Operation(summary = "Return tags for specific target", description = "Get a paged list of tags for a target. Required permission: READ_REPOSITORY")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Successfully retrieved"),
@ApiResponse(responseCode = "204", description = "No tags"),
@ApiResponse(responseCode = "401", description = "The request requires user authentication.",
content = @Content(mediaType = "application/json", schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403",

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.mgmt.rest.resource;
import java.util.AbstractMap.SimpleEntry;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@@ -397,11 +398,8 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
@Override
public ResponseEntity<List<MgmtTag>> getTags(@PathVariable("targetId") String targetId) {
final Set<TargetTag> tags = targetManagement.getTagsByControllerId(targetId);
if (tags.isEmpty()) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.ok(MgmtTagMapper.toResponse(tags.stream().toList()));
}
return ResponseEntity.ok(
MgmtTagMapper.toResponse(tags == null ? Collections.emptyList() : tags.stream().toList()));
}
@Override

View File

@@ -1898,22 +1898,17 @@ class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest {
}
@Test
void getControllerTagReturnsTagWithNoContent() throws Exception {
// create target with attributes
final String knownTargetId = "targetIdWithNoTags";
final Target target = testdataFactory.createTarget(knownTargetId);
// test query target over rest resource
mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownTargetId + "/tags"))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isNoContent());
}
@Test
void getControllerTagReturnsTagWithOk() throws Exception {
void getControllerTagReturnsTagsWithOk() throws Exception {
// create target with attributes
final String knownTargetId = "targetIdWithTags";
final Target target = testdataFactory.createTarget(knownTargetId);
// test query target over rest resource with no tags - expect OK with empty list
mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownTargetId + "/tags"))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.hasSize(0)));
final List<TargetTag> targetTags = testdataFactory.createTargetTags(2, "tag_getControllerTagReturnsTagWithOk");
final List<String> tagNames = new ArrayList<>();
for (final TargetTag targetTag : targetTags) {
@@ -1921,7 +1916,7 @@ class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest {
tagNames.add(targetTag.getName());
}
// test query target over rest resource
// test query target over rest resource with 2 tags - expect OK with 2 tags the target is tagged with
mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownTargetId + "/tags"))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())