From b6d73c578f56e16a6af74d4e311d7efdc34f9ff2 Mon Sep 17 00:00:00 2001 From: Kai Zimmermann Date: Wed, 21 Jun 2017 14:59:47 +0200 Subject: [PATCH] Fix Controller Management null address (#550) * Forbid null address in controller management. Signed-off-by: kaizimmerm * Constant. Signed-off-by: kaizimmerm * Fix AMQP test. Signed-off-by: kaizimmerm --- ...pMessageHandlerServiceIntegrationTest.java | 12 ++++++----- .../test/AbstractAmqpIntegrationTest.java | 4 ++++ .../repository/ControllerManagement.java | 2 +- .../jpa/ControllerManagementTest.java | 20 ++++++++++--------- .../repository/jpa/TargetManagementTest.java | 4 +++- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageHandlerServiceIntegrationTest.java b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageHandlerServiceIntegrationTest.java index a78c2a98f..5bf8ad31a 100644 --- a/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageHandlerServiceIntegrationTest.java +++ b/hawkbit-dmf/hawkbit-dmf-amqp/src/test/java/org/eclipse/hawkbit/integration/AmqpMessageHandlerServiceIntegrationTest.java @@ -35,6 +35,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent; import org.eclipse.hawkbit.repository.model.Action.Status; import org.eclipse.hawkbit.repository.model.DistributionSet; import org.eclipse.hawkbit.repository.model.DistributionSetAssignmentResult; +import org.eclipse.hawkbit.repository.model.Target; import org.eclipse.hawkbit.repository.model.TargetUpdateStatus; import org.eclipse.hawkbit.repository.test.matcher.Expect; import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents; @@ -428,11 +429,11 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AmqpServiceIntegra @Expect(type = ActionCreatedEvent.class, count = 1), @Expect(type = SoftwareModuleCreatedEvent.class, count = 3), @Expect(type = DistributionSetCreatedEvent.class, count = 1), - @Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 1) }) + @Expect(type = TargetUpdatedEvent.class, count = 1), @Expect(type = TargetPollEvent.class, count = 2) }) public void receiveDownLoadAndInstallMessageAfterAssignment() { // setup - controllerManagement.findOrRegisterTargetIfItDoesNotexist(REGISTER_TARGET, null); + controllerManagement.findOrRegisterTargetIfItDoesNotexist(REGISTER_TARGET, TEST_URI); final DistributionSet distributionSet = testdataFactory.createDistributionSet(UUID.randomUUID().toString()); assignDistributionSet(distributionSet.getId(), REGISTER_TARGET); @@ -453,18 +454,19 @@ public class AmqpMessageHandlerServiceIntegrationTest extends AmqpServiceIntegra @Expect(type = DistributionSetCreatedEvent.class, count = 1), @Expect(type = CancelTargetAssignmentEvent.class, count = 1), @Expect(type = ActionUpdatedEvent.class, count = 1), @Expect(type = TargetUpdatedEvent.class, count = 1), - @Expect(type = TargetPollEvent.class, count = 1) }) + @Expect(type = TargetPollEvent.class, count = 2) }) public void receiveCancelUpdateMessageAfterAssignmentWasCanceled() { // Setup - controllerManagement.findOrRegisterTargetIfItDoesNotexist(REGISTER_TARGET, null); + final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist(REGISTER_TARGET, TEST_URI); final DistributionSet distributionSet = testdataFactory.createDistributionSet(UUID.randomUUID().toString()); final DistributionSetAssignmentResult distributionSetAssignmentResult = assignDistributionSet( distributionSet.getId(), REGISTER_TARGET); deploymentManagement.cancelAction(distributionSetAssignmentResult.getActions().get(0)); // test - registerAndAssertTargetWithExistingTenant(REGISTER_TARGET, 1, TargetUpdateStatus.PENDING, "bumlux"); + registerSameTargetAndAssertBasedOnLastPolling(REGISTER_TARGET, 1, TargetUpdateStatus.PENDING, + target.getLastTargetQuery()); // verify assertCancelActionMessage(distributionSetAssignmentResult.getActions().get(0)); diff --git a/hawkbit-dmf/hawkbit-dmf-rabbitmq-test/src/main/java/org/eclipse/hawkbit/rabbitmq/test/AbstractAmqpIntegrationTest.java b/hawkbit-dmf/hawkbit-dmf-rabbitmq-test/src/main/java/org/eclipse/hawkbit/rabbitmq/test/AbstractAmqpIntegrationTest.java index 8f8dd5a5a..1f0a8e767 100644 --- a/hawkbit-dmf/hawkbit-dmf-rabbitmq-test/src/main/java/org/eclipse/hawkbit/rabbitmq/test/AbstractAmqpIntegrationTest.java +++ b/hawkbit-dmf/hawkbit-dmf-rabbitmq-test/src/main/java/org/eclipse/hawkbit/rabbitmq/test/AbstractAmqpIntegrationTest.java @@ -8,10 +8,12 @@ */ package org.eclipse.hawkbit.rabbitmq.test; +import java.net.URI; import java.util.concurrent.TimeUnit; import org.eclipse.hawkbit.repository.jpa.RepositoryApplicationConfiguration; import org.eclipse.hawkbit.repository.test.util.AbstractIntegrationTest; +import org.eclipse.hawkbit.util.IpUtil; import org.junit.Before; import org.junit.Rule; import org.springframework.amqp.core.Message; @@ -35,6 +37,8 @@ import com.jayway.awaitility.core.ConditionFactory; @DirtiesContext(classMode = ClassMode.AFTER_CLASS) public abstract class AbstractAmqpIntegrationTest extends AbstractIntegrationTest { + protected static final URI TEST_URI = IpUtil.createAmqpUri("testHost", "testExcange"); + @Rule @Autowired public BrokerRunning brokerRunning; diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/ControllerManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/ControllerManagement.java index 1164d8668..03a251ce1 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/ControllerManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/ControllerManagement.java @@ -168,7 +168,7 @@ public interface ControllerManagement { * @return target reference */ @PreAuthorize(SpringEvalExpressions.IS_CONTROLLER) - Target findOrRegisterTargetIfItDoesNotexist(@NotEmpty String controllerId, URI address); + Target findOrRegisterTargetIfItDoesNotexist(@NotEmpty String controllerId, @NotNull URI address); /** * Retrieves last {@link Action} for a download of an artifact of given diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/ControllerManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/ControllerManagementTest.java index 357b4bbdc..3b95c594c 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/ControllerManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/ControllerManagementTest.java @@ -24,6 +24,7 @@ import javax.validation.ConstraintViolationException; import org.apache.commons.lang3.RandomUtils; import org.eclipse.hawkbit.repository.RepositoryProperties; import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent; +import org.eclipse.hawkbit.repository.event.remote.TargetPollEvent; import org.eclipse.hawkbit.repository.event.remote.entity.ActionCreatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.ActionUpdatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.CancelTargetAssignmentEvent; @@ -60,6 +61,7 @@ import ru.yandex.qatools.allure.annotations.Stories; @Features("Component Tests - Repository") @Stories("Controller Management") public class ControllerManagementTest extends AbstractJpaIntegrationTest { + private static final URI LOCALHOST = URI.create("http://127.0.0.1"); @Autowired private RepositoryProperties repositoryProperties; @@ -419,21 +421,21 @@ public class ControllerManagementTest extends AbstractJpaIntegrationTest { @Test @Description("Register a controller which does not exist") + @ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1), + @Expect(type = TargetPollEvent.class, count = 2) }) public void findOrRegisterTargetIfItDoesNotexist() { - final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist("AA", null); + final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotexist("AA", + LOCALHOST); assertThat(target).as("target should not be null").isNotNull(); - final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotexist("AA", null); + final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotexist("AA", + LOCALHOST); assertThat(target.getId()).as("Target should be the equals").isEqualTo(sameTarget.getId()); assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L); - // throws exception - try { - controllerManagement.findOrRegisterTargetIfItDoesNotexist("", null); - fail("should fail as target does not exist"); - } catch (final ConstraintViolationException e) { - - } + assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy( + () -> controllerManagement.findOrRegisterTargetIfItDoesNotexist("", LOCALHOST)) + .as("register target with empty controllerId should fail"); } @Test diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java index b8ebd7483..47fff1a2c 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/TargetManagementTest.java @@ -28,6 +28,7 @@ import org.apache.commons.lang3.RandomStringUtils; import org.eclipse.hawkbit.im.authentication.SpPermission; import org.eclipse.hawkbit.repository.event.remote.TargetAssignDistributionSetEvent; import org.eclipse.hawkbit.repository.event.remote.TargetDeletedEvent; +import org.eclipse.hawkbit.repository.event.remote.TargetPollEvent; import org.eclipse.hawkbit.repository.event.remote.entity.ActionCreatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.ActionUpdatedEvent; import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent; @@ -797,7 +798,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest { @Test @Description("Tests the a target can be read with only the read target permission") - @Expect(type = TargetCreatedEvent.class, count = 0) + @ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1), + @Expect(type = TargetPollEvent.class, count = 1) }) public void targetCanBeReadWithOnlyReadTargetPermission() throws Exception { final String knownTargetControllerId = "readTarget"; controllerManagement.findOrRegisterTargetIfItDoesNotexist(knownTargetControllerId, new URI("http://127.0.0.1"));