diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java index 2363661a5..9a07854df 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/main/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResource.java @@ -154,10 +154,23 @@ public class MgmtTargetResource implements MgmtTargetRestApi { } } - final Target updateTarget = this.targetManagement.update(entityFactory.target().update(targetId) + Target updateTarget; + + if (targetRest.getTargetType() != null && targetRest.getTargetType() == -1L) { + // if targetType in request is -1 - unassign targetType from target + this.targetManagement.unAssignType(targetId); + // update target without targetType here ... + updateTarget = this.targetManagement.update(entityFactory.target().update(targetId) .name(targetRest.getName()).description(targetRest.getDescription()).address(targetRest.getAddress()) - .targetType(targetRest.getTargetType()).securityToken(targetRest.getSecurityToken()) - .requestAttributes(targetRest.isRequestAttributes())); + .securityToken(targetRest.getSecurityToken()).requestAttributes(targetRest.isRequestAttributes())); + + } else { + updateTarget = this.targetManagement.update( + entityFactory.target().update(targetId).name(targetRest.getName()).description(targetRest.getDescription()) + .address(targetRest.getAddress()).targetType(targetRest.getTargetType()).securityToken(targetRest.getSecurityToken()) + .requestAttributes(targetRest.isRequestAttributes())); + + } final MgmtTarget response = MgmtTargetMapper.toResponse(updateTarget, tenantConfigHelper); MgmtTargetMapper.addPollStatus(updateTarget, response); diff --git a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResourceTest.java b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResourceTest.java index dad2a2861..06ab60874 100644 --- a/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResourceTest.java +++ b/hawkbit-rest/hawkbit-mgmt-resource/src/test/java/org/eclipse/hawkbit/mgmt/rest/resource/MgmtTargetResourceTest.java @@ -454,6 +454,75 @@ class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest { assertThat(findTargetByControllerID.getName()).isEqualTo(knownNameNotModify); } + @Test + @Description("Ensures that when targetType value of -1 is provided the target type is unassigned from the target.") + public void updateTargetAndUnnasignTargetType() throws Exception { + final String knownControllerId = "123"; + final String knownNewAddress = "amqp://test123/foobar"; + final String knownNameNotModify = "controllerName"; + final Long unnasignTargetTypeValue = -1L; + + final TargetType targetType = targetTypeManagement.create( + entityFactory.targetType().create().name("targettype1").description("targettypedes1")); + + final String body = new JSONObject().put("targetType", unnasignTargetTypeValue).toString(); + + // create a target with the created TargetType + targetManagement.create(entityFactory.target().create().controllerId(knownControllerId).name(knownNameNotModify) + .address(knownNewAddress).targetType(targetType.getId())); + + mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId) + .contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.controllerId", equalTo(knownControllerId))) + .andExpect(jsonPath("$.address", equalTo(knownNewAddress))) + .andExpect(jsonPath("$.name", equalTo(knownNameNotModify))) + .andExpect(jsonPath("$.targetType").exists()); + + mvc.perform(put(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId).content(body) + .contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.controllerId", equalTo(knownControllerId))) + .andExpect(jsonPath("$.address", equalTo(knownNewAddress))) + .andExpect(jsonPath("$.name", equalTo(knownNameNotModify))) + .andExpect(jsonPath("$.targetType").doesNotExist()); + + } + + @Test + @Description("Ensures that when targetType value of -1 is provided the target type is unassigned from the target when updating multiple fields in target object.") + public void updateTargetNameAndUnnasignTargetType() throws Exception { + final String knownControllerId = "123"; + final String knownNewAddress = "amqp://test123/foobar"; + final String knownNameNotModify = "controllerName"; + final Long unnasignTargetTypeValue = -1L; + final String controllerNewName = "controllerNewName"; + + final TargetType targetType = targetTypeManagement.create( + entityFactory.targetType().create().name("targettype1").description("targettypedes1")); + + final String body = new JSONObject() + .put("targetType", unnasignTargetTypeValue).put("name", "controllerNewName") + .toString(); + + // create a target with the created TargetType + targetManagement.create(entityFactory.target().create().controllerId(knownControllerId).name(knownNameNotModify) + .address(knownNewAddress).targetType(targetType.getId())); + + mvc.perform(get(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId) + .contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.controllerId", equalTo(knownControllerId))) + .andExpect(jsonPath("$.address", equalTo(knownNewAddress))) + .andExpect(jsonPath("$.name", equalTo(knownNameNotModify))) + .andExpect(jsonPath("$.targetType").exists()); + + //check if controller name is updated AND target type is missing (not assigned) + mvc.perform(put(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/" + knownControllerId).content(body) + .contentType(MediaType.APPLICATION_JSON)).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.controllerId", equalTo(knownControllerId))) + .andExpect(jsonPath("$.address", equalTo(knownNewAddress))) + .andExpect(jsonPath("$.name", equalTo(controllerNewName))) + .andExpect(jsonPath("$.targetType").doesNotExist()); + } + @Test @Description("Ensures that target query returns list of targets in defined format.") void getTargetWithoutAdditionalRequestParameters() throws Exception { diff --git a/hawkbit-rest/hawkbit-rest-docs/src/test/java/org/eclipse/hawkbit/rest/mgmt/documentation/TargetResourceDocumentationTest.java b/hawkbit-rest/hawkbit-rest-docs/src/test/java/org/eclipse/hawkbit/rest/mgmt/documentation/TargetResourceDocumentationTest.java index b006d81ec..14b71ab84 100644 --- a/hawkbit-rest/hawkbit-rest-docs/src/test/java/org/eclipse/hawkbit/rest/mgmt/documentation/TargetResourceDocumentationTest.java +++ b/hawkbit-rest/hawkbit-rest-docs/src/test/java/org/eclipse/hawkbit/rest/mgmt/documentation/TargetResourceDocumentationTest.java @@ -204,7 +204,9 @@ public class TargetResourceDocumentationTest extends AbstractApiRestDocumentatio enableConfirmationFlow(); final Target target = createTargetByGivenNameWithAttributes(targetId, createDistributionSet()); - final String targetAsJson = createJsonTarget(targetId, "newTargetName", "I've been updated"); + final long targetTypeId = target.getTargetType().getId(); + + final String targetAsJson = createJsonTarget(targetId, "newTargetName", "I've been updated", targetTypeId); mockMvc.perform(put(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING + "/{targetId}", target.getControllerId()) .contentType(MediaType.APPLICATION_JSON).content(targetAsJson)).andExpect(status().isOk()) @@ -220,7 +222,9 @@ public class TargetResourceDocumentationTest extends AbstractApiRestDocumentatio optionalRequestFieldWithPath("securityToken") .description(MgmtApiModelProperties.SECURITY_TOKEN), optionalRequestFieldWithPath("requestAttributes") - .description(MgmtApiModelProperties.REQUEST_ATTRIBUTES)), + .description(MgmtApiModelProperties.REQUEST_ATTRIBUTES), + optionalRequestFieldWithPath("targetType").description(MgmtApiModelProperties.TARGETTYPE_ID + + ". If value of -1 provided the target type will be unassigned.")), getResponseFieldTarget(false))); } @@ -983,7 +987,7 @@ public class TargetResourceDocumentationTest extends AbstractApiRestDocumentatio return "[" + this.objectMapper.writeValueAsString(target) + "]"; } - private String createJsonTarget(final String controllerId, final String name, final String description) + private String createJsonTarget(final String controllerId, final String name, final String description, final long targetTypeId) throws JsonProcessingException { final Map target = new HashMap<>(); target.put("controllerId", controllerId); @@ -992,6 +996,7 @@ public class TargetResourceDocumentationTest extends AbstractApiRestDocumentatio target.put("address", "https://192.168.0.1"); target.put("securityToken", "2345678DGGDGFTDzztgf"); target.put("requestAttributes", true); + target.put("targetType", targetTypeId); return this.objectMapper.writeValueAsString(target); }