Improvements repository validation constraints (#626)

* Add html tag Validator on strings. Add string trim.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Revert unintended changes. Sonar issues.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Remove weired comment.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Raise EclipseLink due to validation problem.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fix permission.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Colour field test.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>

* Fix sonar issues.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2018-02-12 14:56:36 +01:00
committed by GitHub
parent 1a6ab123e3
commit d3cb9bb24d
89 changed files with 812 additions and 495 deletions

View File

@@ -40,6 +40,7 @@ import com.google.common.collect.Lists;
@TestPropertySource(locations = "classpath:/jpa-test.properties")
public abstract class AbstractJpaIntegrationTest extends AbstractIntegrationTest {
protected static final String INVALID_TEXT_HTML = "</noscript><br><script>";
protected static final String NOT_EXIST_ID = "1234";
protected static final long NOT_EXIST_IDL = Long.parseLong(NOT_EXIST_ID);

View File

@@ -67,8 +67,8 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
@ExpectEvents({ @Expect(type = SoftwareModuleDeletedEvent.class, count = 0) })
public void entityQueriesReferringToNotExistingEntitiesThrowsException() throws URISyntaxException {
verifyThrownExceptionBy(() -> artifactManagement.create(IOUtils.toInputStream("test", "UTF-8"),
NOT_EXIST_IDL, "xxx", null, null, false, null), "SoftwareModule");
verifyThrownExceptionBy(() -> artifactManagement.create(IOUtils.toInputStream("test", "UTF-8"), NOT_EXIST_IDL,
"xxx", null, null, false, null), "SoftwareModule");
verifyThrownExceptionBy(
() -> artifactManagement.create(IOUtils.toInputStream("test", "UTF-8"), 1234L, "xxx", false),
@@ -76,8 +76,7 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
verifyThrownExceptionBy(() -> artifactManagement.delete(NOT_EXIST_IDL), "Artifact");
verifyThrownExceptionBy(() -> artifactManagement.findBySoftwareModule(PAGE, NOT_EXIST_IDL),
"SoftwareModule");
verifyThrownExceptionBy(() -> artifactManagement.findBySoftwareModule(PAGE, NOT_EXIST_IDL), "SoftwareModule");
assertThat(artifactManagement.getByFilename(NOT_EXIST_ID).isPresent()).isFalse();
verifyThrownExceptionBy(() -> artifactManagement.getByFilenameAndSoftwareModule("xxx", NOT_EXIST_IDL),
@@ -102,12 +101,11 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
final byte random[] = RandomStringUtils.random(5 * 1024).getBytes();
final Artifact result = artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file1",
false);
final Artifact result = artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file1", false);
artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file11", false);
artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file12", false);
final Artifact result2 = artifactManagement.create(new ByteArrayInputStream(random), sm2.getId(),
"file2", false);
final Artifact result2 = artifactManagement.create(new ByteArrayInputStream(random), sm2.getId(), "file2",
false);
assertThat(result).isInstanceOf(Artifact.class);
assertThat(result.getSoftwareModule().getId()).isEqualTo(sm.getId());
@@ -162,10 +160,10 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
assertThat(artifactRepository.findAll()).isEmpty();
final Artifact result = artifactManagement.create(new RandomGeneratedInputStream(5 * 1024), sm.getId(),
"file1", false);
final Artifact result2 = artifactManagement.create(new RandomGeneratedInputStream(5 * 1024),
sm2.getId(), "file2", false);
final Artifact result = artifactManagement.create(new RandomGeneratedInputStream(5 * 1024), sm.getId(), "file1",
false);
final Artifact result2 = artifactManagement.create(new RandomGeneratedInputStream(5 * 1024), sm2.getId(),
"file2", false);
assertThat(artifactRepository.findAll()).hasSize(2);
@@ -204,10 +202,9 @@ public class ArtifactManagementTest extends AbstractJpaIntegrationTest {
final byte random[] = RandomStringUtils.random(5 * 1024).getBytes();
final Artifact result = artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file1",
final Artifact result = artifactManagement.create(new ByteArrayInputStream(random), sm.getId(), "file1", false);
final Artifact result2 = artifactManagement.create(new ByteArrayInputStream(random), sm2.getId(), "file2",
false);
final Artifact result2 = artifactManagement.create(new ByteArrayInputStream(random), sm2.getId(),
"file2", false);
assertThat(artifactRepository.findAll()).hasSize(2);
assertThat(result.getId()).isNotNull();

View File

@@ -15,6 +15,7 @@ import static org.junit.Assert.fail;
import java.io.ByteArrayInputStream;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@@ -146,6 +147,33 @@ public class ControllerManagementTest extends AbstractJpaIntegrationTest {
assertThat(controllerManagement.findActionStatusByAction(PAGE, actionId).getNumberOfElements()).isEqualTo(6);
}
@Test
@Description("Controller confirmation failes with invalid messages.")
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = ActionCreatedEvent.class, count = 1), @Expect(type = TargetUpdatedEvent.class, count = 1),
@Expect(type = TargetAssignDistributionSetEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3) })
public void controllerConfirmationFailsWithInvalidMessages() {
final Long actionId = createTargetAndAssignDs();
simulateIntermediateStatusOnUpdate(actionId);
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.addUpdateActionStatus(entityFactory.actionStatus()
.create(actionId).status(Action.Status.FINISHED).message(INVALID_TEXT_HTML)))
.as("set invalid description text should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> controllerManagement.addUpdateActionStatus(
entityFactory.actionStatus().create(actionId).status(Action.Status.FINISHED)
.messages(Arrays.asList("this is valid.", INVALID_TEXT_HTML))))
.as("set invalid description text should not be created");
assertThat(actionStatusRepository.count()).isEqualTo(5);
assertThat(controllerManagement.findActionStatusByAction(PAGE, actionId).getNumberOfElements()).isEqualTo(5);
}
@Test
@Description("Controller confirms successfull update with FINISHED status on a action that is on canceling. "
+ "Reason: The decission to ignore the cancellation is in fact up to the controller.")

