Skip to content
This repository has been archived by the owner on May 18, 2020. It is now read-only.

Commit

Permalink
fix: use ProcessRuntime wrapper to start process instance
Browse files Browse the repository at this point in the history
  • Loading branch information
igdianov committed Sep 10, 2019
1 parent 1804ef5 commit 9429790
Showing 1 changed file with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand All @@ -12,15 +15,22 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.StartProcessPayloadBuilder;
import org.activiti.api.process.model.payloads.StartProcessPayload;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.security.SecurityManager;
import org.activiti.cloud.api.process.model.IntegrationRequest;
import org.activiti.cloud.api.process.model.IntegrationResult;
import org.activiti.cloud.api.process.model.impl.IntegrationResultImpl;
import org.activiti.core.common.spring.identity.ExtendedInMemoryUserDetailsManager;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.awaitility.Awaitility;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
Expand All @@ -33,6 +43,14 @@
import org.springframework.messaging.Message;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
Expand All @@ -45,9 +63,15 @@ public class RuntimeBundleApplicationTest {
private static final String CONNECTOR_PROCESS = "ConnectorProcess";
private static final String BUSINESS_KEY = "businessKey";

@Autowired
private ProcessRuntime processRuntime;

@Autowired
private RuntimeService runtimeService;

@Autowired
private TestSecurityUtil securityUtil;

private static CountDownLatch exampleConnectorConsumer;

@Before
Expand All @@ -58,13 +82,15 @@ public void setUp() {
@Test
public void testExampleConnectorProcess() throws InterruptedException {
// given
securityUtil.logInAs("user");

String businessKey = BUSINESS_KEY;
StartProcessPayload payload = new StartProcessPayloadBuilder().withProcessDefinitionKey(CONNECTOR_PROCESS)
.withBusinessKey(businessKey)
.build();

// when
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
.processDefinitionKey(CONNECTOR_PROCESS)
.businessKey(businessKey)
.start();
ProcessInstance processInstance = processRuntime.start(payload);
// then
assertThat(processInstance).as("Should start process instance")
.isNotNull();
Expand Down Expand Up @@ -130,8 +156,106 @@ public TestIntegrationResultSender testIntegrationResultSender() {
return new TestIntegrationResultSender();
}

@Bean
public UserDetailsService myUserDetailsService() {
ExtendedInMemoryUserDetailsManager extendedInMemoryUserDetailsManager = new ExtendedInMemoryUserDetailsManager();

List<GrantedAuthority> userAuthorities = new ArrayList<>();
userAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER"));
userAuthorities.add(new SimpleGrantedAuthority("GROUP_activitiTeam"));

extendedInMemoryUserDetailsManager.createUser(new User("user",
"password",
userAuthorities));


List<GrantedAuthority> adminAuthorities = new ArrayList<>();
adminAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_ADMIN"));

extendedInMemoryUserDetailsManager.createUser(new User("admin",
"password",
adminAuthorities));

List<GrantedAuthority> garthAuthorities = new ArrayList<>();
garthAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER"));
garthAuthorities.add(new SimpleGrantedAuthority("GROUP_doctor"));

extendedInMemoryUserDetailsManager.createUser(new User("garth",
"password",
garthAuthorities));

//dean has role but no group
List<GrantedAuthority> deanAuthorities = new ArrayList<>();
deanAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER"));
extendedInMemoryUserDetailsManager.createUser(new User("dean",
"password",
deanAuthorities));

return extendedInMemoryUserDetailsManager;
}
}

@TestConfiguration
public static class TestSecurityUtil {

private Logger logger = LoggerFactory.getLogger(TestSecurityUtil.class);

@Autowired
private UserDetailsService userDetailsService;

@Autowired
private SecurityManager securityManager;

public void logInAs(String username) {

UserDetails user = userDetailsService.loadUserByUsername(username);
if (user == null) {
throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user");
}
logger.info("> Logged in as: " + username);
SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() {
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return user.getAuthorities();
}

@Override
public Object getCredentials() {
return user.getPassword();
}

@Override
public Object getDetails() {
return user;
}

@Override
public Object getPrincipal() {
return user;
}

@Override
public boolean isAuthenticated() {
return true;
}

@Override
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {

}

@Override
public String getName() {
return user.getUsername();
}
}));
org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username);

assertThat(securityManager.getAuthenticatedUserId()).isEqualTo(username);
}
}


public static class TestIntegrationResultSender {

@Autowired
Expand Down

0 comments on commit 9429790

Please sign in to comment.