Add not found DS test & improve EntityNotFoundException (#1896)
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -13,15 +13,16 @@ import java.io.Serial;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
import org.eclipse.hawkbit.exception.AbstractServerRtException;
|
import org.eclipse.hawkbit.exception.AbstractServerRtException;
|
||||||
import org.eclipse.hawkbit.exception.SpServerError;
|
import org.eclipse.hawkbit.exception.SpServerError;
|
||||||
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
import org.eclipse.hawkbit.repository.model.BaseEntity;
|
||||||
import org.eclipse.hawkbit.repository.model.MetaData;
|
import org.eclipse.hawkbit.repository.model.MetaData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the {@link EntityNotFoundException} is thrown when a entity queried but not
|
* the {@link EntityNotFoundException} is thrown when a entity queried but not found.
|
||||||
* found.
|
|
||||||
*/
|
*/
|
||||||
|
@Getter
|
||||||
public class EntityNotFoundException extends AbstractServerRtException {
|
public class EntityNotFoundException extends AbstractServerRtException {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
@@ -29,6 +30,10 @@ public class EntityNotFoundException extends AbstractServerRtException {
|
|||||||
|
|
||||||
private static final SpServerError THIS_ERROR = SpServerError.SP_REPO_ENTITY_NOT_EXISTS;
|
private static final SpServerError THIS_ERROR = SpServerError.SP_REPO_ENTITY_NOT_EXISTS;
|
||||||
|
|
||||||
|
private Class<?> type;
|
||||||
|
private Object entityId;
|
||||||
|
private String key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default constructor.
|
* Default constructor.
|
||||||
*/
|
*/
|
||||||
@@ -39,8 +44,7 @@ public class EntityNotFoundException extends AbstractServerRtException {
|
|||||||
/**
|
/**
|
||||||
* Parameterized constructor.
|
* Parameterized constructor.
|
||||||
*
|
*
|
||||||
* @param cause
|
* @param cause of the exception
|
||||||
* of the exception
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final Throwable cause) {
|
public EntityNotFoundException(final Throwable cause) {
|
||||||
super(THIS_ERROR, cause);
|
super(THIS_ERROR, cause);
|
||||||
@@ -49,10 +53,8 @@ public class EntityNotFoundException extends AbstractServerRtException {
|
|||||||
/**
|
/**
|
||||||
* Parameterized constructor.
|
* Parameterized constructor.
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message of the exception
|
||||||
* of the exception
|
* @param cause of the exception
|
||||||
* @param cause
|
|
||||||
* of the exception
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final String message, final Throwable cause) {
|
public EntityNotFoundException(final String message, final Throwable cause) {
|
||||||
super(message, THIS_ERROR, cause);
|
super(message, THIS_ERROR, cause);
|
||||||
@@ -61,8 +63,7 @@ public class EntityNotFoundException extends AbstractServerRtException {
|
|||||||
/**
|
/**
|
||||||
* Parameterized constructor.
|
* Parameterized constructor.
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message of the exception
|
||||||
* of the exception
|
|
||||||
*/
|
*/
|
||||||
protected EntityNotFoundException(final String message) {
|
protected EntityNotFoundException(final String message) {
|
||||||
super(message, THIS_ERROR);
|
super(message, THIS_ERROR);
|
||||||
@@ -71,59 +72,55 @@ public class EntityNotFoundException extends AbstractServerRtException {
|
|||||||
/**
|
/**
|
||||||
* Parameterized constructor for {@link BaseEntity} not found.
|
* Parameterized constructor for {@link BaseEntity} not found.
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type of the entity that was not found
|
||||||
* of the entity that was not found
|
* @param entityId of the {@link BaseEntity}
|
||||||
*
|
|
||||||
* @param entityId
|
|
||||||
* of the {@link BaseEntity}
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final Class<? extends BaseEntity> type, final Object entityId) {
|
public EntityNotFoundException(final Class<? extends BaseEntity> type, final Object entityId) {
|
||||||
this(type.getSimpleName() + " with given identifier {" + entityId + "} does not exist.");
|
super(type.getSimpleName() + " with given identifier {" + entityId + "} does not exist.", THIS_ERROR);
|
||||||
|
this.type = type;
|
||||||
|
this.entityId = entityId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameterized constructor for {@link MetaData} not found.
|
* Parameterized constructor for {@link MetaData} not found.
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type of the entity that was not found
|
||||||
* of the entity that was not found
|
* @param entityId of the {@link BaseEntity} the {@link MetaData} was for
|
||||||
* @param entityId
|
* @param key for the {@link MetaData} entry
|
||||||
* of the {@link BaseEntity} the {@link MetaData} was for
|
|
||||||
* @param key
|
|
||||||
* for the {@link MetaData} entry
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final Class<? extends MetaData> type, final Long entityId, final String key) {
|
public EntityNotFoundException(final Class<? extends MetaData> type, final Long entityId, final String key) {
|
||||||
this(type, String.valueOf(entityId), key);
|
this(type, String.valueOf(entityId), key);
|
||||||
|
this.type = type;
|
||||||
|
this.entityId = entityId;
|
||||||
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameterized constructor for {@link MetaData} not found.
|
* Parameterized constructor for {@link MetaData} not found.
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type of the entity that was not found
|
||||||
* of the entity that was not found
|
* @param entityId of the {@link BaseEntity} the {@link MetaData} was for
|
||||||
* @param entityId
|
* @param key for the {@link MetaData} entry
|
||||||
* of the {@link BaseEntity} the {@link MetaData} was for
|
|
||||||
* @param key
|
|
||||||
* for the {@link MetaData} entry
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final Class<? extends MetaData> type, final String entityId, final String key) {
|
public EntityNotFoundException(final Class<? extends MetaData> type, final String entityId, final String key) {
|
||||||
this(type.getSimpleName() + " for given entity {" + entityId + "} and with key {" + key + "} does not exist.");
|
this(type.getSimpleName() + " for given entity {" + entityId + "} and with key {" + key + "} does not exist.");
|
||||||
|
this.type = type;
|
||||||
|
this.entityId = entityId;
|
||||||
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parameterized constructor for a list of {@link BaseEntity}s not found.
|
* Parameterized constructor for a list of {@link BaseEntity}s not found.
|
||||||
*
|
*
|
||||||
* @param type
|
* @param type of the entity that was not found
|
||||||
* of the entity that was not found
|
* @param expected collection of the {@link BaseEntity#getId()}s
|
||||||
*
|
* @param found collection of the {@link BaseEntity#getId()}s
|
||||||
* @param expected
|
|
||||||
* collection of the {@link BaseEntity#getId()}s
|
|
||||||
* @param found
|
|
||||||
* collection of the {@link BaseEntity#getId()}s
|
|
||||||
*/
|
*/
|
||||||
public EntityNotFoundException(final Class<? extends BaseEntity> type, final Collection<?> expected,
|
public EntityNotFoundException(final Class<? extends BaseEntity> type, final Collection<?> expected,
|
||||||
final Collection<?> found) {
|
final Collection<?> found) {
|
||||||
this(type.getSimpleName() + "s with given identifiers {" + expected.stream().filter(id -> !found.contains(id))
|
this(type.getSimpleName() + "s with given identifiers {" + expected.stream().filter(id -> !found.contains(id))
|
||||||
.map(String::valueOf).collect(Collectors.joining(",")) + "} do not exist.");
|
.map(String::valueOf).collect(Collectors.joining(",")) + "} do not exist.");
|
||||||
|
this.type = type;
|
||||||
|
this.entityId = expected.stream().filter(id -> !found.contains(id)).map(String::valueOf);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -11,11 +11,14 @@ package org.eclipse.hawkbit.repository.jpa.management;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@@ -25,6 +28,7 @@ import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagUpda
|
|||||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
||||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagUpdatedEvent;
|
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagUpdatedEvent;
|
||||||
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
||||||
|
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||||
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
|
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
|
||||||
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||||
import org.eclipse.hawkbit.repository.model.DistributionSetFilter;
|
import org.eclipse.hawkbit.repository.model.DistributionSetFilter;
|
||||||
@@ -145,8 +149,7 @@ public class DistributionSetTagManagementTest extends AbstractJpaIntegrationTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verifies the toogle mechanism by means on assigning tag if at least on DS in the list does not have"
|
@Description("Verifies assign/unassign.")
|
||||||
+ "the tag yet. Unassign if all of them have the tag already.")
|
|
||||||
public void assignAndUnassignDistributionSetTags() {
|
public void assignAndUnassignDistributionSetTags() {
|
||||||
final Collection<DistributionSet> groupA = testdataFactory.createDistributionSets(20);
|
final Collection<DistributionSet> groupA = testdataFactory.createDistributionSets(20);
|
||||||
final Collection<DistributionSet> groupB = testdataFactory.createDistributionSets("unassigned", 20);
|
final Collection<DistributionSet> groupB = testdataFactory.createDistributionSets("unassigned", 20);
|
||||||
@@ -179,6 +182,40 @@ public class DistributionSetTagManagementTest extends AbstractJpaIntegrationTest
|
|||||||
assertThat(distributionSetManagement.findByTag(Pageable.unpaged(), tag.getId()).getContent()).isEmpty();
|
assertThat(distributionSetManagement.findByTag(Pageable.unpaged(), tag.getId()).getContent()).isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Random RND = new Random();
|
||||||
|
@Test
|
||||||
|
@Description("Verifies that tagging of set containing missing DS throws meaningful and correct exception.")
|
||||||
|
public void failOnMissingDs() {
|
||||||
|
final Collection<Long> group = testdataFactory.createDistributionSets(5).stream()
|
||||||
|
.map(DistributionSet::getId)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
final DistributionSetTag tag = distributionSetTagManagement
|
||||||
|
.create(entityFactory.tag().create().name("tag1").description("tagdesc1"));
|
||||||
|
final List<Long> missing = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
while (true) {
|
||||||
|
final Long id = RND.nextLong();
|
||||||
|
if (!group.contains(id)) {
|
||||||
|
missing.add(id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Collections.sort(missing);
|
||||||
|
final Collection<Long> withMissing = concat(group, missing);
|
||||||
|
assertThatThrownBy(() -> distributionSetManagement.assignTag(withMissing, tag.getId()))
|
||||||
|
.matches(e -> {
|
||||||
|
if (e instanceof EntityNotFoundException enfe) {
|
||||||
|
if (enfe.getType().equals(DistributionSet.class)) {
|
||||||
|
if (enfe.getEntityId() instanceof Collection entityId) {
|
||||||
|
return entityId.stream().sorted().toList().equals(missing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Ensures that a created tag is persisted in the repository as defined.")
|
@Description("Ensures that a created tag is persisted in the repository as defined.")
|
||||||
public void createDistributionSetTag() {
|
public void createDistributionSetTag() {
|
||||||
|
|||||||
@@ -11,11 +11,14 @@ package org.eclipse.hawkbit.repository.jpa.management;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import jakarta.validation.ConstraintViolationException;
|
import jakarta.validation.ConstraintViolationException;
|
||||||
@@ -26,8 +29,11 @@ import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagUpda
|
|||||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
import org.eclipse.hawkbit.repository.event.remote.entity.TargetCreatedEvent;
|
||||||
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagUpdatedEvent;
|
import org.eclipse.hawkbit.repository.event.remote.entity.TargetTagUpdatedEvent;
|
||||||
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
|
||||||
|
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
|
||||||
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
|
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
|
||||||
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
|
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
|
||||||
|
import org.eclipse.hawkbit.repository.model.DistributionSet;
|
||||||
|
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
|
||||||
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
import org.eclipse.hawkbit.repository.model.NamedEntity;
|
||||||
import org.eclipse.hawkbit.repository.model.Tag;
|
import org.eclipse.hawkbit.repository.model.Tag;
|
||||||
import org.eclipse.hawkbit.repository.model.Target;
|
import org.eclipse.hawkbit.repository.model.Target;
|
||||||
@@ -140,8 +146,7 @@ class TargetTagManagementTest extends AbstractJpaIntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Description("Verifies the toogle mechanism by means on assigning tag if at least on target in the list does not have"
|
@Description("Verifies assign/unassign.")
|
||||||
+ "the tag yet. Unassign if all of them have the tag already.")
|
|
||||||
void assignAndUnassignTargetTags() {
|
void assignAndUnassignTargetTags() {
|
||||||
final List<Target> groupA = testdataFactory.createTargets(20);
|
final List<Target> groupA = testdataFactory.createTargets(20);
|
||||||
final List<Target> groupB = testdataFactory.createTargets(20, "groupb", "groupb");
|
final List<Target> groupB = testdataFactory.createTargets(20, "groupb", "groupb");
|
||||||
|
|||||||
Reference in New Issue
Block a user