PAN-52 Added findByName and findByBranch methods to the repository, and created first couple of methods and corresponding tests in the ProofController

This commit is contained in:
2019-03-14 11:51:29 -06:00
parent 7af41d4235
commit d560f5831f
2 changed files with 71 additions and 6 deletions
@@ -10,6 +10,7 @@ import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@Slf4j
@RestController
@@ -37,6 +38,36 @@ public class ProofController {
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
}
@GetMapping("/{id}")
public @ResponseBody
ResponseEntity<ProofDto> getProofById(@PathVariable("id") final Integer id) {
LOG.info("Received request to query for proof with id " + id);
if (id == null) {
LOG.error("ERROR: ID was null");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
LOG.debug("Querying for proof with id " + id);
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final Optional<ProofDto> proof = proofRepository.findById(id);
stopWatch.stop();
LOG.debug("Received response from server: query took " + stopWatch.getTotalTimeMillis() + "ms to complete");
return proof.map(proofDto -> {
LOG.info("Returning proof with id " + id);
return new ResponseEntity<>(proofDto, HttpStatus.OK);
}).orElseGet(
() -> {
LOG.warn("No proof was found with id " + id);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
});
}
@GetMapping("/{branch}")
public @ResponseBody
ResponseEntity<List<ProofDto>> getAllProofsByBranch(@PathVariable("branch") final String branch) {
@@ -11,16 +11,13 @@ 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.Date;
import java.util.List;
import java.util.*;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class ProofControllerTest {
@@ -87,6 +84,43 @@ public class ProofControllerTest {
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
}
@Test
public void testGetProofById() {
final ProofDto proofDto = createProof();
when(proofRepository.findById(anyInt())).thenReturn(Optional.ofNullable(proofDto));
final ResponseEntity<ProofDto> responseEntity = proofController.getProofById(1);
assertNotNull(responseEntity);
assertTrue(responseEntity.hasBody());
assertNotNull(responseEntity.getBody());
assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
assertEquals(proofDto, responseEntity.getBody());
verify(proofRepository).findById(anyInt());
}
@Test
public void testGetProofById_nullId() {
final ResponseEntity responseEntity = proofController.getProofById(null);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
verifyZeroInteractions(proofRepository);
}
@Test
public void testGetProofById_noProofFound() {
when(proofRepository.findById(anyInt())).thenReturn(Optional.empty());
final ResponseEntity responseEntity = proofController.getProofById(1);
assertNotNull(responseEntity);
assertFalse(responseEntity.hasBody());
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
verify(proofRepository).findById(anyInt());
}
private ProofDto createProof() {
final List<String> referencedTheoremsList = new ArrayList<>();
referencedTheoremsList.add("test theorem 1");