未验证 提交 92e6216d 编写于 作者: D Davide Cerbo 提交者: GitHub

AAE-11508 produce the application rollback event when the application has been rolled back (#4193)

* add application rollback event

* AAE-11508 add a test
上级 107acb9e
......@@ -23,6 +23,7 @@ public interface ApplicationEvent extends
RuntimeEvent<Deployment, ApplicationEvents> {
enum ApplicationEvents {
APPLICATION_DEPLOYED
APPLICATION_DEPLOYED,
APPLICATION_ROLLBACK
}
}
......@@ -22,12 +22,19 @@ import org.activiti.api.process.model.events.ApplicationEvent;
public class ApplicationDeployedEventImpl extends RuntimeEventImpl<Deployment, ApplicationEvent.ApplicationEvents>
implements ApplicationDeployedEvent {
private final ApplicationEvents eventType;
public ApplicationDeployedEventImpl(Deployment entity) {
this(entity, ApplicationEvents.APPLICATION_DEPLOYED);
}
public ApplicationDeployedEventImpl(Deployment entity, ApplicationEvents eventType) {
super(entity);
this.eventType = eventType;
}
@Override
public ApplicationEvents getEventType() {
return ApplicationEvents.APPLICATION_DEPLOYED;
return eventType;
}
}
......@@ -15,24 +15,33 @@
*/
package org.activiti.spring;
import java.util.ArrayList;
import java.util.List;
import org.activiti.api.process.model.Deployment;
import java.util.stream.Collectors;
import org.activiti.api.process.model.events.ApplicationDeployedEvent;
import org.activiti.api.process.model.events.ApplicationEvent.ApplicationEvents;
import org.activiti.api.process.runtime.events.listener.ProcessRuntimeEventListener;
import org.activiti.api.runtime.event.impl.ApplicationDeployedEventImpl;
import org.activiti.api.runtime.event.impl.ApplicationDeployedEvents;
import org.activiti.engine.RepositoryService;
import org.activiti.runtime.api.model.impl.APIDeploymentConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
public class ApplicationDeployedEventProducer extends AbstractActivitiSmartLifeCycle {
protected static final Logger LOGGER = LoggerFactory.getLogger(ApplicationDeployedEventProducer.class);
private static final String APPLICATION_DEPLOYMENT_NAME= "SpringAutoDeployment";
private RepositoryService repositoryService;
private APIDeploymentConverter deploymentConverter;
private List<ProcessRuntimeEventListener<ApplicationDeployedEvent>> listeners;
private ApplicationEventPublisher eventPublisher;
private static final String APPLICATION_DEPLOYMENT_NAME= "SpringAutoDeployment";
@Value("${activiti.deploy.after-rollback:false}")
private boolean afterRollback;
public ApplicationDeployedEventProducer(RepositoryService repositoryService,
APIDeploymentConverter deploymentConverter,
......@@ -56,17 +65,29 @@ public class ApplicationDeployedEventProducer extends AbstractActivitiSmartLifeC
}
private List<ApplicationDeployedEvent> getApplicationDeployedEvents() {
List<Deployment> deployments = deploymentConverter.from(repositoryService
ApplicationEvents eventType = getEventType();
return deploymentConverter.from(repositoryService
.createDeploymentQuery()
.deploymentName(APPLICATION_DEPLOYMENT_NAME)
.list());
.list())
.stream()
.map(deployment -> new ApplicationDeployedEventImpl(deployment, eventType))
.collect(Collectors.toList());
}
List<ApplicationDeployedEvent> applicationDeployedEvents = new ArrayList<>();
for (Deployment deployment : deployments) {
ApplicationDeployedEventImpl applicationDeployedEvent = new ApplicationDeployedEventImpl(deployment);
applicationDeployedEvents.add(applicationDeployedEvent);
private ApplicationEvents getEventType() {
ApplicationEvents eventType;
if(afterRollback) {
LOGGER.info("This pod has been marked as created after a rollback.");
eventType = ApplicationEvents.APPLICATION_ROLLBACK;
} else {
eventType = ApplicationEvents.APPLICATION_DEPLOYED;
}
return applicationDeployedEvents;
return eventType;
}
public void setAfterRollback(boolean afterRollback) {
this.afterRollback = afterRollback;
}
@Override
......
......@@ -16,6 +16,8 @@
package org.activiti.spring;
import static java.util.Arrays.asList;
import static org.activiti.api.process.model.events.ApplicationEvent.ApplicationEvents.APPLICATION_DEPLOYED;
import static org.activiti.api.process.model.events.ApplicationEvent.ApplicationEvents.APPLICATION_ROLLBACK;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
......@@ -69,17 +71,31 @@ public class ApplicationDeployedEventProducerTest {
@Test
public void shouldPublishEventsWhenApplicationIsDeployed() {
ArgumentCaptor<ApplicationDeployedEvents> captorPublisher = startEventProducer();
assertThat(captorPublisher.getValue().getApplicationDeployedEvents().get(0).getEventType()).isEqualTo(APPLICATION_DEPLOYED);
}
@Test
public void shouldPublishEventsWhenApplicationIsRollback() {
producer.setAfterRollback(true);
ArgumentCaptor<ApplicationDeployedEvents> captorPublisher = startEventProducer();
assertThat(captorPublisher.getValue().getApplicationDeployedEvents().get(0).getEventType()).isEqualTo(APPLICATION_ROLLBACK);
}
private ArgumentCaptor<ApplicationDeployedEvents> startEventProducer() {
DeploymentQuery deploymentQuery = mock(DeploymentQuery.class);
given(repositoryService.createDeploymentQuery()).willReturn(deploymentQuery);
List<Deployment> internalDeployment = asList(mock(Deployment.class),
mock(Deployment.class));
mock(Deployment.class));
given(deploymentQuery.deploymentName(APPLICATION_DEPLOYMENT_NAME)).willReturn(deploymentQuery);
given(deploymentQuery.list()).willReturn(internalDeployment);
List<org.activiti.api.process.model.Deployment> apiDeployments= asList(
mock(org.activiti.api.process.model.Deployment.class));
mock(org.activiti.api.process.model.Deployment.class));
given(converter.from(internalDeployment)).willReturn(apiDeployments);
producer.start();
......@@ -90,12 +106,13 @@ public class ApplicationDeployedEventProducerTest {
List<ApplicationDeployedEvent> allValues = captor.getAllValues();
assertThat(allValues)
.extracting(ApplicationDeployedEvent::getEntity)
.hasSize(2)
.containsOnly(apiDeployments.get(0));
.extracting(ApplicationDeployedEvent::getEntity)
.hasSize(2)
.containsOnly(apiDeployments.get(0));
ArgumentCaptor<ApplicationDeployedEvents> captorPublisher = ArgumentCaptor.forClass(ApplicationDeployedEvents.class);
verify(eventPublisher).publishEvent(captorPublisher.capture());
return captorPublisher;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册