Skip to content

Commit

Permalink
Merge branch 'master' into PUB-2574c
Browse files Browse the repository at this point in the history
  • Loading branch information
NatashaAlker authored Dec 13, 2024
2 parents 9460dc6 + ed646e9 commit c3bfabd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 64 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ dependencies {
implementation group: 'com.opencsv', name: 'opencsv', version: '5.9'
implementation group: 'commons-validator', name: 'commons-validator', version: '1.9.0'

implementation group: 'com.github.hmcts', name: 'pip-data-models', version: '2.1.32', {
implementation group: 'com.github.hmcts', name: 'pip-data-models', version: '2.1.33', {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-data-jpa'
}
implementation group: 'io.hypersistence', name: 'hypersistence-utils-hibernate-63', version: '3.8.3'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import uk.gov.hmcts.reform.pip.account.management.model.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.report.AccountMiData;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.http.HttpStatus.FORBIDDEN;
import static org.springframework.http.HttpStatus.NOT_FOUND;
Expand Down Expand Up @@ -59,11 +62,10 @@ class CustomAccountRetrievalTest {
private static final String NOT_FOUND_STATUS_CODE_MESSAGE = "Status code does not match not found";
private static final String USER_SHOULD_MATCH = "Users should match";
private static final String FORBIDDEN_STATUS_CODE = "Status code does not match forbidden";
private static final String VALIDATION_MI_REPORT = "Should successfully retrieve MI data";

private static final String UNAUTHORIZED_ROLE = "APPROLE_unknown.authorized";
private static final String UNAUTHORIZED_USERNAME = "unauthorized_isAuthorized";
private static final String EXPECTED_HEADERS = "user_id,provenance_user_id,user_provenance,roles,"
+ "created_date,last_signed_in_date";

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final PiUser VALID_USER = createUser(true, UUID.randomUUID().toString());
Expand Down Expand Up @@ -106,22 +108,23 @@ void testMiAccountDataRequestSuccess() throws Exception {
}
);

String createdUserId = mappedResponse.get(CreationEnum.CREATED_ACCOUNTS).get(0).toString();
String createdUserId = mappedResponse.get(CreationEnum.CREATED_ACCOUNTS).getFirst().toString();

MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.get(MI_REPORTING_ACCOUNT_DATA_URL);

String responseMiData = mockMvc.perform(request).andExpect(status().isOk()).andReturn()
.getResponse().getContentAsString();
MvcResult responseMiData = mockMvc.perform(request).andExpect(status().isOk()).andReturn();

assertEquals(EXPECTED_HEADERS, responseMiData.split("\n")[0],
"Should successfully retrieve MI data headers"
);
assertNotNull(responseMiData.getResponse(), VALIDATION_MI_REPORT);

assertTrue(
responseMiData.contains(createdUserId),
"Should successfully retrieve MI data"
List<AccountMiData> miData = Arrays.asList(
OBJECT_MAPPER.readValue(
responseMiData.getResponse().getContentAsString(),
AccountMiData[].class
)
);

assertEquals(createdUserId, miData.getLast().getUserId().toString(), VALIDATION_MI_REPORT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package uk.gov.hmcts.reform.pip.account.management.controllers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -12,7 +9,6 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -26,6 +22,7 @@
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.authentication.roles.IsAdmin;
import uk.gov.hmcts.reform.pip.model.report.AccountMiData;

import java.util.List;

Expand All @@ -50,19 +47,11 @@ public AccountFilteringController(AccountFilteringService accountFilteringServic
this.accountFilteringService = accountFilteringService;
}

@ApiResponse(responseCode = OK_CODE, description = "A CSV like structure which contains the data. "
+ "See example for headers ", content = {
@Content(examples = {@ExampleObject("user_id,provenance_user_id,user_provenance,roles,"
+ "created_date,last_signed_in_date")},
mediaType = MediaType.TEXT_PLAIN_VALUE,
schema = @Schema(implementation = String.class))
}
)
@Operation(summary = "Returns a list of (anonymized) account data for MI reporting. This endpoint will be "
+ "deprecated in the future, in favour of returning a JSON model")
@ApiResponse(responseCode = OK_CODE, description = "A JSON model which contains the data. ")
@Operation(summary = "Returns (anonymized) account data for MI reporting")
@GetMapping("/mi-data")
public ResponseEntity<String> getMiData() {
return ResponseEntity.status(HttpStatus.OK).body(accountFilteringService.getAccManDataForMiReporting());
public ResponseEntity<List<AccountMiData>> getMiData() {
return ResponseEntity.status(HttpStatus.OK).body(accountFilteringService.getAccountDataForMi());
}

@ApiResponse(responseCode = OK_CODE, description = "List of third party accounts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uk.gov.hmcts.reform.pip.account.management.model.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.report.AccountMiData;

import java.util.List;
import java.util.Optional;
Expand All @@ -24,10 +25,10 @@ List<PiUser> findExistingByProvenanceId(@Param("provUserId") String provenanceUs

Optional<PiUser> findByUserId(UUID userId);

@Query(value = "SELECT cast(user_id as text), provenance_user_id, user_provenance, roles, created_date, "
+ "last_signed_in_date FROM pi_user",
nativeQuery = true)
List<String> getAccManDataForMI();
@Query("SELECT new uk.gov.hmcts.reform.pip.model.report.AccountMiData("
+ "u.userId, u.provenanceUserId, u.userProvenance, u.roles, u.createdDate, u.lastSignedInDate) "
+ "FROM PiUser u")
List<AccountMiData> getAccountDataForMi();

@Query(value = "SELECT * FROM pi_user WHERE CAST(last_verified_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :daysAgo AND user_provenance = 'PI_AAD' AND roles = 'VERIFIED'", nativeQuery = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uk.gov.hmcts.reform.pip.account.management.model.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.report.AccountMiData;

import java.util.List;

Expand All @@ -26,13 +27,13 @@ public AccountFilteringService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public String getAccManDataForMiReporting() {
StringBuilder builder = new StringBuilder(85);
builder.append("user_id,provenance_user_id,user_provenance,roles,created_date,last_signed_in_date")
.append(System.lineSeparator());
userRepository.getAccManDataForMI()
.forEach(line -> builder.append(line).append(System.lineSeparator()));
return builder.toString();
/**
* Method which will retrieve MI reporting data for all accounts.
*
* @return A list of MI Data objects for all accounts.
*/
public List<AccountMiData> getAccountDataForMi() {
return userRepository.getAccountDataForMi();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
import uk.gov.hmcts.reform.pip.account.management.model.PiUser;
import uk.gov.hmcts.reform.pip.model.account.Roles;
import uk.gov.hmcts.reform.pip.model.account.UserProvenances;
import uk.gov.hmcts.reform.pip.model.report.AccountMiData;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -31,18 +32,18 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.hmcts.reform.pip.model.account.Roles.ALL_NON_RESTRICTED_ADMIN_ROLES;
import static uk.gov.hmcts.reform.pip.model.account.Roles.INTERNAL_ADMIN_CTSC;
import static uk.gov.hmcts.reform.pip.model.account.UserProvenances.PI_AAD;
import static uk.gov.hmcts.reform.pip.model.account.UserProvenances.SSO;

@ExtendWith(MockitoExtension.class)
class AccountFilteringServiceTest {
private static final String EMAIL = "test@hmcts.net";
private static final String ID = "1234";
private static final List<String> EXAMPLE_CSV = List.of(
"2fe899ff-96ed-435a-bcad-1411bbe96d2a,string,CFT_IDAM,INTERNAL_ADMIN_CTSC,2022-01-19 13:45:50.873778,"
+ "2023-01-25 14:22:56.434343");
private static final UUID USER_ID = UUID.randomUUID();
private static final LocalDateTime CREATED_DATE = LocalDateTime.of(2022,1, 19, 13, 45, 50);
private static final LocalDateTime LAST_SIGNED_IN = LocalDateTime.of(2023,1, 25, 14, 22, 43);
private static final PiUser PI_USER = new PiUser();

private static final String USER_NOT_FOUND_EXCEPTION_MESSAGE =
"The exception when a user has not been found has been thrown";
private static final String RETURN_USER_ERROR = "Returned user does not match expected user";
Expand All @@ -63,26 +64,14 @@ static void setup() {

@Test
void testMiService() {
when(userRepository.getAccManDataForMI()).thenReturn(EXAMPLE_CSV);
String testString = accountFilteringService.getAccManDataForMiReporting();

assertThat(testString)
.as("Json parsing has probably failed")
.contains("CTSC")
.hasLineCount(2);

String[] splitLineString = testString.split("(\r\n|\r|\n)");
long countLine1 = splitLineString[0].chars().filter(character -> character == ',').count();

assertThat(splitLineString[0])
.as("Header row does not match")
.isEqualTo("user_id,provenance_user_id,user_provenance,roles,created_date,last_signed_in_date");

assertThat(splitLineString)
.as("Data must be missing, are only headers printing? or Wrong data")
.hasSizeGreaterThanOrEqualTo(2)
.allSatisfy(
e -> assertThat(e.chars().filter(character -> character == ',').count()).isEqualTo(countLine1));
AccountMiData miRecord = new AccountMiData(USER_ID, ID, PI_AAD, INTERNAL_ADMIN_CTSC,
CREATED_DATE, LAST_SIGNED_IN);
List<AccountMiData> miData = List.of(miRecord, miRecord);

when(userRepository.getAccountDataForMi()).thenReturn(miData);
List<AccountMiData> miReportData = accountFilteringService.getAccountDataForMi();

assertEquals(miData, miReportData, "Returned data does not match expected data");
}

@Test
Expand Down

0 comments on commit c3bfabd

Please sign in to comment.