|
1 | 1 | package ca.bc.gov.oracleapi.endpoint.consep; |
2 | 2 |
|
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
3 | 5 | import static org.mockito.ArgumentMatchers.any; |
| 6 | +import static org.mockito.ArgumentMatchers.anyList; |
4 | 7 | import static org.mockito.ArgumentMatchers.eq; |
5 | 8 | import static org.mockito.Mockito.doNothing; |
6 | 9 | import static org.mockito.Mockito.doThrow; |
|
21 | 24 | import ca.bc.gov.oracleapi.entity.consep.ActivityEntity; |
22 | 25 | import ca.bc.gov.oracleapi.entity.consep.MccReplicateEntity; |
23 | 26 | import ca.bc.gov.oracleapi.entity.consep.idclass.ReplicateId; |
| 27 | +import ca.bc.gov.oracleapi.exception.InvalidTestActivityKeyException; |
24 | 28 | import ca.bc.gov.oracleapi.service.consep.ActivityService; |
25 | 29 | import ca.bc.gov.oracleapi.service.consep.MoistureContentService; |
26 | 30 | import ca.bc.gov.oracleapi.service.consep.TestResultService; |
|
30 | 34 | import java.math.BigDecimal; |
31 | 35 | import java.time.LocalDateTime; |
32 | 36 | import java.time.format.DateTimeFormatter; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.Collections; |
33 | 39 | import java.util.List; |
34 | 40 | import java.util.Optional; |
35 | 41 | import org.junit.jupiter.api.BeforeEach; |
@@ -503,4 +509,171 @@ void deleteReplicate_shouldReturnNotFound() throws Exception { |
503 | 509 | replicateNumber).with(csrf().asHeader()).contentType(MediaType.APPLICATION_JSON)) |
504 | 510 | .andExpect(status().isNotFound()); |
505 | 511 | } |
| 512 | + |
| 513 | + @Test |
| 514 | + @DisplayName("POST /{riaKey}/replicates - should delete multiple replicates") |
| 515 | + void deleteReplicates_shouldSucceed() throws Exception { |
| 516 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 517 | + List<Integer> replicateNumbers = Arrays.asList(1, 2, 3); |
| 518 | + |
| 519 | + doNothing().when(moistureContentService).deleteMccReplicates(riaKey, replicateNumbers); |
| 520 | + |
| 521 | + mockMvc.perform(post("/api/moisture-content-cone/{riaKey}/replicates", riaKey) |
| 522 | + .with(csrf().asHeader()) |
| 523 | + .contentType(MediaType.APPLICATION_JSON) |
| 524 | + .content(objectMapper.writeValueAsString(replicateNumbers))) |
| 525 | + .andExpect(status().isOk()) |
| 526 | + .andExpect(jsonPath("$[0]").value(1)) |
| 527 | + .andExpect(jsonPath("$[1]").value(2)) |
| 528 | + .andExpect(jsonPath("$[2]").value(3)); |
| 529 | + } |
| 530 | + |
| 531 | + @Test |
| 532 | + @DisplayName("POST /{riaKey}/replicates - should return 404 when replicates not found") |
| 533 | + void deleteReplicates_shouldReturnNotFound() throws Exception { |
| 534 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 535 | + List<Integer> replicateNumbers = Arrays.asList(1, 2, 3); |
| 536 | + |
| 537 | + doThrow(new InvalidTestActivityKeyException()) |
| 538 | + .when(moistureContentService).deleteMccReplicates(riaKey, replicateNumbers); |
| 539 | + |
| 540 | + mockMvc.perform(post("/api/moisture-content-cone/{riaKey}/replicates", riaKey) |
| 541 | + .with(csrf().asHeader()) |
| 542 | + .contentType(MediaType.APPLICATION_JSON) |
| 543 | + .content(objectMapper.writeValueAsString(replicateNumbers))) |
| 544 | + .andExpect(status().isNotFound()); |
| 545 | + } |
| 546 | + |
| 547 | + @Test |
| 548 | + @DisplayName("POST /{riaKey}/calculate-average - should calculate average successfully") |
| 549 | + void calculateAverage_shouldSucceed() throws Exception { |
| 550 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 551 | + List<Double> numbers = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0); |
| 552 | + double expectedAverage = 3.0; |
| 553 | + |
| 554 | + when(moistureContentService.calculateAverage(riaKey, numbers)).thenReturn(expectedAverage); |
| 555 | + |
| 556 | + mockMvc.perform(post("/api/moisture-content-cone/{riaKey}/calculate-average", riaKey) |
| 557 | + .with(csrf().asHeader()) |
| 558 | + .contentType(MediaType.APPLICATION_JSON) |
| 559 | + .content(objectMapper.writeValueAsString(numbers))) |
| 560 | + .andExpect(status().isOk()) |
| 561 | + .andExpect(jsonPath("$").value(expectedAverage)); |
| 562 | + } |
| 563 | + |
| 564 | + @Test |
| 565 | + @DisplayName("POST /{riaKey}/calculate-average - should return 400 for null list") |
| 566 | + void calculateAverage_shouldReturnBadRequestForNullList() throws Exception { |
| 567 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 568 | + |
| 569 | + mockMvc.perform(post("/api/moisture-content-cone/{riaKey}/calculate-average", riaKey) |
| 570 | + .with(csrf().asHeader()) |
| 571 | + .contentType(MediaType.APPLICATION_JSON) |
| 572 | + .content("null")) |
| 573 | + .andExpect(status().isBadRequest()); |
| 574 | + } |
| 575 | + |
| 576 | + @Test |
| 577 | + @DisplayName("POST /{riaKey}/calculate-average - should return 500 for service error") |
| 578 | + void calculateAverage_shouldReturnInternalServerError() throws Exception { |
| 579 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 580 | + List<Double> numbers = Arrays.asList(1.0, 2.0, 3.0); |
| 581 | + |
| 582 | + when(moistureContentService.calculateAverage(riaKey, numbers)) |
| 583 | + .thenThrow(new RuntimeException("Unexpected error")); |
| 584 | + |
| 585 | + mockMvc.perform(post("/api/moisture-content-cone/{riaKey}/calculate-average", riaKey) |
| 586 | + .with(csrf().asHeader()) |
| 587 | + .contentType(MediaType.APPLICATION_JSON) |
| 588 | + .content(objectMapper.writeValueAsString(numbers))) |
| 589 | + .andExpect(status().isInternalServerError()); |
| 590 | + } |
| 591 | + |
| 592 | + @Test |
| 593 | + @DisplayName("GET /validate/{riaKey} - should successfully validate data") |
| 594 | + void validateMoistureContentData_shouldSucceed() throws Exception { |
| 595 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 596 | + |
| 597 | + // Mock data |
| 598 | + MoistureContentConesDto mockDto = new MoistureContentConesDto( |
| 599 | + 1, "Sample", "STATUS", new BigDecimal("50.0"), 1, |
| 600 | + "REQ123", "SL123", "ACT", "TST", "Comment", |
| 601 | + LocalDateTime.now(), LocalDateTime.now(), Collections.emptyList() |
| 602 | + ); |
| 603 | + |
| 604 | + when(moistureContentService.getMoistureConeContentData(riaKey)) |
| 605 | + .thenReturn(Optional.of(mockDto)); |
| 606 | + |
| 607 | + doNothing().when(moistureContentService) |
| 608 | + .validateMoistureConeContentData(anyList()); |
| 609 | + |
| 610 | + doNothing().when(activityService) |
| 611 | + .validateActivityData(any(ActivityEntity.class)); |
| 612 | + |
| 613 | + doNothing().when(testResultService) |
| 614 | + .updateTestResultStatusToCompleted(riaKey); |
| 615 | + |
| 616 | + mockMvc.perform(get("/api/moisture-content-cone/validate/{riaKey}", riaKey) |
| 617 | + .with(csrf().asHeader())) |
| 618 | + .andExpect(status().isOk()); |
| 619 | + } |
| 620 | + |
| 621 | + @Test |
| 622 | + @DisplayName("GET /validate/{riaKey} - should return 400 when validation fails") |
| 623 | + void validateMoistureContentData_shouldReturnBadRequest() throws Exception { |
| 624 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 625 | + |
| 626 | + MoistureContentConesDto mockDto = new MoistureContentConesDto( |
| 627 | + 1, "Sample", "STATUS", new BigDecimal("50.0"), 1, |
| 628 | + "REQ123", "SL123", "ACT", "TST", "Comment", |
| 629 | + LocalDateTime.now(), LocalDateTime.now(), Collections.emptyList() |
| 630 | + ); |
| 631 | + |
| 632 | + when(moistureContentService.getMoistureConeContentData(riaKey)) |
| 633 | + .thenReturn(Optional.of(mockDto)); |
| 634 | + |
| 635 | + doThrow(new IllegalArgumentException("Invalid moisture data")) |
| 636 | + .when(moistureContentService) |
| 637 | + .validateMoistureConeContentData(anyList()); |
| 638 | + |
| 639 | + mockMvc.perform(get("/api/moisture-content-cone/validate/{riaKey}", riaKey) |
| 640 | + .with(csrf().asHeader())) |
| 641 | + .andExpect(status().isBadRequest()) |
| 642 | + .andExpect(result -> assertTrue( |
| 643 | + result.getResolvedException() instanceof ResponseStatusException)) |
| 644 | + .andExpect(result -> assertEquals( |
| 645 | + "400 BAD_REQUEST \"Invalid moisture data\"", |
| 646 | + result.getResolvedException().getMessage())); |
| 647 | + } |
| 648 | + |
| 649 | + @Test |
| 650 | + @DisplayName("GET /validate/{riaKey} - should return 400 when activity validation fails") |
| 651 | + void validateMoistureContentData_shouldReturnBadRequestForActivity() throws Exception { |
| 652 | + BigDecimal riaKey = new BigDecimal("1234567890"); |
| 653 | + |
| 654 | + MoistureContentConesDto mockDto = new MoistureContentConesDto( |
| 655 | + 1, "Sample", "STATUS", new BigDecimal("50.0"), 1, |
| 656 | + "REQ123", "SL123", "ACT", "TST", "Comment", |
| 657 | + LocalDateTime.now(), LocalDateTime.now(), Collections.emptyList() |
| 658 | + ); |
| 659 | + |
| 660 | + when(moistureContentService.getMoistureConeContentData(riaKey)) |
| 661 | + .thenReturn(Optional.of(mockDto)); |
| 662 | + |
| 663 | + doNothing().when(moistureContentService) |
| 664 | + .validateMoistureConeContentData(anyList()); |
| 665 | + |
| 666 | + doThrow(new IllegalArgumentException("Invalid activity data")) |
| 667 | + .when(activityService) |
| 668 | + .validateActivityData(any(ActivityEntity.class)); |
| 669 | + |
| 670 | + mockMvc.perform(get("/api/moisture-content-cone/validate/{riaKey}", riaKey) |
| 671 | + .with(csrf().asHeader())) |
| 672 | + .andExpect(status().isBadRequest()) |
| 673 | + .andExpect(result -> assertTrue( |
| 674 | + result.getResolvedException() instanceof ResponseStatusException)) |
| 675 | + .andExpect(result -> assertEquals( |
| 676 | + "400 BAD_REQUEST \"Invalid activity data\"", |
| 677 | + result.getResolvedException().getMessage())); |
| 678 | + } |
506 | 679 | } |
0 commit comments