Feature assert events within tests (#341)
* Count and assert repository events within a test. Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>
This commit is contained in:
committed by
Kai Zimmermann
parent
c1e5689f6a
commit
9b42c8cf57
@@ -392,7 +392,7 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
|
||||
|
||||
/**
|
||||
* {@link EventEntityManagerHolder} bean.
|
||||
*
|
||||
*
|
||||
* @return a new {@link EventEntityManagerHolder}
|
||||
*/
|
||||
@Bean
|
||||
@@ -403,7 +403,7 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
|
||||
|
||||
/**
|
||||
* {@link EventEntityManager} bean.
|
||||
*
|
||||
*
|
||||
* @param aware
|
||||
* the tenant aware
|
||||
* @param entityManager
|
||||
@@ -458,5 +458,4 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration {
|
||||
final AutoAssignChecker autoAssignChecker) {
|
||||
return new AutoAssignScheduler(tenantAware, systemManagement, systemSecurityContext, autoAssignChecker);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import org.eclipse.hawkbit.repository.model.SoftwareModule;
|
||||
import org.eclipse.hawkbit.repository.test.util.HashGeneratorUtils;
|
||||
import org.eclipse.hawkbit.repository.test.util.WithUser;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
|
||||
@@ -46,9 +45,6 @@ import ru.yandex.qatools.allure.annotations.Stories;
|
||||
@Features("Component Tests - Repository")
|
||||
@Stories("Artifact Management")
|
||||
public class ArtifactManagementTest extends AbstractJpaIntegrationTestWithMongoDB {
|
||||
public ArtifactManagementTest() {
|
||||
LOG = LoggerFactory.getLogger(ArtifactManagementTest.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for
|
||||
|
||||
@@ -32,7 +32,6 @@ import org.eclipse.hawkbit.repository.model.TargetTag;
|
||||
import org.eclipse.hawkbit.repository.model.TargetTagAssignmentResult;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
|
||||
@@ -47,9 +46,6 @@ import ru.yandex.qatools.allure.annotations.Stories;
|
||||
@Features("Component Tests - Repository")
|
||||
@Stories("Tag Management")
|
||||
public class TagManagementTest extends AbstractJpaIntegrationTest {
|
||||
public TagManagementTest() {
|
||||
LOG = LoggerFactory.getLogger(TagManagementTest.class);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.jpa;
|
||||
|
||||
import static com.google.common.collect.Iterables.limit;
|
||||
import static com.google.common.collect.Iterables.toArray;
|
||||
import static com.google.common.collect.Lists.newArrayList;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static org.fest.assertions.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -29,6 +33,14 @@ import javax.persistence.Query;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
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.entity.ActionCreatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.ActionUpdatedEvent;
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent;
|
||||
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.TenantNotExistException;
|
||||
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
|
||||
@@ -43,6 +55,8 @@ import org.eclipse.hawkbit.repository.model.Tag;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.eclipse.hawkbit.repository.model.TargetIdName;
|
||||
import org.eclipse.hawkbit.repository.model.TargetTag;
|
||||
import org.eclipse.hawkbit.repository.test.matcher.Expect;
|
||||
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
|
||||
import org.eclipse.hawkbit.repository.test.util.WithSpringAuthorityRule;
|
||||
import org.eclipse.hawkbit.repository.test.util.WithUser;
|
||||
import org.junit.Test;
|
||||
@@ -60,6 +74,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Ensures that retrieving the target security is only permitted with the necessary permissions.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
public void getTargetSecurityTokenOnlyWithCorrectPermission() throws Exception {
|
||||
final Target createdTarget = targetManagement.createTarget(new JpaTarget("targetWithSecurityToken", "token"));
|
||||
|
||||
@@ -85,12 +100,12 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
assertThat(securityTokenAsSystemCode).isNotNull();
|
||||
|
||||
assertThat(securityTokenWithoutPermission).isNull();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Ensures that targets cannot be created e.g. in plug'n play scenarios when tenant does not exists.")
|
||||
@WithUser(tenantId = "tenantWhichDoesNotExists", allSpPermissions = true, autoCreateTenant = false)
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
public void createTargetForTenantWhichDoesNotExistThrowsTenantNotExistException() {
|
||||
try {
|
||||
targetManagement.createTarget(new JpaTarget("targetId123"));
|
||||
@@ -102,6 +117,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Verify that a target with empty controller id cannot be created")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 0) })
|
||||
public void createTargetWithNoControllerId() {
|
||||
try {
|
||||
targetManagement.createTarget(new JpaTarget(""));
|
||||
@@ -120,6 +136,9 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Ensures that targets can assigned and unassigned to a target tag. Not exists target will be ignored for the assignment.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 4),
|
||||
@Expect(type = TargetTagCreatedEvent.class, count = 1),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 8) })
|
||||
public void assignAndUnassignTargetsToTag() {
|
||||
final List<String> assignTarget = new ArrayList<String>();
|
||||
assignTarget.add(targetManagement.createTarget(new JpaTarget("targetId123")).getControllerId());
|
||||
@@ -156,6 +175,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Ensures that targets can deleted e.g. test all cascades")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 12),
|
||||
@Expect(type = TargetDeletedEvent.class, count = 12), @Expect(type = TargetUpdatedEvent.class, count = 6) })
|
||||
public void deleteAndCreateTargets() {
|
||||
Target target = targetManagement.createTarget(new JpaTarget("targetId123"));
|
||||
assertThat(targetManagement.countTargetsAll()).as("target count is wrong").isEqualTo(1);
|
||||
@@ -167,7 +188,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
targetManagement.deleteTargets(target.getId());
|
||||
assertThat(targetManagement.countTargetsAll()).as("target count is wrong").isEqualTo(0);
|
||||
|
||||
final List<Long> targets = new ArrayList<Long>();
|
||||
final List<Long> targets = new ArrayList<>();
|
||||
for (int i = 0; i < 5; i++) {
|
||||
target = targetManagement.createTarget(new JpaTarget("" + i));
|
||||
targets.add(target.getId());
|
||||
@@ -194,6 +215,10 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Finds a target by given ID and checks if all data is in the reponse (including the data defined as lazy).")
|
||||
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 2),
|
||||
@Expect(type = TargetCreatedEvent.class, count = 1), @Expect(type = TargetUpdatedEvent.class, count = 5),
|
||||
@Expect(type = ActionCreatedEvent.class, count = 2), @Expect(type = ActionUpdatedEvent.class, count = 1),
|
||||
@Expect(type = TargetAssignDistributionSetEvent.class, count = 2) })
|
||||
public void findTargetByControllerIDWithDetails() {
|
||||
final DistributionSet set = testdataFactory.createDistributionSet("test");
|
||||
final DistributionSet set2 = testdataFactory.createDistributionSet("test2");
|
||||
@@ -236,7 +261,6 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
assertThat(target.getAssignedDistributionSet()).as("Assigned ds size is wrong").isEqualTo(set2);
|
||||
assertThat(target.getTargetInfo().getInstalledDistributionSet().getId()).as("Installed ds is wrong")
|
||||
.isEqualTo(set.getId());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -247,6 +271,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Checks if the EntityAlreadyExistsException is thrown if the targets with the same controller ID are created twice.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 5) })
|
||||
public void createMultipleTargetsDuplicate() {
|
||||
final List<Target> targets = testdataFactory.generateTargets(5, "mySimpleTargs", "my simple targets");
|
||||
targetManagement.createTargets(targets);
|
||||
@@ -260,6 +285,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Checks if the EntityAlreadyExistsException is thrown if a single target with the same controller ID are created twice.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1) })
|
||||
public void createTargetDuplicate() {
|
||||
targetManagement.createTarget(new JpaTarget("4711"));
|
||||
try {
|
||||
@@ -320,6 +346,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@WithUser(allSpPermissions = true)
|
||||
@Description("Creates and updates a target and verifies the changes in the repository.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 1),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 1) })
|
||||
public void singleTargetIsInsertedIntoRepo() throws Exception {
|
||||
|
||||
final String myCtrlID = "myCtrlID";
|
||||
@@ -358,6 +386,9 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
@Test
|
||||
@WithUser(allSpPermissions = true)
|
||||
@Description("Create multiple tragets as bulk operation and delete them in bulk.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 101),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 100),
|
||||
@Expect(type = TargetDeletedEvent.class, count = 51) })
|
||||
public void bulkTargetCreationAndDelete() throws Exception {
|
||||
final String myCtrlID = "myCtrlID";
|
||||
final List<Target> firstList = testdataFactory.generateTargets(100, myCtrlID, "first description");
|
||||
@@ -406,26 +437,19 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
targetManagement.deleteTargets(savedExtra.getId());
|
||||
|
||||
final int nr2Del = 50;
|
||||
int i = nr2Del;
|
||||
final Long[] deletedTargetIDs = new Long[nr2Del];
|
||||
final Target[] deletedTargets = new Target[nr2Del];
|
||||
final int numberToDelete = 50;
|
||||
final Iterable<Target> targetsToDelete = limit(firstSaved, numberToDelete);
|
||||
final Target[] deletedTargets = toArray(targetsToDelete, Target.class);
|
||||
final List<Long> targetsIdsToDelete = newArrayList(targetsToDelete.iterator()).stream().map(Target::getId)
|
||||
.collect(toList());
|
||||
|
||||
final Iterator<Target> it = firstSaved.iterator();
|
||||
while (nr2Del > 0 && it.hasNext() && i > 0) {
|
||||
final Target pt = it.next();
|
||||
deletedTargetIDs[i - 1] = pt.getId();
|
||||
deletedTargets[i - 1] = pt;
|
||||
i--;
|
||||
}
|
||||
targetManagement.deleteTargets(targetsIdsToDelete);
|
||||
|
||||
targetManagement.deleteTargets(deletedTargetIDs);
|
||||
final List<Target> targetsLeft = targetManagement.findTargetsAll(new PageRequest(0, 200)).getContent();
|
||||
assertThat(firstSaved.spliterator().getExactSizeIfKnown() - numberToDelete).as("Size of splited list")
|
||||
.isEqualTo(targetsLeft.spliterator().getExactSizeIfKnown());
|
||||
|
||||
final List<Target> found = targetManagement.findTargetsAll(new PageRequest(0, 200)).getContent();
|
||||
assertThat(firstSaved.spliterator().getExactSizeIfKnown() - nr2Del).as("Size of splited list")
|
||||
.isEqualTo(found.spliterator().getExactSizeIfKnown());
|
||||
|
||||
assertThat(found).as("Not all undeleted found").doesNotContain(deletedTargets);
|
||||
assertThat(targetsLeft).as("Not all undeleted found").doesNotContain(deletedTargets);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -540,6 +564,8 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the assigment of tags to the a single target.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 2),
|
||||
@Expect(type = TargetTagCreatedEvent.class, count = 7) })
|
||||
public void targetTagAssignment() {
|
||||
final Target t1 = testdataFactory.generateTarget("id-1", "blablub");
|
||||
final int noT2Tags = 4;
|
||||
@@ -570,6 +596,9 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the assigment of tags to multiple targets.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 50),
|
||||
@Expect(type = TargetTagCreatedEvent.class, count = 4),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 80) })
|
||||
public void targetTagBulkAssignments() {
|
||||
final List<Target> tagATargets = targetManagement
|
||||
.createTargets(testdataFactory.generateTargets(10, "tagATargets", "first description"));
|
||||
@@ -641,6 +670,9 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Tests the unassigment of tags to multiple targets.")
|
||||
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 3),
|
||||
@Expect(type = TargetCreatedEvent.class, count = 109),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 227) })
|
||||
public void targetTagBulkUnassignments() {
|
||||
final TargetTag targTagA = tagManagement.createTargetTag(new JpaTargetTag("Targ-A-Tag"));
|
||||
final TargetTag targTagB = tagManagement.createTargetTag(new JpaTargetTag("Targ-B-Tag"));
|
||||
@@ -689,7 +721,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
targetManagement.toggleTagAssignment(targBCs, targTagC);
|
||||
targetManagement.toggleTagAssignment(targABCs, targTagC);
|
||||
|
||||
checkTargetHasTags(true, targAs, targTagA);
|
||||
checkTargetHasTags(true, targAs, targTagA); // 0
|
||||
checkTargetHasTags(true, targBs, targTagB);
|
||||
checkTargetHasTags(true, targABs, targTagA, targTagB);
|
||||
checkTargetHasTags(true, targBCs, targTagB);
|
||||
@@ -699,11 +731,13 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
checkTargetHasNotTags(targACs, targTagC);
|
||||
checkTargetHasNotTags(targBCs, targTagC);
|
||||
checkTargetHasNotTags(targABCs, targTagC);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Description("Retrieves targets by ID with lazy loading of the tags. Checks the successfull load.")
|
||||
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 1),
|
||||
@Expect(type = TargetCreatedEvent.class, count = 25),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 25) })
|
||||
public void findTargetsByControllerIDsWithTags() {
|
||||
final TargetTag targTagA = tagManagement.createTargetTag(new JpaTargetTag("Targ-A-Tag"));
|
||||
|
||||
@@ -726,6 +760,7 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test the optimized quere for retrieving all ID/name pairs of targets.")
|
||||
@ExpectEvents({ @Expect(type = TargetCreatedEvent.class, count = 25) })
|
||||
public void findAllTargetIdNamePaiss() {
|
||||
final List<Target> targAs = targetManagement
|
||||
.createTargets(testdataFactory.generateTargets(25, "target-id-A", "first description"));
|
||||
@@ -741,6 +776,9 @@ public class TargetManagementTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Description("Test that NO TAG functionality which gives all targets with no tag assigned.")
|
||||
@ExpectEvents({ @Expect(type = TargetTagCreatedEvent.class, count = 1),
|
||||
@Expect(type = TargetCreatedEvent.class, count = 50),
|
||||
@Expect(type = TargetUpdatedEvent.class, count = 25) })
|
||||
public void findTargetsWithNoTag() {
|
||||
|
||||
final TargetTag targTagA = tagManagement.createTargetTag(new JpaTargetTag("Targ-A-Tag"));
|
||||
@@ -762,6 +800,7 @@ 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)
|
||||
public void targetCanBeReadWithOnlyReadTargetPermission() throws Exception {
|
||||
final String knownTargetControllerId = "readTarget";
|
||||
controllerManagament.findOrRegisterTargetIfItDoesNotexist(knownTargetControllerId, new URI("http://127.0.0.1"));
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.eclipse.hawkbit.repository.jpa.model.helper.EntityInterceptorHolder;
|
||||
import org.eclipse.hawkbit.repository.model.EntityInterceptor;
|
||||
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
|
||||
import org.eclipse.hawkbit.repository.model.Target;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import ru.yandex.qatools.allure.annotations.Description;
|
||||
@@ -28,10 +29,9 @@ import ru.yandex.qatools.allure.annotations.Stories;
|
||||
@Stories("Entity Listener Interceptor")
|
||||
public class EntityInterceptorListenerTest extends AbstractJpaIntegrationTest {
|
||||
|
||||
@Override
|
||||
public void after() {
|
||||
@After
|
||||
public void tearDown() {
|
||||
EntityInterceptorHolder.getInstance().getEntityInterceptors().clear();
|
||||
super.after();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user