Fix empty controller name (#925)
* Truncate target name when name is empty and controllerId exceeds name length limit * Add check if controllerId is null * Use seperate method to truncate controllerId + use Array.asList to create list * Make truncateControllerIdToMaxNameLength a static method * Additionally check target name by calling target management Signed-off-by: Sebastian Firsching <sebastian.firsching@bosch-si.com>
This commit is contained in:
committed by
Jeroen Laverman
parent
835757e93a
commit
62c876a435
@@ -192,10 +192,15 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Target, EventAw
|
||||
*/
|
||||
public JpaTarget(final String controllerId, final String securityToken) {
|
||||
this.controllerId = controllerId;
|
||||
setName(controllerId);
|
||||
setName(truncateControllerIdToMaxNameLength(controllerId));
|
||||
this.securityToken = securityToken;
|
||||
}
|
||||
|
||||
private static String truncateControllerIdToMaxNameLength(final String controllerId) {
|
||||
return controllerId != null && controllerId.length() > NAME_MAX_SIZE ? controllerId.substring(0, NAME_MAX_SIZE)
|
||||
: controllerId;
|
||||
}
|
||||
|
||||
JpaTarget() {
|
||||
// empty constructor for JPA.
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ import org.eclipse.hawkbit.mgmt.json.model.distributionset.MgmtActionType;
|
||||
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
|
||||
import org.eclipse.hawkbit.repository.ActionFields;
|
||||
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
|
||||
import org.eclipse.hawkbit.repository.model.Action;
|
||||
import org.eclipse.hawkbit.repository.model.Action.ActionType;
|
||||
import org.eclipse.hawkbit.repository.model.Action.Status;
|
||||
@@ -667,6 +668,26 @@ public class MgmtTargetResourceTest extends AbstractManagementApiIntegrationTest
|
||||
.andExpect(status().isNoContent()).andExpect(content().string(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Ensures that a target creation with empty name and a controllerId that exceeds the name length limitation is successful and the name gets truncated.")
|
||||
public void createTargetWithEmptyNameAndLongControllerId() throws Exception {
|
||||
final String randomString = RandomStringUtils.randomAlphanumeric(JpaTarget.CONTROLLER_ID_MAX_SIZE);
|
||||
|
||||
final Target target = entityFactory.target().create().controllerId(randomString).build();
|
||||
|
||||
final String targetList = JsonBuilder.targets(Arrays.asList(target), false);
|
||||
|
||||
final String expectedTargetName = randomString.substring(0,
|
||||
Math.min(JpaTarget.CONTROLLER_ID_MAX_SIZE, NamedEntity.NAME_MAX_SIZE));
|
||||
|
||||
mvc.perform(post(MgmtRestConstants.TARGET_V1_REQUEST_MAPPING).content(targetList)
|
||||
.contentType(MediaType.APPLICATION_JSON)).andExpect(status().isCreated())
|
||||
.andExpect(jsonPath("[0].controllerId", equalTo(randomString)))
|
||||
.andExpect(jsonPath("[0].name", equalTo(expectedTargetName)));
|
||||
|
||||
assertThat(targetManagement.getByControllerID(randomString).get().getName()).isEqualTo(expectedTargetName);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Ensures that post request for creating a target with no payload returns a bad request.")
|
||||
public void createTargetWithoutPayloadBadRequest() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user