PAN-50 Wrote Unit tests for the Theorem Controller, and initial integration Test for the theorem controller, but need to fix custom queries in both accountRepository and theoremRepository

This commit is contained in:
2019-03-11 00:09:34 -06:00
parent 816d85196b
commit fd760293ef
6 changed files with 387 additions and 6 deletions
@@ -56,7 +56,7 @@ public class PersistenceTestConfig {
.create() .create()
.username("panda") .username("panda")
.password("secret") .password("secret")
.url("jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false") .url("jdbc:mysql://127.0.0.1:3306/pandamonium?autoReconnect=true&useSSL=false&serverTimezone=UTC")
.driverClassName("com.mysql.cj.jdbc.Driver") .driverClassName("com.mysql.cj.jdbc.Driver")
.build(); .build();
} }
@@ -0,0 +1,90 @@
package edu.msudenver.tsp.persistence;
import edu.msudenver.tsp.persistence.dto.TheoremDto;
import edu.msudenver.tsp.persistence.dto.TheoremType;
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = PersistenceTestConfig.class)
public class TheoremsIntegrationTest {
@Autowired private TheoremRepository theoremRepository;
@Test
public void testCRUDFunctionality() {
final TheoremDto theoremDto = createTheorem();
final TheoremDto savedTheorem = theoremRepository.save(theoremDto);
assertNotNull(savedTheorem);
assertEquals(Integer.valueOf(0), savedTheorem.getVersion());
final int id = savedTheorem.getId();
assertEquals("Test theorem", savedTheorem.getName());
assertEquals("Test branch", savedTheorem.getBranch());
assertTrue(savedTheorem.isProvenStatus());
assertEquals(2, savedTheorem.getReferencedTheorems().size());
assertEquals(2, savedTheorem.getReferencedDefinitions().size());
assertEquals("test theorem 1", savedTheorem.getReferencedTheorems().get(0));
assertEquals("test theorem 2", savedTheorem.getReferencedTheorems().get(1));
assertEquals("test definition 1", savedTheorem.getReferencedDefinitions().get(0));
assertEquals("test definition 2", savedTheorem.getReferencedDefinitions().get(1));
savedTheorem.setBranch("Test Update");
final TheoremDto updatedTheorem = theoremRepository.save(savedTheorem);
assertNotNull(updatedTheorem);
assertEquals(Integer.valueOf(0), updatedTheorem.getVersion());
assertEquals("Test theorem", updatedTheorem.getName());
assertEquals("Test Update", updatedTheorem.getBranch());
assertTrue(updatedTheorem.isProvenStatus());
assertEquals(2, updatedTheorem.getReferencedTheorems().size());
assertEquals(2, updatedTheorem.getReferencedDefinitions().size());
assertEquals("test theorem 1", updatedTheorem.getReferencedTheorems().get(0));
assertEquals("test theorem 2", updatedTheorem.getReferencedTheorems().get(1));
assertEquals("test definition 1", updatedTheorem.getReferencedDefinitions().get(0));
assertEquals("test definition 2", updatedTheorem.getReferencedDefinitions().get(1));
assertEquals(updatedTheorem.getId(), id);
final List<TheoremDto> listOfTheoremsByBranch = theoremRepository.findByBranch("Test Update");
assertNotNull(listOfTheoremsByBranch);
assertEquals(1, listOfTheoremsByBranch.size());
assertEquals(updatedTheorem, listOfTheoremsByBranch.get(0));
theoremRepository.delete(theoremDto);
final Optional<TheoremDto> deletedTheorem = theoremRepository.findById(id);
assertFalse(deletedTheorem.isPresent());
}
private TheoremDto createTheorem() {
final List<String> referencedTheoremsList = new ArrayList<>();
referencedTheoremsList.add("test theorem 1");
referencedTheoremsList.add("test theorem 2");
final List<String> referencedDefinitionsList = new ArrayList<>();
referencedDefinitionsList.add("test definition 1");
referencedDefinitionsList.add("test definition 2");
final TheoremDto theoremDto = new TheoremDto();
theoremDto.setName("Test theorem");
theoremDto.setBranch("Test branch");
theoremDto.setProvenStatus(true);
theoremDto.setTheoremType(TheoremType.THEOREM);
theoremDto.setReferencedTheorems(referencedTheoremsList);
theoremDto.setReferencedDefinitions(referencedDefinitionsList);
return theoremDto;
}
}
@@ -67,7 +67,7 @@ public class TheoremController {
@GetMapping("/{proven_status}") @GetMapping("/{proven_status}")
public @ResponseBody public @ResponseBody
ResponseEntity<List<TheoremDto>> getAllTheoremsByBranch(@PathVariable("proven_status") final Boolean provenStatus) { ResponseEntity<List<TheoremDto>> getAllTheoremsByProvenStatus(@PathVariable("proven_status") final Boolean provenStatus) {
LOG.info("Received request to query for theorems whose proven status is " + provenStatus); LOG.info("Received request to query for theorems whose proven status is " + provenStatus);
if (provenStatus == null) { if (provenStatus == null) {
LOG.error("ERROR: branch was null"); LOG.error("ERROR: branch was null");
@@ -47,7 +47,7 @@ public class AccountControllerTest {
assertTrue(responseEntity.hasBody()); assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody()); assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(account -> assertEquals(account, accountDto)); responseEntity.getBody().forEach(account -> assertEquals(accountDto, account));
} }
@Test @Test
@@ -166,10 +166,10 @@ public class AccountControllerTest {
@Test @Test
public void testInsertAccount_bindingResultHasErrors() { public void testInsertAccount_bindingResultHasErrors() {
final AccountDto definitionDto = createAccount(); final AccountDto accountDto = createAccount();
when(bindingResult.hasErrors()).thenReturn(true); when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = accountController.insertAccount(definitionDto, bindingResult); final ResponseEntity responseEntity = accountController.insertAccount(accountDto, bindingResult);
assertNotNull(responseEntity); assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody()); assertFalse(responseEntity.hasBody());
@@ -46,7 +46,7 @@ public class DefinitionControllerTest {
assertTrue(responseEntity.hasBody()); assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody()); assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(definition -> assertEquals(definition, definitionDto)); responseEntity.getBody().forEach(definition -> assertEquals(definitionDto, definition));
} }
@Test @Test
@@ -0,0 +1,291 @@
package edu.msudenver.tsp.persistence.controller;
import edu.msudenver.tsp.persistence.dto.TheoremDto;
import edu.msudenver.tsp.persistence.dto.TheoremType;
import edu.msudenver.tsp.persistence.repository.TheoremRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
@WebMvcTest(controllers = TheoremController.class)
public class TheoremControllerTest {
@Mock private TheoremRepository theoremRepository;
@InjectMocks private TheoremController theoremController;
@Mock private BindingResult bindingResult;
@Test
public void testGetAllTheorems() {
final TheoremDto theoremDto = createTheorem();
final List<TheoremDto> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
listOfTheorems.add(theoremDto);
when(theoremRepository.findAll()).thenReturn(listOfTheorems);
final ResponseEntity<Iterable<TheoremDto>> responseEntity = theoremController.getAllTheorems();
assertNotNull(responseEntity);
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetTheoremById() {
final TheoremDto theoremDto = createTheorem();
when(theoremRepository.findById(anyInt())).thenReturn(Optional.ofNullable(theoremDto));
final ResponseEntity<TheoremDto> responseEntity = theoremController.getTheoremById(1);
assertNotNull(responseEntity);
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(theoremDto, responseEntity.getBody());
verify(theoremRepository).findById(anyInt());
}
@Test
public void testGetTheoremById_nullId() {
final ResponseEntity responseEntity = theoremController.getTheoremById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testGetTheoremById_noTheoremFound() {
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = theoremController.getTheoremById(1);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(theoremRepository).findById(anyInt());
}
@Test
public void testGetAllTheoremsByBranch() {
final TheoremDto theoremDto = createTheorem();
final List<TheoremDto> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
listOfTheorems.add(theoremDto);
when(theoremRepository.findByBranch(anyString())).thenReturn(listOfTheorems);
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByBranch("test");
assertNotNull(responseEntity);
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByBranch_nullBranch() {
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByBranch(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testGetAllTheoremsByProvenStatus() {
final TheoremDto theoremDto = createTheorem();
final List<TheoremDto> listOfTheorems = new ArrayList<>();
listOfTheorems.add(theoremDto);
listOfTheorems.add(theoremDto);
when(theoremRepository.findByProvenStatus(anyBoolean())).thenReturn(listOfTheorems);
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByProvenStatus(true);
assertNotNull(responseEntity);
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
responseEntity.getBody().forEach(theorem -> assertEquals(theoremDto, theorem));
}
@Test
public void testGetAllTheoremsByProvenStatus_nullProvenStatus() {
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByProvenStatus(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testInsertTheorem() {
final TheoremDto theoremDto = createTheorem();
when(theoremRepository.save(any(TheoremDto.class))).thenReturn(theoremDto);
final ResponseEntity<TheoremDto> responseEntity = theoremController.insertTheorem(theoremDto, bindingResult);
assertNotNull(responseEntity);
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
assertEquals(theoremDto, responseEntity.getBody());
verify(theoremRepository).save(any(TheoremDto.class));
}
@Test
public void testInsertTheorem_theoremDtoIsNull() {
final ResponseEntity responseEntity = theoremController.insertTheorem(null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testInsertTheorem_bindingResultHasErrors() {
final TheoremDto theoremDto = createTheorem();
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity responseEntity = theoremController.insertTheorem(theoremDto, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testUpdateTheorem() {
final TheoremDto existingTheorem = createTheorem();
existingTheorem.setId(1);
existingTheorem.setVersion(1);
final TheoremDto theoremUpdate = new TheoremDto();
theoremUpdate.setName("Test Update");
final TheoremDto updatedTheorem = existingTheorem;
updatedTheorem.setName("Test Update");
when(theoremRepository.findById(anyInt())).thenReturn(Optional.of(existingTheorem));
when(theoremRepository.save(any(TheoremDto.class))).thenReturn(updatedTheorem);
final ResponseEntity<TheoremDto> responseEntity = theoremController.updateTheorem(1, theoremUpdate, bindingResult);
assertNotNull(responseEntity);
assertTrue(responseEntity.hasBody());
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(updatedTheorem, responseEntity.getBody());
verify(theoremRepository).findById(anyInt());
verify(theoremRepository).save(any(TheoremDto.class));
}
@Test
public void testUpdateTheorem_bindingResultHasErrors() {
when(bindingResult.hasErrors()).thenReturn(true);
final ResponseEntity<TheoremDto> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testUpdateTheorem_theoremDtoIsNull() {
final ResponseEntity<TheoremDto> responseEntity = theoremController.updateTheorem(1, null, bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testUpdateTheorem_idIsNull() {
final ResponseEntity<TheoremDto> responseEntity = theoremController.updateTheorem(null, createTheorem(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
@Test
public void testUpdateTheorem_theoremDoesNotExist() {
when(theoremRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity<TheoremDto> responseEntity = theoremController.updateTheorem(1, createTheorem(), bindingResult);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verify(theoremRepository, times(0)).save(any(TheoremDto.class));
}
@Test
public void testDeleteTheoremById() {
doNothing().when(theoremRepository).deleteById(anyInt());
final ResponseEntity responseEntity = theoremController.deleteTheoremById(1);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
verify(theoremRepository).deleteById(anyInt());
}
@Test
public void testDeleteTheoremById_idIsNull() {
final ResponseEntity responseEntity = theoremController.deleteTheoremById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(theoremRepository);
}
private TheoremDto createTheorem() {
final List<String> referencedTheoremsList = new ArrayList<>();
referencedTheoremsList.add("test theorem 1");
referencedTheoremsList.add("test theorem 2");
final List<String> referencedDefinitionsList = new ArrayList<>();
referencedDefinitionsList.add("test definition 1");
referencedDefinitionsList.add("test definition 2");
final TheoremDto theoremDto = new TheoremDto();
theoremDto.setName("Test theorem");
theoremDto.setBranch("Test branch");
theoremDto.setProvenStatus(true);
theoremDto.setTheoremType(TheoremType.THEOREM);
theoremDto.setReferencedTheorems(referencedTheoremsList);
theoremDto.setReferencedDefinitions(referencedDefinitionsList);
return theoremDto;
}
}