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:
+31
@@ -10,6 +10,7 @@ import org.springframework.util.StopWatch;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@@ -37,6 +38,36 @@ public class ProofController {
|
|||||||
return new ResponseEntity<>(listOfProofs, HttpStatus.OK);
|
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}")
|
@GetMapping("/{branch}")
|
||||||
public @ResponseBody
|
public @ResponseBody
|
||||||
ResponseEntity<List<ProofDto>> getAllProofsByBranch(@PathVariable("branch") final String branch) {
|
ResponseEntity<List<ProofDto>> getAllProofsByBranch(@PathVariable("branch") final String branch) {
|
||||||
|
|||||||
+40
-6
@@ -11,16 +11,13 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertTrue;
|
import static junit.framework.TestCase.assertTrue;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Matchers.anyInt;
|
||||||
import static org.mockito.Matchers.anyString;
|
import static org.mockito.Matchers.anyString;
|
||||||
import static org.mockito.Mockito.verifyZeroInteractions;
|
import static org.mockito.Mockito.*;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class ProofControllerTest {
|
public class ProofControllerTest {
|
||||||
@@ -87,6 +84,43 @@ public class ProofControllerTest {
|
|||||||
assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
|
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() {
|
private ProofDto createProof() {
|
||||||
final List<String> referencedTheoremsList = new ArrayList<>();
|
final List<String> referencedTheoremsList = new ArrayList<>();
|
||||||
referencedTheoremsList.add("test theorem 1");
|
referencedTheoremsList.add("test theorem 1");
|
||||||
|
|||||||
Reference in New Issue
Block a user