View File

@@ -66,6 +66,7 @@ import ru.yandex.qatools.allure.annotations.Stories;
@Features("Component Tests - Repository")
@Stories("DistributionSet Management")
public class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
@Test
@Description("Verifies that management get access react as specfied on calls for non existing entities by means "
+ "of Optional not present.")
@@ -77,7 +78,6 @@ public class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
assertThat(distributionSetManagement.getWithDetails(NOT_EXIST_IDL)).isNotPresent();
assertThat(distributionSetManagement.getByNameAndVersion(NOT_EXIST_ID, NOT_EXIST_ID)).isNotPresent();
assertThat(distributionSetManagement.getMetaDataByDistributionSetId(set.getId(), NOT_EXIST_ID)).isNotPresent();
}
@Test
@@ -192,11 +192,21 @@ public class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
.version("a").description(RandomStringUtils.randomAlphanumeric(513))))
.as("entity with too long description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.create(
entityFactory.distributionSet().create().name("a").version("a").description(INVALID_TEXT_HTML)))
.as("entity with invalid description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.description(RandomStringUtils.randomAlphanumeric(513))))
.as("entity with too long description should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).description(INVALID_TEXT_HTML)))
.as("entity with invalid characters should not be updated");
}
@Step
@@ -209,13 +219,23 @@ public class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.create(entityFactory.distributionSet().create().version("a").name("")))
.as("entity with too long name should not be created");
.as("entity with too short name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().version("a").name(INVALID_TEXT_HTML)))
.as("entity with invalid characters in name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.name(RandomStringUtils.randomAlphanumeric(65))))
.as("entity with too long name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).name(INVALID_TEXT_HTML)))
.as("entity with invalid characters should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()).name("")))
.as("entity with too short name should not be updated");

View File

@@ -95,28 +95,42 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
private void createAndUpdateDistributionSetWithInvalidDescription(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet()
.create().name("a").version("a").description(RandomStringUtils.randomAlphanumeric(513))))
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a")
.version("a").description(RandomStringUtils.randomAlphanumeric(513))))
.as("set with too long description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet()
.update(set.getId()).description(RandomStringUtils.randomAlphanumeric(513))))
.isThrownBy(() -> distributionSetManagement.create(
entityFactory.distributionSet().create().name("a").version("a").description(INVALID_TEXT_HTML)))
.as("set invalid description text should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.description(RandomStringUtils.randomAlphanumeric(513))))
.as("set with too long description should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).description(INVALID_TEXT_HTML)))
.as("set with invalid description should not be updated");
}
@Step
private void createAndUpdateDistributionSetWithInvalidName(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet()
.create().version("a").name(RandomStringUtils.randomAlphanumeric(65))))
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().version("a")
.name(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().version("a").name("")))
.create(entityFactory.distributionSet().create().version("a").name(INVALID_TEXT_HTML)))
.as("set with invalid name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.create(entityFactory.distributionSet().create().version("a").name("")))
.as("set with too short name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
@@ -125,13 +139,17 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
.as("set with null name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet()
.update(set.getId()).name(RandomStringUtils.randomAlphanumeric(65))))
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.name(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).name("")))
.update(entityFactory.distributionSet().update(set.getId()).name(INVALID_TEXT_HTML)))
.as("set with invalid name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()).name("")))
.as("set with too short name should not be updated");
}
@@ -139,29 +157,37 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
private void createAndUpdateDistributionSetWithInvalidVersion(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet()
.create().name("a").version(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long name should not be created");
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a")
.version(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long version should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("a").version("")))
.as("set with too short name should not be created");
.create(entityFactory.distributionSet().create().name("a").version(INVALID_TEXT_HTML)))
.as("set with invalid version should not be created");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a").version("")))
.as("set with too short version should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("a").version(null)))
.as("set with null name should not be created");
.as("set with null version should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet()
.update(set.getId()).version(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long name should not be updated");
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.version(RandomStringUtils.randomAlphanumeric(65))))
.as("set with too long version should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).version("")))
.as("set with too short name should not be updated");
.update(entityFactory.distributionSet().update(set.getId()).version(INVALID_TEXT_HTML)))
.as("set with invalid version should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId()).version("")))
.as("set with too short version should not be updated");
}
@Test
@@ -195,8 +221,8 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
final DistributionSetType nonUpdatableType = distributionSetTypeManagement.create(entityFactory
.distributionSetType().create().key("updatableType").name("to be deleted").colour("test123"));
assertThat(distributionSetTypeManagement.getByKey("updatableType").get().getMandatoryModuleTypes()).isEmpty();
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft")
.version("1").type(nonUpdatableType.getKey()));
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")
.type(nonUpdatableType.getKey()));
distributionSetTypeManagement.update(
entityFactory.distributionSetType().update(nonUpdatableType.getId()).description("a new description"));
@@ -212,8 +238,8 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
final DistributionSetType nonUpdatableType = distributionSetTypeManagement
.create(entityFactory.distributionSetType().create().key("updatableType").name("to be deleted"));
assertThat(distributionSetTypeManagement.getByKey("updatableType").get().getMandatoryModuleTypes()).isEmpty();
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft")
.version("1").type(nonUpdatableType.getKey()));
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")
.type(nonUpdatableType.getKey()));
assertThatThrownBy(() -> distributionSetTypeManagement
.assignMandatorySoftwareModuleTypes(nonUpdatableType.getId(), Sets.newHashSet(osType.getId())))
@@ -229,8 +255,8 @@ public class DistributionSetTypeManagementTest extends AbstractJpaIntegrationTes
nonUpdatableType = distributionSetTypeManagement.assignMandatorySoftwareModuleTypes(nonUpdatableType.getId(),
Sets.newHashSet(osType.getId()));
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft")
.version("1").type(nonUpdatableType.getKey()));
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")
.type(nonUpdatableType.getKey()));
final Long typeId = nonUpdatableType.getId();
assertThatThrownBy(() -> distributionSetTypeManagement.unassignSoftwareModuleType(typeId, osType.getId()))

