add tests for repository entity events

Signed-off-by: Michael Hirsch <michael.hirsch@bosch-si.com>
This commit is contained in:
Michael Hirsch
2016-08-01 15:14:40 +02:00
parent 4e237f748b
commit 9c442967f8
6 changed files with 237 additions and 67 deletions

View File

@@ -11,27 +11,26 @@ package org.eclipse.hawkbit.repository.eventbus.event;
import org.eclipse.hawkbit.eventbus.event.AbstractDistributedEvent;
import org.eclipse.hawkbit.repository.model.DistributionSet;
/**
* * Defines the {@link AbstractDistributedEvent} for deletion of {@link DistributionSet}.
*
* Defines the {@link AbstractDistributedEvent} for deletion of
* {@link DistributionSet}.
*/
public class DistributionDeletedEvent extends AbstractDistributedEvent{
private static final long serialVersionUID = -3308850381757843098L;
final Long[] distributionSetIDs;
public class DistributionDeletedEvent extends AbstractDistributedEvent {
private static final long serialVersionUID = -3308850381757843098L;
private final Long distributionId;
/**
* @param tenant
* the tenant for this event
* @param distributionSetId
* the ID of the target which has been deleted
* the ID of the distribution set which has been deleted
*/
public DistributionDeletedEvent(final String tenant, final Long...distributionIds) {
public DistributionDeletedEvent(final String tenant, final Long distributionId) {
super(-1, tenant);
this.distributionSetIDs = distributionIds;
this.distributionId = distributionId;
}
public Long[] getDistributionSetIDs() {
return distributionSetIDs;
}
public Long getDistributionSetId() {
return distributionId;
}
}

View File

@@ -28,6 +28,7 @@ import org.eclipse.hawkbit.repository.DistributionSetMetadataFields;
import org.eclipse.hawkbit.repository.DistributionSetTypeFields;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.eclipse.hawkbit.repository.TagManagement;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionSetTagAssigmentResultEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityLockedException;
@@ -54,6 +55,7 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSetTagAssignmentResult;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
@@ -102,6 +104,9 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Autowired
private AfterTransactionCommitExecutor afterCommit;
@Autowired
private TenantAware tenantAware;
@Override
public DistributionSet findDistributionSetByIdWithDetails(final Long distid) {
return distributionSetRepository.findOne(DistributionSetSpecification.byId(distid));
@@ -193,6 +198,9 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
// handle the empty list
distributionSetRepository.deleteByIdIn(toHardDelete);
}
Arrays.stream(distributionSetIDs)
.forEach(dsId -> eventBus.post(new DistributionDeletedEvent(tenantAware.getCurrentTenant(), dsId)));
}
@Override

View File

@@ -29,6 +29,7 @@ import javax.persistence.criteria.Root;
import org.eclipse.hawkbit.repository.TargetFields;
import org.eclipse.hawkbit.repository.TargetManagement;
import org.eclipse.hawkbit.repository.eventbus.event.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetTagAssigmentResultEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
@@ -48,6 +49,7 @@ import org.eclipse.hawkbit.repository.model.TargetIdName;
import org.eclipse.hawkbit.repository.model.TargetTag;
import org.eclipse.hawkbit.repository.model.TargetTagAssignmentResult;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
@@ -94,6 +96,9 @@ public class JpaTargetManagement implements TargetManagement {
@Autowired
private EventBus eventBus;
@Autowired
private TenantAware tenantAware;
@Autowired
private AfterTransactionCommitExecutor afterCommit;
@@ -206,6 +211,8 @@ public class JpaTargetManagement implements TargetManagement {
targetInfoRepository.deleteByTargetIdIn(targetsForCurrentTenant);
targetRepository.deleteByIdIn(targetsForCurrentTenant);
}
targetsForCurrentTenant
.forEach(targetId -> eventBus.post(new TargetDeletedEvent(tenantAware.getCurrentTenant(), targetId)));
}
@Override

View File

@@ -0,0 +1,161 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.hawkbit.repository.jpa.eventbus;
import static org.fest.assertions.Assertions.assertThat;
import java.net.URI;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import org.eclipse.hawkbit.eventbus.event.Event;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.DistributionDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetCreatedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetInfoUpdateEvent;
import org.eclipse.hawkbit.repository.eventbus.event.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
import org.fest.assertions.api.Assertions;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import ru.yandex.qatools.allure.annotations.Description;
import ru.yandex.qatools.allure.annotations.Features;
import ru.yandex.qatools.allure.annotations.Stories;
@Features("Component Tests - Repository")
@Stories("Entity Events")
public class RepositoryEntityEventTest extends AbstractJpaIntegrationTest {
@Autowired
private EventBus eventBus;
private final MyEventListener eventListener = new MyEventListener();
@Before
public void beforeTest() {
eventListener.queue.clear();
eventBus.register(eventListener);
}
@Test
@Description("Verifies that the target created event is published when a target has been created")
public void targetCreatedEventIsPublished() throws InterruptedException {
final Target createdTarget = targetManagement.createTarget(entityFactory.generateTarget("12345"));
final TargetCreatedEvent targetCreatedEvent = eventListener.waitForEvent(TargetCreatedEvent.class, 1,
TimeUnit.SECONDS);
assertThat(targetCreatedEvent).isNotNull();
assertThat(targetCreatedEvent.getEntity().getId()).isEqualTo(createdTarget.getId());
}
@Test
@Description("Verifies that the target update event is published when a target has been updated")
public void targetUpdateEventIsPublished() throws InterruptedException {
final Target createdTarget = targetManagement.createTarget(entityFactory.generateTarget("12345"));
createdTarget.setName("updateName");
targetManagement.updateTarget(createdTarget);
final TargetUpdatedEvent targetUpdatedEvent = eventListener.waitForEvent(TargetUpdatedEvent.class, 1,
TimeUnit.SECONDS);
assertThat(targetUpdatedEvent).isNotNull();
assertThat(targetUpdatedEvent.getEntity().getId()).isEqualTo(createdTarget.getId());
}
@Test
@Description("Verifies that the target info update event is published when a target info has been updated")
public void targetInfoUpdateEventIsPublished() throws InterruptedException {
final Target createdTarget = targetManagement.createTarget(entityFactory.generateTarget("12345"));
controllerManagament.updateTargetStatus(createdTarget.getTargetInfo(), TargetUpdateStatus.PENDING,
System.currentTimeMillis(), URI.create("http://127.0.0.1"));
final TargetInfoUpdateEvent targetInfoUpdatedEvent = eventListener.waitForEvent(TargetInfoUpdateEvent.class, 1,
TimeUnit.SECONDS);
assertThat(targetInfoUpdatedEvent).isNotNull();
assertThat(targetInfoUpdatedEvent.getEntity().getTarget().getId()).isEqualTo(createdTarget.getId());
}
@Test
@Description("Verifies that the target deleted event is published when a target has been deleted")
public void targetDeletedEventIsPublished() throws InterruptedException {
final Target createdTarget = targetManagement.createTarget(entityFactory.generateTarget("12345"));
targetManagement.deleteTargets(createdTarget.getId());
final TargetDeletedEvent targetDeletedEvent = eventListener.waitForEvent(TargetDeletedEvent.class, 1,
TimeUnit.SECONDS);
assertThat(targetDeletedEvent).isNotNull();
assertThat(targetDeletedEvent.getTargetId()).isEqualTo(createdTarget.getId());
}
@Test
@Description("Verifies that the distribution set created event is published when a distribution set has been created")
public void distributionSetCreatedEventIsPublished() throws InterruptedException {
final DistributionSet generateDistributionSet = entityFactory.generateDistributionSet();
generateDistributionSet.setName("dsEventTest");
generateDistributionSet.setVersion("1");
final DistributionSet createDistributionSet = distributionSetManagement
.createDistributionSet(generateDistributionSet);
final DistributionCreatedEvent dsCreatedEvent = eventListener.waitForEvent(DistributionCreatedEvent.class, 1,
TimeUnit.SECONDS);
assertThat(dsCreatedEvent).isNotNull();
assertThat(dsCreatedEvent.getEntity().getId()).isEqualTo(createDistributionSet.getId());
}
@Test
@Description("Verifies that the distribution set deleted event is published when a distribution set has been deleted")
public void distributionSetDeletedEventIsPublished() throws InterruptedException {
final DistributionSet generateDistributionSet = entityFactory.generateDistributionSet();
generateDistributionSet.setName("dsEventTest");
generateDistributionSet.setVersion("1");
final DistributionSet createDistributionSet = distributionSetManagement
.createDistributionSet(generateDistributionSet);
distributionSetManagement.deleteDistributionSet(createDistributionSet);
final DistributionDeletedEvent dsDeletedEvent = eventListener.waitForEvent(DistributionDeletedEvent.class, 1,
TimeUnit.SECONDS);
assertThat(dsDeletedEvent).isNotNull();
assertThat(dsDeletedEvent.getDistributionSetId()).isEqualTo(createDistributionSet.getId());
}
private class MyEventListener {
private final BlockingQueue<Event> queue = new LinkedBlockingQueue<>();
@Subscribe
public void onEvent(final Event event) {
queue.offer(event);
}
public <T> T waitForEvent(final Class<T> eventType, final long timeout, final TimeUnit timeUnit)
throws InterruptedException {
Event event = null;
while ((event = queue.poll(timeout, timeUnit)) != null) {
if (event.getClass().isAssignableFrom(eventType)) {
return (T) event;
}
}
Assertions.fail("Missing event " + eventType + " within timeout " + timeout + " " + timeUnit);
return null;
}
}
}

View File

@@ -108,7 +108,7 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
@Autowired
private DsMetadataPopupLayout dsMetadataPopupLayout;
/**
* Initialize the component.
*/
@@ -130,8 +130,8 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
@EventBusListenerMethod(scope = EventScope.SESSION)
void onEvents(final DistributionSetUpdateEvent event) {
final DistributionSet ds = event.getEntity();
final DistributionSetIdName lastSelectedDsIdName = manageDistUIState.getLastSelectedDistribution().isPresent() ? manageDistUIState
.getLastSelectedDistribution().get() : null;
final DistributionSetIdName lastSelectedDsIdName = manageDistUIState.getLastSelectedDistribution().isPresent()
? manageDistUIState.getLastSelectedDistribution().get() : null;
final List<DistributionSetIdName> visibleItemIds = (List<DistributionSetIdName>) getVisibleItemIds();
// refresh the details tabs only if selected ds is updated
@@ -219,10 +219,10 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
protected void publishEntityAfterValueChange(final DistributionSet distributionSet) {
eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.SELECTED_ENTITY, distributionSet));
eventBus.publish(this, DistributionsUIEvent.ORDER_BY_DISTRIBUTION);
if(distributionSet!=null){
manageDistUIState.setLastSelectedEntity(new DistributionSetIdName(distributionSet.getId(),
distributionSet.getName(),distributionSet.getVersion()));
}
if (distributionSet != null) {
manageDistUIState.setLastSelectedEntity(new DistributionSetIdName(distributionSet.getId(),
distributionSet.getName(), distributionSet.getVersion()));
}
}
@Override
@@ -515,7 +515,7 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
manageDistUIState.setNoDataAvailableDist(!available);
}
@Override
protected void addCustomGeneratedColumns() {
addGeneratedColumn(SPUILabelDefinitions.METADATA_ICON, new ColumnGenerator() {
@@ -525,12 +525,12 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
public Object generateCell(final Table source, final Object itemId, final Object columnId) {
final String nameVersionStr = getNameAndVerion(itemId);
final Button manageMetaDataBtn = createManageMetadataButton(nameVersionStr);
manageMetaDataBtn.addClickListener(event -> showMetadataDetails(((DistributionSetIdName) itemId).getId()));
manageMetaDataBtn
.addClickListener(event -> showMetadataDetails(((DistributionSetIdName) itemId).getId()));
return manageMetaDataBtn;
}
});
}
@Override
protected List<TableColumn> getTableVisibleColumns() {
@@ -541,7 +541,7 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
return columnList;
}
private Button createManageMetadataButton(String nameVersionStr) {
private Button createManageMetadataButton(final String nameVersionStr) {
final Button manageMetadataBtn = SPUIComponentProvider.getButton(
SPUIComponentIdProvider.DS_TABLE_MANAGE_METADATA_ID + "." + nameVersionStr, "", "", null, false,
FontAwesome.LIST_ALT, SPUIButtonStyleSmallNoBorder.class);
@@ -550,9 +550,9 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
return manageMetadataBtn;
}
private void showMetadataDetails(Long itemId) {
DistributionSet ds = distributionSetManagement.findDistributionSetByIdWithDetails(itemId);
UI.getCurrent().addWindow(dsMetadataPopupLayout.getWindow(ds,null));
private void showMetadataDetails(final Long itemId) {
final DistributionSet ds = distributionSetManagement.findDistributionSetByIdWithDetails(itemId);
UI.getCurrent().addWindow(dsMetadataPopupLayout.getWindow(ds, null));
}
private String getNameAndVerion(final Object itemId) {
@@ -580,24 +580,22 @@ public class DistributionSetTable extends AbstractNamedVersionTable<Distribution
}
private void updateDistributionInTable(final DistributionSet editedDs) {
final Item item = getContainerDataSource().getItem(
new DistributionSetIdName(editedDs.getId(), editedDs.getName(), editedDs.getVersion()));
final Item item = getContainerDataSource()
.getItem(new DistributionSetIdName(editedDs.getId(), editedDs.getName(), editedDs.getVersion()));
updateEntity(editedDs, item);
}
private void onDistributionDeleteEvent(List<DistributionDeletedEvent> events) {
private void onDistributionDeleteEvent(final List<DistributionDeletedEvent> events) {
final LazyQueryContainer dsContainer = (LazyQueryContainer) getContainerDataSource();
final List<Object> visibleItemIds = (List<Object>) getVisibleItemIds();
boolean shouldRefreshDs = false;
for (final DistributionDeletedEvent deletedEvent : events) {
Long[] distributionSetIDs = deletedEvent.getDistributionSetIDs();
for (Long dsId : distributionSetIDs) {
final DistributionSetIdName targetIdName = new DistributionSetIdName(dsId, null, null);
if (visibleItemIds.contains(targetIdName)) {
dsContainer.removeItem(targetIdName);
} else {
shouldRefreshDs = true;
}
final Long distributionSetId = deletedEvent.getDistributionSetId();
final DistributionSetIdName targetIdName = new DistributionSetIdName(distributionSetId, null, null);
if (visibleItemIds.contains(targetIdName)) {
dsContainer.removeItem(targetIdName);
} else {
shouldRefreshDs = true;
}
}

View File

@@ -93,16 +93,16 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
@Autowired
private ManagementViewAcceptCriteria managementViewAcceptCriteria;
@Autowired
private transient TargetManagement targetService;
@Autowired
private DsMetadataPopupLayout dsMetadataPopupLayout;
@Autowired
private transient DistributionSetManagement distributionSetManagement;
@Autowired
private EntityFactory entityFactory;
@@ -125,8 +125,8 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
onDistributionDeleteEvent((List<DistributionDeletedEvent>) events);
} else if (DistributionCreatedEvent.class.isInstance(firstEvent)
&& ((DistributionCreatedEvent) firstEvent).getEntity().isComplete()) {
refreshDistributions();
}
refreshDistributions();
}
}
@@ -137,12 +137,12 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
final List<DistributionSetIdName> visibleItemIds = (List<DistributionSetIdName>) getVisibleItemIds();
// refresh the details tabs only if selected ds is updated
// refresh the details tabs only if selected ds is updated
// refresh the details tabs only if selected ds is updated
if (lastSelectedDsIdName != null && lastSelectedDsIdName.getId().equals(ds.getId())) {
// update table row+details layout
eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.UPDATED_ENTITY, ds));
} else if (visibleItemIds.stream().filter(e -> e.getId().equals(ds.getId())).findFirst().isPresent()) {
//update the name/version details visible in table
// update the name/version details visible in table
UI.getCurrent().access(() -> updateDistributionInTable(event.getEntity()));
}
}
@@ -261,27 +261,26 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
@Override
public Object generateCell(final Table source, final Object itemId, final Object columnId) {
HorizontalLayout iconLayout = new HorizontalLayout();
final HorizontalLayout iconLayout = new HorizontalLayout();
final String nameVersionStr = getNameAndVerion(itemId);
final Button manageMetaDataBtn = createManageMetadataButton(nameVersionStr);
manageMetaDataBtn.addClickListener(event -> showMetadataDetails(itemId));
iconLayout.addComponent((Button)getPinButton(itemId));
iconLayout.addComponent((Button) getPinButton(itemId));
iconLayout.addComponent(manageMetaDataBtn);
return iconLayout;
}
});
}
private String getNameAndVerion(final Object itemId) {
final Item item = getItem(itemId);
final String name = (String) item.getItemProperty(SPUILabelDefinitions.VAR_NAME).getValue();
final String version = (String) item.getItemProperty(SPUILabelDefinitions.VAR_VERSION).getValue();
return name + "." + version;
}
private Button createManageMetadataButton(String nameVersionStr) {
private Button createManageMetadataButton(final String nameVersionStr) {
final Button manageMetadataBtn = SPUIComponentProvider.getButton(
SPUIComponentIdProvider.DS_TABLE_MANAGE_METADATA_ID + "." + nameVersionStr, "", "", null, false,
FontAwesome.LIST_ALT, SPUIButtonStyleSmallNoBorder.class);
@@ -313,9 +312,9 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
@Override
protected void publishEntityAfterValueChange(final DistributionSet selectedLastEntity) {
eventBus.publish(this, new DistributionTableEvent(BaseEntityEventType.SELECTED_ENTITY, selectedLastEntity));
if(selectedLastEntity!=null){
managementUIState.setLastSelectedDistribution(new DistributionSetIdName(selectedLastEntity.getId(),
selectedLastEntity.getName(),selectedLastEntity.getVersion()));
if (selectedLastEntity != null) {
managementUIState.setLastSelectedDistribution(new DistributionSetIdName(selectedLastEntity.getId(),
selectedLastEntity.getName(), selectedLastEntity.getVersion()));
}
}
@@ -728,27 +727,25 @@ public class DistributionTable extends AbstractNamedVersionTable<DistributionSet
managementUIState.setNoDataAvailableDistribution(!available);
}
private void showMetadataDetails(Object itemId) {
private void showMetadataDetails(final Object itemId) {
final DistributionSetIdName distIdName = (DistributionSetIdName) getContainerDataSource().getItem(itemId)
.getItemProperty(SPUILabelDefinitions.VAR_DIST_ID_NAME).getValue();
DistributionSet ds = distributionSetManagement.findDistributionSetByIdWithDetails(distIdName.getId());
final DistributionSet ds = distributionSetManagement.findDistributionSetByIdWithDetails(distIdName.getId());
UI.getCurrent().addWindow(dsMetadataPopupLayout.getWindow(ds, null));
}
private void onDistributionDeleteEvent(List<DistributionDeletedEvent> events) {
private void onDistributionDeleteEvent(final List<DistributionDeletedEvent> events) {
final LazyQueryContainer dsContainer = (LazyQueryContainer) getContainerDataSource();
final List<Object> visibleItemIds = (List<Object>) getVisibleItemIds();
boolean shouldRefreshDs = false;
for (final DistributionDeletedEvent deletedEvent : events) {
Long[] distributionSetIDs = deletedEvent.getDistributionSetIDs();
for (Long dsId : distributionSetIDs) {
final DistributionSetIdName targetIdName = new DistributionSetIdName(dsId, null, null);
if (visibleItemIds.contains(targetIdName)) {
dsContainer.removeItem(targetIdName);
} else {
shouldRefreshDs = true;
}
final Long distributionSetId = deletedEvent.getDistributionSetId();
final DistributionSetIdName targetIdName = new DistributionSetIdName(distributionSetId, null, null);
if (visibleItemIds.contains(targetIdName)) {
dsContainer.removeItem(targetIdName);
} else {
shouldRefreshDs = true;
}
}