generated from hmcts/spring-boot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into S28-3356/schema-edit-requests
- Loading branch information
Showing
22 changed files
with
976 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...integrationTest/java/uk/gov/hmcts/reform/preapi/services/TermsAndConditionsServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package uk.gov.hmcts.reform.preapi.services; | ||
|
||
import jakarta.transaction.Transactional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import uk.gov.hmcts.reform.preapi.entities.TermsAndConditions; | ||
import uk.gov.hmcts.reform.preapi.enums.TermsAndConditionsType; | ||
import uk.gov.hmcts.reform.preapi.exception.NotFoundException; | ||
import uk.gov.hmcts.reform.preapi.util.HelperFactory; | ||
import uk.gov.hmcts.reform.preapi.utils.IntegrationTestBase; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class TermsAndConditionsServiceIT extends IntegrationTestBase { | ||
@Autowired | ||
private TermsAndConditionsService termsAndConditionsService; | ||
|
||
private TermsAndConditions termsAndConditionsApp; | ||
private TermsAndConditions termsAndConditionsPortal; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
termsAndConditionsApp = HelperFactory.createTermsAndConditions(TermsAndConditionsType.APP, "app content"); | ||
entityManager.persist(termsAndConditionsApp); | ||
|
||
termsAndConditionsPortal = HelperFactory.createTermsAndConditions( | ||
TermsAndConditionsType.PORTAL, | ||
"portal content" | ||
); | ||
entityManager.persist(termsAndConditionsPortal); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void getLatestTermsAndConditionsApp() { | ||
var latestAppTerms1 = termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.APP); | ||
|
||
assertThat(latestAppTerms1.getId()).isEqualTo(termsAndConditionsApp.getId()); | ||
|
||
var newTerms = HelperFactory.createTermsAndConditions(TermsAndConditionsType.APP, "app content 2"); | ||
entityManager.persist(newTerms); | ||
|
||
var latestAppTerms2 = termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.APP); | ||
assertThat(latestAppTerms2.getId()).isEqualTo(newTerms.getId()); | ||
assertThat(latestAppTerms2.getCreatedAt()).isAfter(latestAppTerms1.getCreatedAt()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void getLatestTermsAndConditionsPortal() { | ||
var latestPortalTerms1 = termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.PORTAL); | ||
|
||
assertThat(latestPortalTerms1.getId()).isEqualTo(termsAndConditionsPortal.getId()); | ||
|
||
var newTerms = HelperFactory.createTermsAndConditions(TermsAndConditionsType.PORTAL, "portal content 2"); | ||
entityManager.persist(newTerms); | ||
|
||
var latestPortalTerms2 = termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.PORTAL); | ||
assertThat(latestPortalTerms2.getId()).isEqualTo(newTerms.getId()); | ||
assertThat(latestPortalTerms2.getCreatedAt()).isAfter(latestPortalTerms1.getCreatedAt()); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void getLatestTermsAndConditionsNotFound() { | ||
// delete all entities | ||
entityManager.clear(); | ||
entityManager.flush(); | ||
|
||
var message1 = assertThrows( | ||
NotFoundException.class, | ||
() -> termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.APP) | ||
); | ||
assertThat(message1.getMessage()).isEqualTo("Not found: Terms and conditions of type: APP"); | ||
|
||
var message2 = assertThrows( | ||
NotFoundException.class, | ||
() -> termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.PORTAL) | ||
); | ||
assertThat(message2.getMessage()).isEqualTo("Not found: Terms and conditions of type: PORTAL"); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/integrationTest/java/uk/gov/hmcts/reform/preapi/services/UserTermsAcceptedServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package uk.gov.hmcts.reform.preapi.services; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import uk.gov.hmcts.reform.preapi.entities.TermsAndConditions; | ||
import uk.gov.hmcts.reform.preapi.entities.User; | ||
import uk.gov.hmcts.reform.preapi.enums.TermsAndConditionsType; | ||
import uk.gov.hmcts.reform.preapi.util.HelperFactory; | ||
import uk.gov.hmcts.reform.preapi.utils.IntegrationTestBase; | ||
|
||
import java.time.Instant; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class UserTermsAcceptedServiceIT extends IntegrationTestBase { | ||
|
||
@Autowired | ||
private UserTermsAcceptedService userTermsAcceptedService; | ||
|
||
private User user; | ||
private TermsAndConditions termsAndConditions; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
user = HelperFactory.createDefaultTestUser(); | ||
user.setId(UUID.randomUUID()); | ||
entityManager.persist(user); | ||
|
||
termsAndConditions = HelperFactory.createTermsAndConditions(TermsAndConditionsType.APP, "Content"); | ||
entityManager.persist(termsAndConditions); | ||
|
||
entityManager.flush(); | ||
} | ||
|
||
@Test | ||
@Transactional | ||
public void acceptTermsAndConditions() { | ||
var mockedUser = mockAdminUser(); | ||
when(mockedUser.getUserId()).thenReturn(user.getId()); | ||
userTermsAcceptedService.acceptTermsAndConditions(termsAndConditions.getId()); | ||
|
||
entityManager.flush(); | ||
entityManager.refresh(user); | ||
|
||
assertThat(user.getUserTermsAccepted()).isNotNull(); | ||
assertThat(user.getUserTermsAccepted()).hasSize(1); | ||
|
||
var termsAccepted = user.getUserTermsAccepted().stream().findFirst().get(); | ||
assertThat(termsAccepted).isNotNull(); | ||
assertThat(termsAccepted.getUser().getId()).isEqualTo(user.getId()); | ||
assertThat(termsAccepted.getTermsAndConditions().getId()).isEqualTo(termsAndConditions.getId()); | ||
// very recently created | ||
assertThat(termsAccepted.getAcceptedAt()).isAfter(Instant.now().minusSeconds(5)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/uk/gov/hmcts/reform/preapi/controllers/TermsAndConditionsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package uk.gov.hmcts.reform.preapi.controllers; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import uk.gov.hmcts.reform.preapi.dto.TermsAndConditionsDTO; | ||
import uk.gov.hmcts.reform.preapi.enums.TermsAndConditionsType; | ||
import uk.gov.hmcts.reform.preapi.services.TermsAndConditionsService; | ||
import uk.gov.hmcts.reform.preapi.services.UserTermsAcceptedService; | ||
|
||
import java.util.UUID; | ||
|
||
@RestController | ||
public class TermsAndConditionsController { | ||
|
||
private final TermsAndConditionsService termsAndConditionsService; | ||
private final UserTermsAcceptedService userTermsAcceptedService; | ||
|
||
@Autowired | ||
public TermsAndConditionsController(TermsAndConditionsService termsAndConditionsService, | ||
UserTermsAcceptedService userTermsAcceptedService) { | ||
this.termsAndConditionsService = termsAndConditionsService; | ||
this.userTermsAcceptedService = userTermsAcceptedService; | ||
} | ||
|
||
@GetMapping("/app-terms-and-conditions/latest") | ||
@Operation( | ||
operationId = "getLatestTermsForApp", | ||
summary = "Get the latest terms and conditions for the app" | ||
) | ||
public ResponseEntity<TermsAndConditionsDTO> getLatestAppTermsAndConditions() { | ||
return ResponseEntity.ok(termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.APP)); | ||
} | ||
|
||
@GetMapping("/portal-terms-and-conditions/latest") | ||
@Operation( | ||
operationId = "getLatestTermsForPortal", | ||
summary = "Get the latest terms and conditions for the portal" | ||
) | ||
public ResponseEntity<TermsAndConditionsDTO> getLatestPortalTermsAndConditions() { | ||
return ResponseEntity.ok(termsAndConditionsService.getLatestTermsAndConditions(TermsAndConditionsType.PORTAL)); | ||
} | ||
|
||
@PostMapping("/accept-terms-and-conditions/{termsId}") | ||
@Operation( | ||
operationId = "acceptTermsAndConditions", | ||
summary = "Accept terms and conditions for a user" | ||
) | ||
@PreAuthorize("hasAnyRole('ROLE_SUPER_USER', 'ROLE_LEVEL_1', 'ROLE_LEVEL_2', 'ROLE_LEVEL_3', 'ROLE_LEVEL_4')") | ||
public ResponseEntity<Void> acceptTermsAndConditions(@PathVariable UUID termsId) { | ||
userTermsAcceptedService.acceptTermsAndConditions(termsId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
} |
Oops, something went wrong.