View File

@@ -38,6 +38,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.InvalidTargetAddressException;
import org.eclipse.hawkbit.repository.exception.TenantNotExistException;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.model.Action.Status;
@@ -208,11 +209,21 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.description(RandomStringUtils.randomAlphanumeric(513))))
.as("target with too long description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.create(entityFactory.target().create().controllerId("a").description(INVALID_TEXT_HTML)))
.as("target with invalid description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.description(RandomStringUtils.randomAlphanumeric(513))))
.as("target with too long description should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.update(entityFactory.target().update(target.getControllerId()).description(INVALID_TEXT_HTML)))
.as("target with invalid description should not be updated");
}
@Step
@@ -223,11 +234,21 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.name(RandomStringUtils.randomAlphanumeric(65))))
.as("target with too long name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.create(entityFactory.target().create().controllerId("a").name(INVALID_TEXT_HTML)))
.as("target with invalidname should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.name(RandomStringUtils.randomAlphanumeric(65))))
.as("target with too long name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.update(entityFactory.target().update(target.getControllerId()).name(INVALID_TEXT_HTML)))
.as("target with invalid name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(
() -> targetManagement.update(entityFactory.target().update(target.getControllerId()).name("")))
@@ -243,11 +264,21 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.securityToken(RandomStringUtils.randomAlphanumeric(129))))
.as("target with too long token should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.create(entityFactory.target().create().controllerId("a").securityToken(INVALID_TEXT_HTML)))
.as("target with invalid token should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.securityToken(RandomStringUtils.randomAlphanumeric(129))))
.as("target with too long token should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.update(
entityFactory.target().update(target.getControllerId()).securityToken(INVALID_TEXT_HTML)))
.as("target with invalid token should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement
.update(entityFactory.target().update(target.getControllerId()).securityToken("")))
@@ -262,10 +293,20 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.address(RandomStringUtils.randomAlphanumeric(513))))
.as("target with too long address should not be created");
assertThatExceptionOfType(InvalidTargetAddressException.class)
.isThrownBy(() -> targetManagement
.create(entityFactory.target().create().controllerId("a").address(INVALID_TEXT_HTML)))
.as("target with invalid should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.update(entityFactory.target().update(target.getControllerId())
.address(RandomStringUtils.randomAlphanumeric(513))))
.as("target with too long address should not be updated");
assertThatExceptionOfType(InvalidTargetAddressException.class)
.isThrownBy(() -> targetManagement
.update(entityFactory.target().update(target.getControllerId()).address(INVALID_TEXT_HTML)))
.as("target with invalid address should not be updated");
}
@Step
@@ -283,18 +324,15 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
.create(entityFactory.target().create().controllerId(RandomStringUtils.randomAlphanumeric(65))))
.as("target with too long controller id should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(
() -> targetManagement.create(entityFactory.target().create().controllerId(INVALID_TEXT_HTML)))
.as("target with invalid controller id should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId(" ")))
.as(WHITESPACE_ERROR);
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId(" a")))
.as(WHITESPACE_ERROR);
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a ")))
.as(WHITESPACE_ERROR);
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetManagement.create(entityFactory.target().create().controllerId("a b")))
.as(WHITESPACE_ERROR);
@@ -330,8 +368,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
final List<Target> assignedTargets = targetManagement.assignTag(assignTarget, targetTag.getId());
assertThat(assignedTargets.size()).as("Assigned targets are wrong").isEqualTo(4);
assignedTargets.forEach(target -> assertThat(
targetTagManagement.findByTarget(PAGE, target.getControllerId()).getNumberOfElements())
.isEqualTo(1));
targetTagManagement.findByTarget(PAGE, target.getControllerId()).getNumberOfElements()).isEqualTo(1));
TargetTag findTargetTag = targetTagManagement.getByName("Tag1").get();
assertThat(assignedTargets.size()).as("Assigned targets are wrong")
@@ -339,8 +376,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
final Target unAssignTarget = targetManagement.unAssignTag("targetId123", findTargetTag.getId());
assertThat(unAssignTarget.getControllerId()).as("Controller id is wrong").isEqualTo("targetId123");
assertThat(targetTagManagement.findByTarget(PAGE, unAssignTarget.getControllerId()))
.as("Tag size is wrong").isEmpty();
assertThat(targetTagManagement.findByTarget(PAGE, unAssignTarget.getControllerId())).as("Tag size is wrong")
.isEmpty();
findTargetTag = targetTagManagement.getByName("Tag1").get();
assertThat(targetManagement.findByTag(PAGE, targetTag.getId())).as("Assigned targets are wrong").hasSize(3);
assertThat(targetManagement.findByRsqlAndTag(PAGE, "controllerId==targetId123", targetTag.getId()))
@@ -631,16 +668,16 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
t2Tags.forEach(tag -> targetManagement.assignTag(Arrays.asList(t2.getControllerId()), tag.getId()));
final Target t11 = targetManagement.getByControllerID(t1.getControllerId()).get();
assertThat(targetTagManagement.findByTarget(PAGE, t11.getControllerId()).getContent())
.as("Tag size is wrong").hasSize(noT1Tags).containsAll(t1Tags);
assertThat(targetTagManagement.findByTarget(PAGE, t11.getControllerId()).getContent())
.as("Tag size is wrong").hasSize(noT1Tags).doesNotContain(Iterables.toArray(t2Tags, TargetTag.class));
assertThat(targetTagManagement.findByTarget(PAGE, t11.getControllerId()).getContent()).as("Tag size is wrong")
.hasSize(noT1Tags).containsAll(t1Tags);
assertThat(targetTagManagement.findByTarget(PAGE, t11.getControllerId()).getContent()).as("Tag size is wrong")
.hasSize(noT1Tags).doesNotContain(Iterables.toArray(t2Tags, TargetTag.class));
final Target t21 = targetManagement.getByControllerID(t2.getControllerId()).get();
assertThat(targetTagManagement.findByTarget(PAGE, t21.getControllerId()).getContent())
.as("Tag size is wrong").hasSize(noT2Tags).containsAll(t2Tags);
assertThat(targetTagManagement.findByTarget(PAGE, t21.getControllerId()).getContent())
.as("Tag size is wrong").hasSize(noT2Tags).doesNotContain(Iterables.toArray(t1Tags, TargetTag.class));
assertThat(targetTagManagement.findByTarget(PAGE, t21.getControllerId()).getContent()).as("Tag size is wrong")
.hasSize(noT2Tags).containsAll(t2Tags);
assertThat(targetTagManagement.findByTarget(PAGE, t21.getControllerId()).getContent()).as("Tag size is wrong")
.hasSize(noT2Tags).doesNotContain(Iterables.toArray(t1Tags, TargetTag.class));
}
@Test

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.fail;
import java.util.ArrayList;
@@ -17,6 +18,9 @@ import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.ConstraintViolationException;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.hawkbit.repository.TargetTagManagement;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagUpdatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
@@ -33,6 +37,7 @@ import org.junit.Test;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Step;
import ru.yandex.qatools.allure.annotations.Stories;
/**
@@ -66,6 +71,89 @@ public class TargetTagManagementTest extends AbstractJpaIntegrationTest {
verifyThrownExceptionBy(() -> targetTagManagement.findByTarget(PAGE, NOT_EXIST_ID), "Target");
}
@Test
@Description("Verify that a tag with with invalid properties cannot be created or updated")
public void createAndUpdateTagWithInvalidFields() {
final TargetTag tag = targetTagManagement
.create(entityFactory.tag().create().name("tag1").description("tagdesc1"));
createAndUpdateTagWithInvalidDescription(tag);
createAndUpdateTagWithInvalidColour(tag);
createAndUpdateTagWithInvalidName(tag);
}
@Step
private void createAndUpdateTagWithInvalidDescription(final Tag tag) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.create(
entityFactory.tag().create().name("a").description(RandomStringUtils.randomAlphanumeric(513))))
.as("tag with too long description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> targetTagManagement.create(entityFactory.tag().create().name("a").description(INVALID_TEXT_HTML)))
.as("tag with invalid description should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.update(
entityFactory.tag().update(tag.getId()).description(RandomStringUtils.randomAlphanumeric(513))))
.as("tag with too long description should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement
.update(entityFactory.tag().update(tag.getId()).description(INVALID_TEXT_HTML)))
.as("tag with invalid description should not be updated");
}
@Step
private void createAndUpdateTagWithInvalidColour(final Tag tag) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.create(
entityFactory.tag().create().name("a").colour(RandomStringUtils.randomAlphanumeric(17))))
.as("tag with too long colour should not be created");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> targetTagManagement.create(entityFactory.tag().create().name("a").colour(INVALID_TEXT_HTML)))
.as("tag with invalid colour should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.update(
entityFactory.tag().update(tag.getId()).colour(RandomStringUtils.randomAlphanumeric(17))))
.as("tag with too long colour should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> targetTagManagement.update(entityFactory.tag().update(tag.getId()).colour(INVALID_TEXT_HTML)))
.as("tag with invalid colour should not be updated");
}
@Step
private void createAndUpdateTagWithInvalidName(final Tag tag) {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement
.create(entityFactory.tag().create().name(RandomStringUtils.randomAlphanumeric(65))))
.as("tag with too long name should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.create(entityFactory.tag().create().name(INVALID_TEXT_HTML)))
.as("tag with invalidname should not be created");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement
.update(entityFactory.tag().update(tag.getId()).name(RandomStringUtils.randomAlphanumeric(65))))
.as("tag with too long name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(
() -> targetTagManagement.update(entityFactory.tag().update(tag.getId()).name(INVALID_TEXT_HTML)))
.as("tag with invalid name should not be updated");
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> targetTagManagement.update(entityFactory.tag().update(tag.getId()).name("")))
.as("tag with too short name should not be updated");
}
@Test
@Description("Verifies the toogle mechanism by means on assigning tag if at least on target in the list does not have"
+ "the tag yet. Unassign if all of them have the tag already.")
@@ -80,8 +168,8 @@ public class TargetTagManagementTest extends AbstractJpaIntegrationTest {
TargetTagAssignmentResult result = toggleTagAssignment(groupA, tag);
assertThat(result.getAlreadyAssigned()).isEqualTo(0);
assertThat(result.getAssigned()).isEqualTo(20);
assertThat(result.getAssignedEntity()).containsAll(targetManagement.getByControllerID(
groupA.stream().map(target -> target.getControllerId()).collect(Collectors.toList())));
assertThat(result.getAssignedEntity()).containsAll(targetManagement
.getByControllerID(groupA.stream().map(Target::getControllerId).collect(Collectors.toList())));
assertThat(result.getUnassigned()).isEqualTo(0);
assertThat(result.getUnassignedEntity()).isEmpty();
assertThat(result.getTargetTag()).isEqualTo(tag);
@@ -90,8 +178,8 @@ public class TargetTagManagementTest extends AbstractJpaIntegrationTest {
result = toggleTagAssignment(concat(groupA, groupB), tag);
assertThat(result.getAlreadyAssigned()).isEqualTo(20);
assertThat(result.getAssigned()).isEqualTo(20);
assertThat(result.getAssignedEntity()).containsAll(targetManagement.getByControllerID(
groupB.stream().map(target -> target.getControllerId()).collect(Collectors.toList())));
assertThat(result.getAssignedEntity()).containsAll(targetManagement
.getByControllerID(groupB.stream().map(Target::getControllerId).collect(Collectors.toList())));
assertThat(result.getUnassigned()).isEqualTo(0);
assertThat(result.getUnassignedEntity()).isEmpty();
assertThat(result.getTargetTag()).isEqualTo(tag);
@@ -132,10 +220,8 @@ public class TargetTagManagementTest extends AbstractJpaIntegrationTest {
assertThat(targetTagRepository.findByNameEquals("kai1").get().getDescription()).as("wrong tag ed")
.isEqualTo("kai2");
assertThat(targetTagManagement.getByName("kai1").get().getColour()).as("wrong tag found")
.isEqualTo("colour");
assertThat(targetTagManagement.get(tag.getId()).get().getColour()).as("wrong tag found")
.isEqualTo("colour");
assertThat(targetTagManagement.getByName("kai1").get().getColour()).as("wrong tag found").isEqualTo("colour");
assertThat(targetTagManagement.get(tag.getId()).get().getColour()).as("wrong tag found").isEqualTo("colour");
}
@Test