Replacing a Mocked Spring Boot Controller with the actual Controller - java
I'm new to Spring Boot and Testing.
tl;dr How do I replace a #MockBean controller with the actual controller, in a spring boot application so that I can test that the controller is working instead of just testing that my objects are output correctly?
I'm writing a gradle managed API with dependencies (from build.gradle):
// Spring Boot (2.0.5 Release)
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
// Testing
testImplementation('org.junit.jupiter:junit-jupiter-api:5.3.1')
testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine:5.3.1')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile("org.assertj:assertj-core:3.11.1")
testCompile 'org.mockito:mockito-core:2.+'
I've got an API controller class with the following relevant code:
#Controller
public class ObjectivesApiController extends AbstractRestHelperFunctionality implements ObjectivesApi {
protected ObjectivesApiController(
UserRepository userRepository,
CompaniesRepository companiesRepository,
TeamsRepository teamsRepository,
ProjectsRepository projectsRepository,
OdiAssessmentRepository odiAssessmentRepository,
OdiCustomerRatingRepository odiCustomerRatingRepository,
OdiTechRatingRepository odiTechRatingRepository,
OdiValueRatingRepository odiValueRatingRepository,
ObjectivesRepository objectivesRepository,
KeyResultRepository keyResultRepository) {
super(
userRepository,
companiesRepository,
teamsRepository,
projectsRepository,
odiAssessmentRepository,
odiCustomerRatingRepository,
odiTechRatingRepository,
odiValueRatingRepository,
objectivesRepository,
keyResultRepository);
}
public ResponseEntity<KeyResult> createKeyResult(#ApiParam(value = "id", required = true) #PathVariable("id") Long id, #ApiParam(value = "keyResult", required = true) #Valid #RequestBody KeyResult keyResultDTO) {
KeyResult keyResult = KeyResultBuilder
.aKeyResult()
.withDescription(keyResultDTO.getDescription())
.withCompleted(keyResultDTO.getCompleted())
.build();
Objective parentObjective = objectivesRepository.findByObjectiveId(id);
parentObjective.addKeyResult(keyResult);
keyResultRepository.save(keyResult);
objectivesRepository.save(parentObjective);
return new ResponseEntity<KeyResult>(HttpStatus.CREATED);
}
public ResponseEntity<Objective> createObjective(#ApiParam(value = "objective", required = true) #Valid #RequestBody Objective objectiveDTO) {
Objective objective = ObjectiveBuilder
.anObjective()
.withDescription(objectiveDTO.getDescription())
.withCompleted(objectiveDTO.getCompleted())
.withKeyResults(objectiveDTO.getKeyResults())
.build();
objective.getKeyResults().forEach(keyResultRepository::save);
objectivesRepository.save(objective);
return new ResponseEntity<Objective>(HttpStatus.CREATED);
}
public ResponseEntity<Void> deleteAllLinkedKeyResults(#ApiParam(value = "id", required = true) #PathVariable("id") Long id) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(id);
subjectObjective.getKeyResults().clear();
objectivesRepository.save(subjectObjective);
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
public ResponseEntity<Void> deleteObjective(#ApiParam(value = "id", required = true) #PathVariable("id") Long id) {
objectivesRepository.delete(objectivesRepository.findByObjectiveId(id));
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
public ResponseEntity<Void> deleteOneKeyResult(#ApiParam(value = "the id of the objective you want key results for", required = true) #PathVariable("objectiveId") Long objectiveId, #ApiParam(value = "the id of the key result", required = true) #PathVariable("keyResultId") Long keyResultId) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(objectiveId);
KeyResult keyResult = keyResultRepository.findByKeyResultId(keyResultId);
subjectObjective.removeKeyResult(keyResult);
objectivesRepository.save(subjectObjective);
keyResultRepository.delete(keyResult);
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
public ResponseEntity<List<Objective>> getAllObjectives() {
List<Objective> allObjectives = objectivesRepository.findAll();
return new ResponseEntity<List<Objective>>(allObjectives, HttpStatus.OK);
}
public ResponseEntity<List<KeyResult>> getKeyResultsForObjective(#ApiParam(value = "id", required = true) #PathVariable("id") Long id) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(id);
List<KeyResult> allKeyResults = subjectObjective.getKeyResults();
return new ResponseEntity<List<KeyResult>>(allKeyResults, HttpStatus.OK);
}
public ResponseEntity<Objective> getObjective(#ApiParam(value = "id", required = true) #PathVariable("id") Long id) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(id);
return new ResponseEntity<Objective>(subjectObjective, HttpStatus.OK);
}
public ResponseEntity<KeyResult> getKeyResultForObjective(#ApiParam(value = "the id of the objective you want key results for", required = true) #PathVariable("objectiveId") Long objectiveId, #ApiParam(value = "the id of the key result", required = true) #PathVariable("keyResultId") Long keyResultId) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(objectiveId);
KeyResult subjecKeyResult = subjectObjective.getKeyResults().stream()
.filter(KeyResult -> keyResultId.equals(KeyResult.getKeyResultId()))
.findFirst()
.orElse(null);
return new ResponseEntity<KeyResult>(subjecKeyResult, HttpStatus.OK);
}
public ResponseEntity<Objective> updateObjective(#ApiParam(value = "id", required = true) #PathVariable("id") Long id, #ApiParam(value = "objective", required = true) #Valid #RequestBody Objective objectiveDTO) {
Objective existingObjective = objectivesRepository.findByObjectiveId(id);
Objective objective = ObjectiveBuilder
.anObjective()
.withObjectiveId(existingObjective.getObjectiveId())
.withDescription(objectiveDTO.getDescription())
.withCompleted(objectiveDTO.getCompleted())
.withKeyResults(objectiveDTO.getKeyResults())
.build();
objective.getKeyResults().forEach(keyResultRepository::save);
objectivesRepository.save(objective);
return new ResponseEntity<Objective>(HttpStatus.NO_CONTENT);
}
public ResponseEntity<KeyResult> updateKeyResult(#ApiParam(value = "the id of the objective you want key results for", required = true) #PathVariable("objectiveId") Long objectiveId, #ApiParam(value = "the id of the key result", required = true) #PathVariable("keyResultId") Long keyResultId, #ApiParam(value = "keyResult", required = true) #Valid #RequestBody KeyResult keyResultDTO) {
if (objectivesRepository.existsById(objectiveId) && keyResultRepository.existsById(keyResultId)) {
Objective subjectObjective = objectivesRepository.findByObjectiveId(objectiveId);
KeyResult subjecKeyResult = subjectObjective.getKeyResults().stream()
.filter(KeyResult -> keyResultId.equals(KeyResult.getKeyResultId()))
.findFirst()
.orElse(null);
KeyResult updatedKeyResult = KeyResultBuilder
.aKeyResult()
.withKeyResultId(subjecKeyResult.getKeyResultId())
.withDescription(keyResultDTO.getDescription())
.withCompleted(keyResultDTO.getCompleted())
.build();
keyResultRepository.save(updatedKeyResult);
Collections.replaceAll(subjectObjective.getKeyResults(), subjecKeyResult, updatedKeyResult);
objectivesRepository.save(subjectObjective);
}
return new ResponseEntity<KeyResult>(HttpStatus.NO_CONTENT);
}
}
For context on this class, all the AbstractRestHelper super class is doing, is creating singletons of my repositories, which are then .. field injected (unsure if this is the right term) in to the controller. This pattern is repeated across all controllers hence the clutter.
The API being implemented is a Swagger 2 API interface that keeps this controller free of annotations where possible.
The final piece is the test class. This is the core of my question.
#ExtendWith(SpringExtension.class)
#WebMvcTest(ObjectivesApiController.class)
class ObjectivesApiControllerTest {
#Autowired
private MockMvc mockMvc;
#MockBean
private ObjectivesApiController objectivesApiControllerMock;
#BeforeEach
void setUp() {
}
#AfterEach
void tearDown() {
}
#Test
void getAllObjectives() throws Exception {
// Create two objects to test with:
Objective testObjective1 = ObjectiveBuilder
.anObjective()
.withObjectiveId(1L)
.withDescription("Test Objective")
.withCompleted(false)
.build();
Objective testObjective2 = ObjectiveBuilder
.anObjective()
.withObjectiveId(2L)
.withDescription("Test Objective")
.withCompleted(true)
.build();
List<Objective> testList = new ArrayList<Objective>();
testList.add(testObjective1);
testList.add(testObjective2);
// Set expectations on what should be found:
when(objectivesApiControllerMock.getAllObjectives()).thenReturn(new ResponseEntity<List<Objective>>(testList, HttpStatus.OK));
// Carry out the mocked API call:
mockMvc.perform(get("/objectives"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].objectiveId", is(1)))
.andExpect(jsonPath("$[0].description", is("Test Objective")))
.andExpect(jsonPath("$[0].completed", is(false)))
.andExpect(jsonPath("$[1].objectiveId", is(2)))
.andExpect(jsonPath("$[1].description", is("Test Objective")))
.andExpect(jsonPath("$[1].completed", is(true)));
// Validate the response is what we expect:
verify(objectivesApiControllerMock, times(1)).getAllObjectives();
verifyNoMoreInteractions(objectivesApiControllerMock);
}
#Test
void getKeyResultsForObjective() throws Exception {
KeyResult testKeyResultWithParentObjective1 = KeyResultBuilder
.aKeyResult()
.withKeyResultId(1L)
.withCompleted(false)
.withDescription("My parent Key Result is 1")
.build();
KeyResult testKeyResultWithParentObjective2 = KeyResultBuilder
.aKeyResult()
.withKeyResultId(2L)
.withCompleted(true)
.withDescription("My parent Key Result is 1")
.build();
Objective testObjectiveWithKeyResults = ObjectiveBuilder
.anObjective()
.withObjectiveId(1L)
.withDescription("Test Objective")
.withKeyResults(new ArrayList<KeyResult>())
.withCompleted(false)
.build();
testObjectiveWithKeyResults.addKeyResult(testKeyResultWithParentObjective1);
testObjectiveWithKeyResults.addKeyResult(testKeyResultWithParentObjective2);
when(objectivesApiControllerMock.getKeyResultsForObjective(1L)).thenReturn(new ResponseEntity<List<KeyResult>>(testObjectiveWithKeyResults.getKeyResults(), HttpStatus.OK));
mockMvc.perform(get("/objectives/1/keyresult"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].keyResultId", is(1)))
.andExpect(jsonPath("$[0].description", is("My parent Key Result is 1")))
.andExpect(jsonPath("$[0].completed", is(false)))
.andExpect(jsonPath("$[1].keyResultId", is(2)))
.andExpect(jsonPath("$[1].description", is("My parent Key Result is 1")))
.andExpect(jsonPath("$[1].completed", is(true)));
}
}
My question is this:
Having mocked the objective controller using Mockito to validate that my objects are being formed properly, I now want to do the same thing but instead of mocking, I want to actually test the controller.
What do you think is the most naive way of getting this to work (I can refactor later). The resources I've search through either use different versions of Junit or rely on mockito rather than the actual controller.
Nothing fits quite right - since the controller is mocked, I'm not actually covering any code, and so the tests are worthless right? The only thing I'm looking at is that the objects are formed properly, where I now need to check that the controller is functioning as it should, AND are returning well formed objects.
Has anyone done anything simillar? What approaches do you use to manage the testing of field injected controllers?
Any advice on this would be hugely appreciated. I'd love to learn how people working on production grade applications are handling the testing of Spring Boot Apps with Controllers, Repos, etc.
Thanks so much!
You could use #SpyBean. That way you can both use it as it is or mock some calls. https://www.baeldung.com/mockito-spy
Related
FlatMap is not being called after a webclient call
I have this function inside a #Service in Spring Webflux and it is called with a list of friends to know if everyone has been joined to a group. If some friends have no group this method calls an API to get its user's information and then calls another API to tag these guys with joined tag false. #Service ... public Flux<Boolean> checkUserHaveGroup(final List<String> friends) { MatchOperation match1 = Aggregation.match(Criteria.where("friends").in(friends).and("status").is("ACTIVE")); UnwindOperation unwind1 = Aggregation.unwind("friends"); MatchOperation match2 = Aggregation.match(Criteria.where("friends").in(friends)); GroupOperation group1 = Aggregation.group("friends"); TypedAggregation<Group> a = Aggregation.newAggregation( Group.class, match1, unwind1, match2, group1); return this.reactiveMongoTemplate.aggregate(a, FriendInGroup.class) .map(friendInGroup -> friendInGroup.id) .collectList() .map(users -> haveNoGroupsList(users, friends)) .flatMapMany(noGroupUsers -> { return Flux.fromIterable(noGroupUsers) .flatMap(pn -> crmService.deleteAttribute(pn, "joinedAGroup")); }); } (this method get the user information) ... public Mono<UserInfo> userInfoById(final String userId) { return webClient.get() .uri(uriBuilder -> uriBuilder.path(constants.getByIdPath() + "/{id}") .build(userId)) .header("auth", tokenService.token()) .exchange() .flatMap(response -> { Mono<UserInfo> responseMono; if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) { responseMono = Mono.error(new UnauthorizedException()); } else if (response.statusCode().equals(HttpStatus.OK)) { responseMono = response.bodyToMono(UserInfoResponse.class) .flatMap(uir -> Mono.just(uir.getData())); } else { responseMono = Mono.error(new UnhandledException()); } return responseMono; }); } ... private Mono<UserInfo> getUserInfo(String userId) { return userInfoAdapter.userInfoById(userId); } ... public Mono<Boolean> deleteAttribute(final String userId, final String attribute) { return getUserInfo(userId) <<<<< here we get the users info .flatMap(ui -> crmDeleteAttribute(ui, attribute)); <<<< this call is never done. } ... public Mono<Boolean> crmDeleteAttribute(final UserInfo user, final String attribute) { return webClient.delete() .uri(uriBuilder -> uriBuilder .path(contants.path()) .build(user.getId(), attribute)) .header("auth", tokenService.token()) .exchange().flatMap(response -> { if (response.statusCode().equals(HttpStatus.OK)) { return Mono.just(Boolean.TRUE); } if (response.statusCode().equals(HttpStatus.BAD_REQUEST)) { return Mono.error(CrmServiceBadRequestException::new); } if (response.statusCode().equals(HttpStatus.UNAUTHORIZED)) { return Mono.error(CrmServiceUnauthorizedException::new); } return Mono.error(CrmServiceUnhandledException::new); }); } After getting the users' info on the API, the API for assign a tag is never been called no matter what I do. I can see in the debugger terminal that the call to UserInfo API was done but after that, the application returns to the controller. Someone could point me to what I'm doing wrong? Any help is welcome. Thanks!
Spring boot integration testing issue
I am working on a project, i have completed but issue is in project i have to write integration testing code. This is the controller #PostMapping("/newgame") public ResponseEntity<Players> beginNewGame(#RequestBody Players players) { Players savedPersion = playerService.beginNewGame(players); return savedPersion!=null ? ResponseEntity.ok(savedPersion): (ResponseEntity<Players>) ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR); } And this is service. #Override public Players beginNewGame(Players players) { //new game state GameState gameState =new GameState(); gameState.setWinner("null"); GameState save = gameStateRepositery.save(gameState); //new score Score score = new Score(); score.setGameId(save.getId()); Score savedScore = scoreRepositery.save(score); players.setScoreId(savedScore.getId()); players.setGameId(save.getId()); Players savedPlayers=null; savedPlayers= playersRepositery.save(players); System.out.println(savedScore.getGameId()+"ksdjfkjskn"); return savedPlayers; } And if i hit the endpoints with proper required parameters, the is working fine, and return me. { "gameId": 57, "id": 1, "playerOne": "vinitSaini", "playerTwo": "anonymousKal", "scoreId": 58 } This is the testing code, how i can check what the response is returning. #ExtendWith(SpringExtension.class) #SpringBootTest #AutoConfigureMockMvc public class PlayersIntTest { #Autowired private MockMvc mockMvc; #Autowired private ObjectMapper objectMapper; #Autowired private PlayersRepositery playersRepositery; #Test void newGame() throws Exception { //mockMvc.perform(post("/api/{forumId}/register", 42L) mockMvc.perform(post("/api/newgame") .contentType("application/json") .content(objectMapper.writeValueAsString(players))) .andExpect(status().isOk()); } So please help me to write integration testing.
You can convert the response in JSON and check values of JSON properties using MockMvcResultMatchers mockMvc.perform(post("/api/newgame") .contentType("application/json") .content(objectMapper.writeValueAsString(players))) .andExpect(status().isOk()) .andExpect(MockMvcResultMatchers.content() .contentType(MediaType.APPLICATION_JSON)) .andExpect(MockMvcResultMatchers.jsonPath("$.gameId") .value(57)) .andExpect(MockMvcResultMatchers.jsonPath("$.id") .value(1)) .andExpect(MockMvcResultMatchers.jsonPath("$.playerOne") .value("vinitSaini")) .andExpect(MockMvcResultMatchers.jsonPath("$.playerTwo") .value("anonymousKal")) .andExpect(MockMvcResultMatchers.jsonPath("$.scoreId") .value(58));
#Test void newGame() throws Exception { //mockMvc.perform(post("/api/{forumId}/register", 42L) ResultActions resultActions = mockMvc.perform(post("/api/newgame") .contentType("application/json") .content(objectMapper.writeValueAsString(players))) .andExpect(status().isOk()); String responseInJson = resultActions.andReturn().getResponse().getContentAsString(); Players responseObj = objectMapper.readValue(responseInJson, Players.class); Assert.assertNotNull(responseObj); // You can add all asserts here }
Unable to mock accountStatus with Mockito
From below piece of code I am not able to mock checkAccountStatus and its coming as null. What changes do I need to do to resolve this issue? Class public AccessIDSearchResponse searchAccessID(AccessIDSearchRequest accessIDRequest) { String[] productTypes = accessIDRequest.getProductTypes(); AccountResponse actResponse = checkAccountStatus(accessIDRequest); System.out.println("Response is---->"+JsonService.getJsonFromObject(actResponse)); if (accessIDRequest.getSearchtype().equalsIgnoreCase("accountId") && !Utility.isEmpty(actResponse) && !"FREEVIEW".equalsIgnoreCase(actResponse.getAccountStatus())) { errorHandler.error(ErrorMessages.EPO_EXISTINGTV_ERR_07, ErrorMessages.ACCESS_ID_NOT_FOUND); } } public AccountResponse checkAccountStatus(AccessIDSearchRequest request) { AccessIDSearchResponse response = new AccessIDSearchResponse(); SearchAccessIdContent content = new SearchAccessIdContent(); DTVNAccountDetails accountDetails = new DTVNAccountDetails(); accountDetails.setAccountNumber(request.getSearchvalue()); List<DTVNAccountDetails> list = new ArrayList<>(); list.add(accountDetails); content.setDtvAccountList(list); response.setContent(content); return helper.getAccountStatus(response); } Helper public AccountResponse getAccountStatus(AccessIDSearchResponse accessIDResponse) { AccountResponse accountResponse = null; AccountRequest request = new AccountRequest(); Account account = new Account(); account.setCustomerID(accessIDResponse.getContent().getDtvAccountList().get(0).getAccountNumber()); request.setAccount(account); String response = dtvnClients.callandGetDtvnStatus(request); System.out.println("Response is--->"+response); if (!Utility.isEmpty(response)) { accountResponse = JqUtil.runJqQueryAndGetString(".content.accountResponse", response, AccountResponse.class); if (!Utility.isEmpty(accountResponse) && accountResponse.isSubscribable() && !Utility.isEmpty(accountResponse.getAccountStatus()) && accountResponse.getAccountStatus().equalsIgnoreCase("FREEVIEW")) { return accountResponse; } } return accountResponse; } Test Class #Test(expected = ServiceException.class) public void test_searchAccessID_3_sample() throws Exception { AccessIDSearchRequest request = new AccessIDSearchRequest(); CommonData commonData = new CommonData(); commonData.setAppName("IDSE"); commonData.setLoginId("qay_slid_sr1281"); request.setCommonData(commonData); request.setSearchtype("accountId"); request.setSearchvalue("qay_slid_sr1281"); request.setMode("abc"); SearchAccessIdContent content = new SearchAccessIdContent(); AccountResponse accountResponse = new AccountResponse(); accountResponse.setAccountStatus("Sucess"); accountResponse.setSubscribable(true); Mockito.when(helper.getAccountStatus(accessIDResponse)).thenReturn(accountResponse); Mockito.when(service.checkAccountStatus(request)).thenReturn(accountResponse); service.searchAccessID(header, request); }
Your mocks are not properly configured. When you call service.searchAccessID(header, request); it was make the underlying call checkAccountStatus(request); (which is correctly mocked and returns accountResponse), but this one does instanciate its result object, so your first mock will never be triggered. Updating your first mock to something more permissive will probably fix your problem Mockito.when(helper.getAccountStatus(any(AccessIDSearchResponse.class))).thenReturn(accountResponse); To be honest, your code is hardly testable because you instanciate too many objects everywhere. Going for mocks here will be a pain in the future when you refactor something. If I were you I would rewrite this piece of code using a TDD approach and favorizing more testable patterns.
Hibernate not deleting object even though the debug is ok?
I'm trying to delete an Object using Hibernate but the thing is not deleting. I debugged the program to make sure the Object is correct and it is, so I'm guessing the problem might be in something I have no idea what it is ... annotations, configuration ?? Maybe someone can help ! Here's the program: Controller: // Erased the imports to make it simpler #RestController public class Controlador { #Autowired private FisicHostDao fisicHostDao; #Autowired private CredentialService credentialService; #RequestMapping(value = "/fisicHost/{id}/credentials", method = RequestMethod.GET, produces = APPLICATION_JSON_UTF8_VALUE) public List<Credential> credentialsByFisicHost(#PathVariable(value = "id") final Long fisicHostId, ModelMap modelMap){ FisicHost optionalFisicHost = fisicHostDao.findById(fisicHostId); if (optionalFisicHost == null) { // Responder con 404 } FisicHost fisicHost = optionalFisicHost; return fisicHost.getCredentials(); } // This is the method handling the request / response #RequestMapping(value = "/fisicHost/{id}/credentials", method = RequestMethod.POST) public String deleteCredential(#PathVariable(value = "id") String credId){ String[] parts = credId.split("-"); int id = Integer.parseInt(parts[1]); Credential c = credentialService.getCredentialById(id); credentialService.delete(c); return "justreturnsomething"; } } As you can see in the picture the object is not null and it does matches the object I want to delete ... So why is it not deleting ?
I'm guessing you need to commit a transaction so that the delete actually gets committed to the database. See Transaction Eg: Session session = sessionFactory.openSession(); try { session.beginTransaction(); try { doHibernateStuff(session); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); throw e; } } finally { session.close(); }
JPA conditional insertion
I have a Java Spring based web application and I want to insert a record to a table only if the table does not contain any rows that are "similar" (according to some specific, irrelevant criteria) to the new row. Because this is a multi-threaded environment, I cannot use a SELECT+INSERT two-step combination as it would expose me to a race condition. The same question was first asked and answered here and here several years ago. Unfortunately, the questions have got only a little attention and the provided answer is not sufficient to my needs. Here's the code I currently have and it's not working: #Component("userActionsManager") #Transactional public class UserActionsManager implements UserActionsManagerInterface { #PersistenceContext(unitName = "itsadDB") private EntityManager manager; #Resource(name = "databaseManager") private DB db; ... #SuppressWarnings("unchecked") #Override #PreAuthorize("hasRole('ROLE_USER') && #username == authentication.name") public String giveAnswer(String username, String courseCode, String missionName, String taskCode, String answer) { ... List<Submission> submissions = getAllCorrectSubmissions(newSubmission); List<Result> results = getAllCorrectResults(result); if (submissions.size() > 0 || results.size() > 0) throw new SessionAuthenticationException("foo"); manager.persist(newSubmission); manager.persist(result); submissions = getAllCorrectSubmissions(newSubmission); results = getAllCorrectResults(result); for (Submission s : submissions) manager.lock(s, LockModeType.OPTIMISTIC_FORCE_INCREMENT); for (Result r : results ) manager.lock(r, LockModeType.OPTIMISTIC_FORCE_INCREMENT); manager.flush(); ... } #SuppressWarnings("unchecked") private List<Submission> getAllCorrectSubmissions(Submission newSubmission) { Query q = manager.createQuery("SELECT s FROM Submission AS s WHERE s.missionTask = ?1 AND s.course = ?2 AND s.user = ?3 AND s.correct = true"); q.setParameter(1, newSubmission.getMissionTask()); q.setParameter(2, newSubmission.getCourse()); q.setParameter(3, newSubmission.getUser()); return (List<Submission>) q.getResultList(); } #SuppressWarnings("unchecked") private List<Result> getAllCorrectResults(Result result) { Query q = manager.createQuery("SELECT r FROM Result AS r WHERE r.missionTask = ?1 AND r.course = ?2 AND r.user = ?3"); q.setParameter(1, result.getMissionTask()); q.setParameter(2, result.getCourse()); q.setParameter(3, result.getUser()); return (List<Result>) q.getResultList(); } ... } According to the answer provided here I am supposed to somehow use OPTIMISTIC_FORCE_INCREMENT but it's not working. I suspect that the provided answer is erroneous so I need a better one. edit: Added more context related code. Right now this code still has a race condition. When I make 10 simultaneous HTTP POST requests approximately 5 rows will get erroneously inserted. Other 5 requests are rejected with HTTP error code 409 (conflict). The correct code would guarantee that only 1 row would get inserted to the database no matter how many concurrent requests I make. Making the method synchronous is not a solution since the race condition still manifests for some unknown reason (I tested it).
Unfortunately after several days of research I was unable to find a short and simple solution to my problem. Since my time budget is not unlimited I had to come up with a workaround. Call it a kludge if you may. Since the whole HTTP request is a transaction, it will be rolled back at the sight of any conflicts. I am using this for my advantage by locking a special entity within the context of the whole HTTP request. Should multiple HTTP requests be received at the same time, all but one will result in some PersistenceException. In the beginning of the transaction I am checking whether no other correct answers have been submitted yet. During that check the lock is already effective so no race condition could happen. The lock is effective until the answer is submitted. This basically simulates a critical section as a SELECT+INSERT two step query on the application level (in pure MySQL I would have used the INSERT IF NOT EXISTS construct). This approach has some drawbacks. Whenever two students submit an answer at the same time, one of them will be thrown an exception. This is sort of bad for performance and bandwidth because the student who received HTTP STATUS 409 has to resubmit their answer. To compensate the latter, I am automatically retrying to submit the answer on the server side a couple of times between randomly chosen time intervals. See the according HTTP request controller code is below: #Controller #RequestMapping("/users") public class UserActionsController { #Autowired private SessionRegistry sessionRegistry; #Autowired #Qualifier("authenticationManager") private AuthenticationManager authenticationManager; #Resource(name = "userActionsManager") private UserActionsManagerInterface userManager; #Resource(name = "databaseManager") private DB db; . . . #RequestMapping(value = "/{username}/{courseCode}/missions/{missionName}/tasks/{taskCode}/submitAnswer", method = RequestMethod.POST) public #ResponseBody Map<String, Object> giveAnswer(#PathVariable String username, #PathVariable String courseCode, #PathVariable String missionName, #PathVariable String taskCode, #RequestParam("answer") String answer, HttpServletRequest request) { init(request); db.log("Submitting an answer to task `"+taskCode+"` of mission `"+missionName+ "` in course `"+courseCode+"` as student `"+username+"`."); String str = null; boolean conflict = true; for (int i=0; i<10; i++) { Random rand = new Random(); int ms = rand.nextInt(1000); try { str = userManager.giveAnswer(username, courseCode, missionName, taskCode, answer); conflict = false; break; } catch (EntityExistsException e) {throw new EntityExistsException();} catch (PersistenceException e) {} catch (UnexpectedRollbackException e) {} try { Thread.sleep(ms); } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } } if (conflict) str = userManager.giveAnswer(username, courseCode, missionName, taskCode, answer); if (str == null) db.log("Answer accepted: `"+answer+"`."); else db.log("Answer rejected: `"+answer+"`."); Map<String, Object> hm = new HashMap<String, Object>(); hm.put("success", str == null); hm.put("message", str); return hm; } } If for some reason the controller is unable to commit the transaction 10 times in a row then it will try one more time but will not attempt to catch the possible exceptions. When an exception is thrown on the 11th try then it will be processed by the global exception controller and the client will receive HTTP STATUS 409. The global exception controller is defined below. #ControllerAdvice public class GlobalExceptionController { #Resource(name = "staticDatabaseManager") private StaticDB db; #ExceptionHandler(SessionAuthenticationException.class) #ResponseStatus(value=HttpStatus.FORBIDDEN, reason="session has expired") //403 public ModelAndView expiredException(HttpServletRequest request, Exception e) { ModelAndView mav = new ModelAndView("exception"); mav.addObject("name", e.getClass().getSimpleName()); mav.addObject("message", e.getMessage()); return mav; } #ExceptionHandler({UnexpectedRollbackException.class, EntityExistsException.class, OptimisticLockException.class, PersistenceException.class}) #ResponseStatus(value=HttpStatus.CONFLICT, reason="conflicting requests") //409 public ModelAndView conflictException(HttpServletRequest request, Exception e) { ModelAndView mav = new ModelAndView("exception"); mav.addObject("name", e.getClass().getSimpleName()); mav.addObject("message", e.getMessage()); synchronized (db) { db.setUserInfo(request); db.log("Conflicting "+request.getMethod()+" request to "+request.getRequestURI()+" ("+e.getClass().getSimpleName()+").", Log.LVL_SECURITY); } return mav; } //ResponseEntity<String> customHandler(Exception ex) { // return new ResponseEntity<String>("Conflicting requests, try again.", HttpStatus.CONFLICT); //} } Finally, the giveAnswer method itself utilizes a special entity with a primary key lock_addCorrectAnswer. I lock that special entity with the OPTIMISTIC_FORCE_INCREMENT flag which makes sure that no two transactions can have overlapping execution times for the giveAnswer method. The respective code can be seen below: #Component("userActionsManager") #Transactional public class UserActionsManager implements UserActionsManagerInterface { #PersistenceContext(unitName = "itsadDB") private EntityManager manager; #Resource(name = "databaseManager") private DB db; . . . #SuppressWarnings("unchecked") #Override #PreAuthorize("hasRole('ROLE_USER') && #username == authentication.name") public String giveAnswer(String username, String courseCode, String missionName, String taskCode, String answer) { . . . if (!userCanGiveAnswer(user, course, missionTask)) { error = "It is forbidden to submit an answer to this task."; db.log(error, Log.LVL_MAJOR); return error; } . . . if (correctAnswer) { . . . addCorrectAnswer(newSubmission, result); return null; } newSubmission = new Submission(user, course, missionTask, answer, false); manager.persist(newSubmission); return error; } private void addCorrectAnswer(Submission submission, Result result) { String var = "lock_addCorrectAnswer"; Global global = manager.find(Global.class, var); if (global == null) { global = new Global(var, 0); manager.persist(global); manager.flush(); } manager.lock(global, LockModeType.OPTIMISTIC_FORCE_INCREMENT); manager.persist(submission); manager.persist(result); manager.flush(); long submissions = getCorrectSubmissionCount(submission); long results = getResultCount(result); if (submissions > 1 || results > 1) throw new EntityExistsException(); } private long getCorrectSubmissionCount(Submission newSubmission) { Query q = manager.createQuery("SELECT count(s) FROM Submission AS s WHERE s.missionTask = ?1 AND s.course = ?2 AND s.user = ?3 AND s.correct = true"); q.setParameter(1, newSubmission.getMissionTask()); q.setParameter(2, newSubmission.getCourse()); q.setParameter(3, newSubmission.getUser()); return (Long) q.getSingleResult(); } private long getResultCount(Result result) { Query q = manager.createQuery("SELECT count(r) FROM Result AS r WHERE r.missionTask = ?1 AND r.course = ?2 AND r.user = ?3"); q.setParameter(1, result.getMissionTask()); q.setParameter(2, result.getCourse()); q.setParameter(3, result.getUser()); return (Long) q.getSingleResult(); } } It is important to note that the entity Global has to have a version annotation in its class for the OPTIMISTIC_FORCE_INCREMENT to work (see code below). #Entity #Table(name = "GLOBALS") public class Global implements Serializable { . . . #Id #Column(name = "NAME", length = 32) private String key; #Column(name = "INTVAL") private int intVal; #Column(name = "STRVAL", length = 4096) private String strVal; #Version private Long version; . . . } Such an approach can be optimized even further. Instead of using the same lock name lock_addCorrectAnswer for all giveAnswer calls, I could generate the lock name deterministically from the name of the submitting user. For example, if the student's username is Hyena then the primary key for the lock entity would be lock_Hyena_addCorrectAnswer. That way multiple students could submit answers at the same time without receiving any conflicts. However, if a malicious user spams the HTTP POST method for submitAnswer 10x in parallel they will be prevented by the this locking mechanism.