Commit b25bdb73 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

adding user doesn't work

parent 2a33def3
Pipeline #11822 failed with stage
in 12 minutes and 17 seconds
......@@ -25,8 +25,9 @@ import org.junit.runners.Suite.SuiteClasses;
SpringSecurityGeneralIntegrationTest.class,
SpringSecurityLdapIntegrationTest.class,
TaxonomiesControllerIntegrationTest.class,
UserControllerAnonymousIntegrationTest.class,
UserControllerIntegrationTest.class,
UserControllerAnonymousIntegrationTest.class
UserControllerIntegrationTestWithoutTransaction.class
})
public class AllIntegrationTests {
......
......@@ -3,9 +3,13 @@ package lcsb.mapviewer.web;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import java.awt.geom.Point2D;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;
import java.util.function.Supplier;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.servlet.Filter;
import org.apache.logging.log4j.core.LogEvent;
......@@ -32,8 +36,10 @@ import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.GenericProtein;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.DbUtils;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao;
import lcsb.mapviewer.persist.dao.user.UserDao;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.web.config.SpringWebConfig;
......@@ -53,22 +59,35 @@ abstract public class ControllerIntegrationTest {
@Autowired
private ProjectDao projectDao;
@Autowired
private UserDao userDao;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private UploadedFileEntryDao fileDao;
@Autowired
private DbUtils dbUtils;
protected MockMvc mockMvc;
private MinervaLoggerAppender appender;
private ExecutorService executorService;
@PostConstruct
public void construct() {
mockMvc = MockMvcBuilders.webAppContextSetup(context)
.addFilter(springSecurityFilterChain)
.build();
executorService = Executors.newFixedThreadPool(1);
}
@PreDestroy
public void tearDown() throws Exception {
executorService.shutdown();
}
/**
......@@ -152,7 +171,6 @@ abstract public class ControllerIntegrationTest {
return user;
}
protected Project createProject(String projectId) {
Project project = new Project(projectId);
ModelData map = new ModelData();
......@@ -190,5 +208,35 @@ abstract public class ControllerIntegrationTest {
return file;
}
/**
* Executes lambda function in new thread with separate hibernate session. The
* function returns type <T> which can be void.
*
* @param fun
* lambda to be executed in separate thread
* @return
* @throws Exception
*/
protected <T> T callInSeparateThread(Supplier<T> fun) throws Exception {
List<Future<T>> elements = executorService.invokeAll(Arrays.asList(new Callable<T>() {
@Override
public T call() throws Exception {
try {
dbUtils.createSessionForCurrentThread();
return fun.get();
} finally {
dbUtils.closeSessionForCurrentThread();
}
}
}));
return elements.get(0).get();
}
protected void removeUserInSeparateThread(User user) throws Exception {
callInSeparateThread(() -> {
userDao.delete(userDao.getById(user.getId()));
return null;
});
}
}
......@@ -40,8 +40,6 @@ import lcsb.mapviewer.services.interfaces.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerIntegrationTest {
private ExecutorService executorService;
private static final String TEST_PROJECT = "test_project";
Logger logger = LogManager.getLogger();
......@@ -64,12 +62,8 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt
@Autowired
private IUserService userService;
@Autowired
private DbUtils dbUtils;
@Before
public void setup() throws Exception {
executorService = Executors.newFixedThreadPool(1);
}
@After
......@@ -78,7 +72,6 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt
if (project != null) {
removeProjectInSeparateThread(project);
}
executorService.shutdown();
}
@Test(timeout = 10000)
......@@ -416,13 +409,6 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt
});
}
private void removeUserInSeparateThread(User user) throws Exception {
callInSeparateThread(() -> {
userDao.delete(userDao.getById(user.getId()));
return null;
});
}
private void removeProjectInSeparateThread(Project project) throws Exception {
removeProjectInSeparateThread(project.getProjectId());
}
......@@ -437,27 +423,4 @@ public class ProjectControllerIntegrationTestForAsyncCalls extends ControllerInt
});
}
/**
* Executes lambda function in new thread with separate hibernate session. The
* function returns type <T> which can be void.
*
* @param fun
* lambda to be executed in separate thread
* @return
* @throws Exception
*/
private <T> T callInSeparateThread(Supplier<T> fun) throws Exception {
List<Future<T>> elements = executorService.invokeAll(Arrays.asList(new Callable<T>() {
@Override
public T call() throws Exception {
try {
dbUtils.createSessionForCurrentThread();
return fun.get();
} finally {
dbUtils.closeSessionForCurrentThread();
}
}
}));
return elements.get(0).get();
}
}
package lcsb.mapviewer.web;
import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.util.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.transaction.annotation.Transactional;
import com.google.gson.*;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.security.Privilege;
import lcsb.mapviewer.model.security.PrivilegeType;
import lcsb.mapviewer.model.user.ConfigurationElementType;
import lcsb.mapviewer.model.user.User;
import lcsb.mapviewer.persist.dao.ProjectDao;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
import lcsb.mapviewer.services.interfaces.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@Rollback
public class UserControllerIntegrationTestWithoutTransaction extends ControllerIntegrationTest {
Logger logger = LogManager.getLogger();
private static final String TEST_USER_PASSWORD = "test_pass";
private static final String TEST_USER_LOGIN = "test_user";
private static final String ADMIN_PASSWORD = "admin";
private static final String ADMIN_LOGIN = "admin";
@Autowired
private IUserService userService;
@Before
public void setup() {
}
@Test
public void createUser() throws Exception {
try {
MockHttpSession session = createSession(ADMIN_LOGIN, ADMIN_PASSWORD);
String body = EntityUtils.toString(new UrlEncodedFormEntity(Arrays.asList(
new BasicNameValuePair("login", TEST_USER_LOGIN),
new BasicNameValuePair("password", TEST_USER_PASSWORD),
new BasicNameValuePair("name", ""),
new BasicNameValuePair("surname", ""),
new BasicNameValuePair("email", ""))));
RequestBuilder request = post("/users/" + TEST_USER_LOGIN)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful());
} finally {
removeUserInSeparateThread(userService.getUserByLogin(TEST_USER_LOGIN));
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment