Feature dmf target delete aware (#474)

* Implemented new function to create and send a thing created message if a
delete event is published

- added mock and integration tests 

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Switched target address form URI to String and fixed test

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Refactoring and removed TODOs

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Fixed javadoc and description

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Used Target from API instead from JpaTarget 

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Refactoring after review

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Small refactoring - fixed typos

Signed-off-by: Jonathan Philip Knoblauch <JonathanPhilip.Knoblauch@bosch-si.com>

* Resolve Merge conflicts

Signed-off-by: Melanie Retter <melanie.retter@bosch-si.com>
This commit is contained in:
Jonathan Knoblauch
2017-04-25 16:38:18 +02:00
committed by Michael Hirsch
parent a19364c635
commit 574fda1101
11 changed files with 222 additions and 51 deletions

View File

@@ -16,7 +16,9 @@ import org.eclipse.hawkbit.repository.model.Target;
*/
public class TargetDeletedEvent extends RemoteIdEvent {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
private String controllerId;
private String targetAddress;
/**
* Default constructor.
@@ -26,20 +28,33 @@ public class TargetDeletedEvent extends RemoteIdEvent {
}
/**
* Constructor for json serialization.
*
*
* @param tenant
* the tenant
* @param entityId
* the entity id
* @param controllerId
* the controllerId of the target
* @param targetAddress
* the target address
* @param entityClass
* the entity class
* @param applicationId
* the origin application id
*/
public TargetDeletedEvent(final String tenant, final Long entityId, final String entityClass,
final String applicationId) {
public TargetDeletedEvent(final String tenant, final Long entityId, final String controllerId,
final String targetAddress, final String entityClass, final String applicationId) {
super(entityId, tenant, entityClass, applicationId);
this.controllerId = controllerId;
this.targetAddress = targetAddress;
}
public String getControllerId() {
return controllerId;
}
public String getTargetAddress() {
return targetAddress;
}
}

View File

@@ -8,6 +8,7 @@
*/
package org.eclipse.hawkbit.repository.jpa;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -174,8 +175,10 @@ public class JpaTargetManagement implements TargetManagement {
targetRepository.deleteByIdIn(targetIDs);
targetIDs.forEach(targetId -> eventPublisher.publishEvent(new TargetDeletedEvent(tenantAware.getCurrentTenant(),
targetId, JpaTarget.class.getName(), applicationContext.getId())));
targets.forEach(target -> eventPublisher.publishEvent(
new TargetDeletedEvent(tenantAware.getCurrentTenant(), target.getId(), target.getControllerId(),
Optional.ofNullable(target.getAddress()).map(URI::toString).orElse(null),
JpaTarget.class.getName(), applicationContext.getId())));
}
@Override

View File

@@ -396,7 +396,8 @@ public class JpaTarget extends AbstractJpaNamedEntity implements Target, EventAw
@Override
public void fireDeleteEvent(final DescriptorEvent descriptorEvent) {
EventPublisherHolder.getInstance().getEventPublisher().publishEvent(new TargetDeletedEvent(getTenant(), getId(),
getClass().getName(), EventPublisherHolder.getInstance().getApplicationId()));
EventPublisherHolder.getInstance().getEventPublisher()
.publishEvent(new TargetDeletedEvent(getTenant(), getId(), getControllerId(), address,
getClass().getName(), EventPublisherHolder.getInstance().getApplicationId()));
}
}

View File

@@ -35,8 +35,12 @@ public class RemoteIdEventTest extends AbstractRemoteEventTest {
private static String NODE = "Node";
private static String CONTROLLER_ID = "controller911";
private static String ADDRESS = "amqp://anyhost";
@Test
@Description("Verifies that the is ds id correct reloaded")
@Description("Verifies that the ds id is correct reloaded")
public void testDistributionSetDeletedEvent() {
assertAndCreateRemoteEvent(DistributionSetDeletedEvent.class);
}
@@ -50,7 +54,9 @@ public class RemoteIdEventTest extends AbstractRemoteEventTest {
@Test
@Description("Verifies that the target id is correct reloaded")
public void testTargetDeletedEvent() {
assertAndCreateRemoteEvent(TargetDeletedEvent.class);
final TargetDeletedEvent deletedEvent = new TargetDeletedEvent(TENANT, ENTITY_ID, CONTROLLER_ID, ADDRESS,
ENTIY_CLASS, NODE);
assertEntity(deletedEvent);
}
@Test
@@ -85,23 +91,19 @@ public class RemoteIdEventTest extends AbstractRemoteEventTest {
}
}
protected RemoteIdEvent assertEntity(final RemoteIdEvent event) {
protected void assertEntity(final RemoteIdEvent event) {
assertThat(event.getEntityId()).isSameAs(ENTITY_ID);
RemoteIdEvent underTestCreatedEvent = (RemoteIdEvent) createProtoStuffEvent(event);
assertDeserializeEvent(underTestCreatedEvent);
final RemoteIdEvent protoStuffEvent = (RemoteIdEvent) createProtoStuffEvent(event);
assertDeserializeEvent(protoStuffEvent, event);
underTestCreatedEvent = (RemoteIdEvent) createJacksonEvent(event);
assertDeserializeEvent(underTestCreatedEvent);
return underTestCreatedEvent;
final RemoteIdEvent jacksonEvent = (RemoteIdEvent) createJacksonEvent(event);
assertDeserializeEvent(jacksonEvent, event);
}
private void assertDeserializeEvent(final RemoteIdEvent underTestCreatedEvent) {
assertThat(underTestCreatedEvent.getEntityId()).isEqualTo(ENTITY_ID);
assertThat(underTestCreatedEvent.getTenant()).isEqualTo(TENANT);
assertThat(underTestCreatedEvent.getEntityClass()).isEqualTo(ENTIY_CLASS);
assertThat(underTestCreatedEvent.getOriginService()).isEqualTo(NODE);
private void assertDeserializeEvent(final RemoteIdEvent underTestCreatedEvent, final RemoteIdEvent event) {
// gets added because events inherit from of java.util.EventObject
assertThat(underTestCreatedEvent).isEqualToIgnoringGivenFields(event, "source");
}
}