Enable specifying target type when created using DMF API (#1472)

Extension of DMF API with possibility of setting target
type name when creating target. If a target type with the
provided name is found (was created beforehand) then it
is associated with the new target.

Signed-off-by: Ondrej Charvat <ondrej.charvat@proton.me>
This commit is contained in:
charvadzo
2024-01-22 14:01:00 +01:00
committed by GitHub
parent af56b71d53
commit 49a5509e89
8 changed files with 236 additions and 24 deletions

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository.jpa.management;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions.CONTROLLER_ROLE;
import static org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions.CONTROLLER_ROLE_ANONYMOUS;
import static org.eclipse.hawkbit.repository.jpa.configuration.Constants.TX_RT_MAX;
import static org.eclipse.hawkbit.repository.model.Action.ActionType.DOWNLOAD_ONLY;
@@ -52,6 +53,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedE
import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleUpdatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTypeCreatedEvent;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.CancelActionNotAllowedException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
@@ -77,6 +79,8 @@ import org.eclipse.hawkbit.repository.test.matcher.Expect;
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
import org.eclipse.hawkbit.repository.test.util.TargetTestData;
import org.eclipse.hawkbit.repository.test.util.SecurityContextSwitch;
import org.eclipse.hawkbit.repository.test.util.WithUser;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
@@ -510,6 +514,7 @@ class ControllerManagementTest extends AbstractJpaIntegrationTest {
@Test
@Description("Register a controller which does not exist")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2) })
void findOrRegisterTargetIfItDoesNotExist() {
@@ -523,15 +528,122 @@ class ControllerManagementTest extends AbstractJpaIntegrationTest {
@Test
@Description("Register a controller with name which does not exist and update its name")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2), @Expect(type = TargetUpdatedEvent.class, count = 1) })
void findOrRegisterTargetIfItDoesNotExistWithName() {
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST, "TestName", null);
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
"ChangedTestName");
"ChangedTestName", null);
assertThat(target.getId()).as("Target should be the equals").isEqualTo(sameTarget.getId());
assertThat(target.getName()).as("Taget names should be different").isNotEqualTo(sameTarget.getName());
assertThat(sameTarget.getName()).as("Taget name should be changed").isEqualTo("ChangedTestName");
assertThat(target.getName()).as("Target names should be different").isNotEqualTo(sameTarget.getName());
assertThat(sameTarget.getName()).as("Target name should be changed").isEqualTo("ChangedTestName");
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
@Test
@Description("Register a controller which does not exist with existing target type and update its target type to another existing one")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class, count = 2),
@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 1) })
void findOrRegisterTargetIfItDoesNotExistWithExistingTypeAndUpdateToExistingType() {
createTargetType("knownTargetTypeName1");
createTargetType("knownTargetTypeName2");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "knownTargetTypeName1");
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "knownTargetTypeName2");
assertThat(target.getId()).as("Target should be the same").isEqualTo(sameTarget.getId());
assertThat(target.getTargetType().getName()).as("Target type should be set")
.isEqualTo("knownTargetTypeName1");
assertThat(sameTarget.getTargetType().getName()).as("Target type should be changed")
.isEqualTo("knownTargetTypeName2");
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
private void createTargetType(String targetTypeName) {
systemSecurityContext.runAsSystem(() ->
targetTypeManagement.create(entityFactory.targetType().create().name(targetTypeName)));
}
@Test
@Description("Register a controller which does not exist with existing target type and update its target type to non existing one")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class, count = 1),
@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2) })
void findOrRegisterTargetIfItDoesNotExistWithExistingTypeAndUpdateToNonExistingType() {
createTargetType("knownTargetTypeName");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null,"knownTargetTypeName");
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null,"unknownTargetTypeName");
assertThat(target.getId()).as("Target should be the same").isEqualTo(sameTarget.getId());
assertThat(sameTarget.getTargetType().getName()).as("Target type should be unchanged")
.isEqualTo("knownTargetTypeName");
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
@Test
@Description("Register a controller which does not exist with existing target type and unassign its target type")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class, count = 1),
@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 1) })
void findOrRegisterTargetIfItDoesNotExistWithExistingTypeAndUnassignType() {
createTargetType("knownTargetTypeName");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "knownTargetTypeName");
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "");
assertThat(target.getId()).as("Target should be the same").isEqualTo(sameTarget.getId());
assertThat(sameTarget.getTargetType()).as("Target type should be unassigned")
.isNull();
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
@Test
@Description("Register a controller which does not exist without target type and update its target type to existing one")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class, count = 1),
@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 1) })
void findOrRegisterTargetIfItDoesNotExistWithoutTypeAndUpdateToExistingType() {
createTargetType("knownTargetTypeName");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, null);
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "knownTargetTypeName");
assertThat(target.getId()).as("Target should be the equals").isEqualTo(sameTarget.getId());
assertThat(target.getTargetType()).as("Target type should not be assigned")
.isNull();
assertThat(sameTarget.getTargetType().getName()).as("Target type should be assigned")
.isEqualTo("knownTargetTypeName");
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
@Test
@Description("Register a controller which does not exist with non existing target type and update its target type to existing one")
@WithUser(principal = "controller", authorities = { CONTROLLER_ROLE })
@ExpectEvents({ @Expect(type = TargetTypeCreatedEvent.class, count = 1),
@Expect(type = TargetCreatedEvent.class, count = 1),
@Expect(type = TargetPollEvent.class, count = 2),
@Expect(type = TargetUpdatedEvent.class, count = 1) })
void findOrRegisterTargetIfItDoesNotExistWithNonExistingTypeAndUpdateToExistingType() {
createTargetType("knownTargetTypeName");
final Target target = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "unknownTargetTypeName");
final Target sameTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist("AA", LOCALHOST,
null, "knownTargetTypeName");
assertThat(target.getId()).as("Target should be the equals").isEqualTo(sameTarget.getId());
assertThat(target.getTargetType()).as("Target type should not be assigned")
.isNull();
assertThat(sameTarget.getTargetType().getName()).as("Target type should be assigned")
.isEqualTo("knownTargetTypeName");
assertThat(targetRepository.count()).as("Only 1 target should be registred").isEqualTo(1L);
}
@@ -616,7 +728,7 @@ class ControllerManagementTest extends AbstractJpaIntegrationTest {
assertThat(newTarget.getName()).isEqualTo(controllerId);
final Target firstTimeUpdatedTarget = controllerManagement.findOrRegisterTargetIfItDoesNotExist(controllerId,
LOCALHOST, targetName);
LOCALHOST, targetName, null);
assertThat(firstTimeUpdatedTarget.getName()).isEqualTo(targetName);
// Name should not change to default (name=targetId) if target is