add to PAN-16

This commit is contained in:
-
2019-03-14 17:04:10 -06:00
parent 139dc943c3
commit c45c0a6108
5 changed files with 421 additions and 0 deletions
@@ -0,0 +1,2 @@
spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
@@ -0,0 +1,84 @@
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.getProvenStatus());
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.getProvenStatus());
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);
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;
}
}
@@ -0,0 +1,5 @@
package edu.msudenver.tsp.persistence.dto;
public enum TheoremType {
THEOREM, PROPOSITION, LEMMA, COROLLARY
}
@@ -0,0 +1,314 @@
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.Collections;
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 testGetAllTheoremsByBranch_noTheoremsFound() {
when(theoremRepository.findByBranch(anyString())).thenReturn(Collections.emptyList());
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByBranch("test nonexistent branch");
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}
@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 testGetAllTheoremsByProvenStatus_noTheoremsFound() {
when(theoremRepository.findByProvenStatus(anyBoolean())).thenReturn(Collections.emptyList());
final ResponseEntity<List<TheoremDto>> responseEntity = theoremController.getAllTheoremsByProvenStatus(false);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}
@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;
}
}
+16
View File
@@ -0,0 +1,16 @@
<%--
Created by IntelliJ IDEA.
User: atusa
Date: 2/1/19
Time: 8:03 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
$END$
</body>
</html>