Add support for target tag retrieval via REST (#1782)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-07-24 14:01:40 +03:00
committed by GitHub
parent c253a4fccd
commit 3189531162
8 changed files with 102 additions and 12 deletions

View File

@@ -17,6 +17,8 @@ import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtTag;
import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtTagRequestBodyPut;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtDistributionSetTagRestApi;
@@ -30,14 +32,10 @@ import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.rest.data.ResponseList;
/**
* A mapper which maps repository model to RESTful model representation and
* back.
*
* A mapper which maps repository model to RESTful model representation and back.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
final class MgmtTagMapper {
private MgmtTagMapper() {
// Utility class
}
static List<MgmtTag> toResponse(final List<TargetTag> targetTags) {
final List<MgmtTag> tagsRest = new ArrayList<>();

View File

@@ -14,6 +14,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -30,6 +31,7 @@ import org.eclipse.hawkbit.mgmt.json.model.action.MgmtActionStatus;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtActionType;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtDistributionSet;
import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtTargetAssignmentResponseBody;
import org.eclipse.hawkbit.mgmt.json.model.tag.MgmtTag;
import org.eclipse.hawkbit.mgmt.json.model.target.MgmtDistributionSetAssignments;
import org.eclipse.hawkbit.mgmt.json.model.target.MgmtTarget;
import org.eclipse.hawkbit.mgmt.json.model.target.MgmtTargetAttributes;
@@ -51,6 +53,7 @@ import org.eclipse.hawkbit.repository.model.DeploymentRequest;
import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetMetadata;
import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.eclipse.hawkbit.utils.TenantConfigHelper;
import org.springframework.data.domain.Page;
@@ -391,6 +394,16 @@ public class MgmtTargetResource implements MgmtTargetRestApi {
return ResponseEntity.ok(MgmtTargetMapper.toResponseWithLinks(targetId, action));
}
@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()));
}
}
@Override
public ResponseEntity<PagedList<MgmtMetadata>> getMetadata(@PathVariable("targetId") final String targetId,
@RequestParam(value = MgmtRestConstants.REQUEST_PARAMETER_PAGING_OFFSET, defaultValue = MgmtRestConstants.REQUEST_PARAMETER_PAGING_DEFAULT_OFFSET) final int pagingOffsetParam,

View File

@@ -18,6 +18,7 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.in;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
@@ -70,6 +71,7 @@ import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetMetadata;
import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.repository.model.TargetType;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.repository.test.util.WithUser;
@@ -1895,6 +1897,39 @@ class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest {
return targetManagement.getByControllerID(tA.getControllerId()).get();
}
@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 {
// create target with attributes
final String knownTargetId = "targetIdWithTags";
final Target target = testdataFactory.createTarget(knownTargetId);
final List<TargetTag> targetTags = testdataFactory.createTargetTags(2, "tag_getControllerTagReturnsTagWithOk");
final List<String> tagNames = new ArrayList<>();
for (final TargetTag targetTag : targetTags) {
targetManagement.toggleTagAssignment(Collections.singletonList(target.getControllerId()), targetTag.getName());
tagNames.add(targetTag.getName());
}
// test query target over rest resource
mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownTargetId + "/tags"))
.andDo(MockMvcResultPrinter.print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", Matchers.hasSize(2)))
.andExpect(jsonPath("$.[0].name", in(tagNames)))
.andExpect(jsonPath("$.[1].name", in(tagNames)));
}
@Test
@Description("Ensures that the metadata creation through API is reflected by the repository.")
void createMetadata() throws Exception {