Target type unnasign when updating target (#1385)

* Add main logic

Signed-off-by: Stanislav Trailov <Stanislav.Trailov@bosch.io>

* Add unit test and fix docu tests

Signed-off-by: Stanislav Trailov <Stanislav.Trailov@bosch.io>

* Remove unused imports

Signed-off-by: Stanislav Trailov <Stanislav.Trailov@bosch.io>

---------

Signed-off-by: Stanislav Trailov <Stanislav.Trailov@bosch.io>
This commit is contained in:
Stanislav Trailov
2023-07-10 15:10:19 +03:00
committed by GitHub
parent a8d5a15a1c
commit 5edd9fdf76
3 changed files with 93 additions and 6 deletions

View File

@@ -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);

View File

@@ -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 {

View File

@@ -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<String, Object> 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);
}