Skip to content

Commit

Permalink
Merge pull request #23 from cabinetoffice/develop
Browse files Browse the repository at this point in the history
Merging latest from develop
  • Loading branch information
dominicwest authored Aug 28, 2023
2 parents fa8b316 + 39f2823 commit 17172ed
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public ResponseEntity getSubmissionInfo(final @PathVariable @NotNull UUID submis

try {
final LambdaSubmissionDefinition submission = submissionsService.getSubmissionInfo(submissionId,
batchExportId);
batchExportId, authHeader);
return ResponseEntity.ok(submission);
}
catch (NotFoundException e) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/gov/cabinetoffice/gap/adminbackend/dtos/UserDTO.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package gov.cabinetoffice.gap.adminbackend.dtos;

import io.micrometer.core.lang.Nullable;
import lombok.Builder;
import lombok.Data;

import java.time.Instant;

@Data
@Builder
public class UserDTO {

private String firstName;
Expand All @@ -13,4 +18,11 @@ public class UserDTO {

private String emailAddress;

private String gapUserId;

private String sub;

@Nullable
private Instant created;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public class LambdaSubmissionDefinition {

private List<SubmissionSection> sections;

private String userId;
private String email;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface SubmissionMapper {
@Mapping(source = "definition.sections", target = "sections")
@Mapping(source = "scheme.id", target = "schemeId")
@Mapping(source = "scheme.name", target = "schemeName")
@Mapping(source = "createdBy.userId", target = "userId")
LambdaSubmissionDefinition submissionToLambdaSubmissionDefinition(Submission submission);

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
package gov.cabinetoffice.gap.adminbackend.services;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.MessageAttributeValue;
import com.amazonaws.services.sqs.model.SendMessageBatchRequest;
import com.amazonaws.services.sqs.model.SendMessageBatchRequestEntry;
import com.google.common.collect.Lists;
import gov.cabinetoffice.gap.adminbackend.constants.AWSConstants;
import gov.cabinetoffice.gap.adminbackend.dtos.UserDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.application.ApplicationFormDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.submission.LambdaSubmissionDefinition;
import gov.cabinetoffice.gap.adminbackend.dtos.submission.SubmissionExportsDTO;
Expand All @@ -39,11 +27,23 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayOutputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

@Service
@RequiredArgsConstructor
Expand All @@ -69,9 +69,14 @@ public class SubmissionsService {

private final SubmissionMapper submissionMapper;

private final RestTemplate restTemplate;

@Value("${cloud.aws.sqs.submissions-export-queue}")
private String submissionsExportQueue;

@Value("${user-service.domain}")
private String userServiceUrl;

public ByteArrayOutputStream exportSpotlightChecks(Integer applicationId) {
AdminSession adminSession = HelperUtils.getAdminSessionForAuthenticatedUser();

Expand Down Expand Up @@ -305,16 +310,32 @@ private String getFilenameFromExportsSignedUrl(GrantExportEntity exportEntity) {

}

public LambdaSubmissionDefinition getSubmissionInfo(final UUID submissionId, final UUID exportBatchId) {
public LambdaSubmissionDefinition getSubmissionInfo(final UUID submissionId, final UUID exportBatchId,
final String authHeader) {

if (!grantExportRepository
.existsById(GrantExportId.builder().exportBatchId(exportBatchId).submissionId(submissionId).build())) {
throw new NotFoundException();
}

final Submission submission = submissionRepository.findById(submissionId).orElseThrow(NotFoundException::new);
final Submission submission = submissionRepository.findByIdWithApplicant(submissionId)
.orElseThrow(NotFoundException::new);
final String userId = submission.getApplicant().getUserId();
final String email = getEmailFromUserId(userId, authHeader);

final LambdaSubmissionDefinition lambdaSubmissionDefinition = submissionMapper
.submissionToLambdaSubmissionDefinition(submission);
lambdaSubmissionDefinition.setEmail(email);
return lambdaSubmissionDefinition;
}

return submissionMapper.submissionToLambdaSubmissionDefinition(submission);
private String getEmailFromUserId(final String userId, final String authHeader) {
final String url = userServiceUrl + "/user?userSub=" + userId;
final HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.add("Authorization", authHeader);
HttpEntity<?> httpEntity = new HttpEntity<>(requestHeaders);
final ResponseEntity<UserDTO> user = restTemplate.exchange(url, HttpMethod.GET, httpEntity, UserDTO.class);
return user.getBody().getEmailAddress();
}

public void updateExportStatus(String submissionId, String batchExportId, GrantExportStatus status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void beforeEach() {
@Test
void happyPath() throws Exception {
final LambdaSubmissionDefinition lambdaSubmissionDefinition = LambdaSubmissionDefinition.builder().build();
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class)))
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class), anyString()))
.thenReturn(lambdaSubmissionDefinition);

mockMvc.perform(
Expand All @@ -194,7 +194,7 @@ void happyPath() throws Exception {

@Test
void unauthorisedPath() throws Exception {
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class)))
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class), anyString()))
.thenThrow(new UnauthorizedException());

mockMvc.perform(
Expand All @@ -205,7 +205,7 @@ void unauthorisedPath() throws Exception {

@Test
void resourceNotFoundPath() throws Exception {
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class)))
when(submissionsService.getSubmissionInfo(any(UUID.class), any(UUID.class), anyString()))
.thenThrow(new NotFoundException());

mockMvc.perform(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.AmazonSQSException;
import gov.cabinetoffice.gap.adminbackend.annotations.WithAdminSession;
import gov.cabinetoffice.gap.adminbackend.dtos.UserDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.application.ApplicationAuditDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.application.ApplicationFormDTO;
import gov.cabinetoffice.gap.adminbackend.dtos.submission.*;
Expand All @@ -29,8 +30,11 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -76,6 +80,9 @@ class SubmissionsServiceTest {
@Spy
private SubmissionMapper submissionMapper = new SubmissionMapperImpl();

@Mock
private RestTemplate restTemplate;

private final List<String> EXPECTED_SPOTLIGHT_ROW = Arrays.asList("GAP-LL-20220927-1", "Some company name",
"9-10 St Andrew Square", "Edinburgh", "EH2 2AF", "500", "12738494", "Yes", "");

Expand Down Expand Up @@ -437,6 +444,8 @@ void whenExportRecordsComplete_returnComplete() {
@Nested
class getSubmissionInfo {

final String authHeader = "randomAuthHeader";

@Test
void happyPath() {
final ZonedDateTime zonedDateTime = ZonedDateTime.now();
Expand All @@ -449,16 +458,21 @@ void happyPath() {
.build())
.scheme(SchemeEntity.builder().id(1).name("testSchemeName").build()).submittedDate(zonedDateTime)
.build();
final UserDTO userDTO = UserDTO.builder().emailAddress("testEmailAddress").build();

when(grantExportRepository.existsById(any(GrantExportId.class))).thenReturn(true);
when(submissionRepository.findById(any(UUID.class))).thenReturn(Optional.of(submission));
when(submissionRepository.findByIdWithApplicant(any(UUID.class))).thenReturn(Optional.of(submission));
when(submissionMapper.submissionToLambdaSubmissionDefinition(any(Submission.class))).thenCallRealMethod();
when(restTemplate.exchange(anyString(), any(), any(), eq(UserDTO.class)))
.thenReturn(new ResponseEntity<>(userDTO, HttpStatus.OK));

final LambdaSubmissionDefinition lambdaSubmissionDefinition = submissionsService
.getSubmissionInfo(UUID.randomUUID(), UUID.randomUUID());
final LambdaSubmissionDefinition actual = submissionsService.getSubmissionInfo(UUID.randomUUID(),
UUID.randomUUID(), authHeader);
final LambdaSubmissionDefinition expected = submissionMapper
.submissionToLambdaSubmissionDefinition(submission);
expected.setEmail(userDTO.getEmailAddress());

assertEquals(submissionMapper.submissionToLambdaSubmissionDefinition(submission),
lambdaSubmissionDefinition);
assertEquals(expected, actual);
}

@Test
Expand All @@ -467,16 +481,18 @@ void resourceNotFoundPath() {
when(submissionRepository.findByIdWithApplicant(any(UUID.class))).thenReturn(Optional.empty());
when(submissionMapper.submissionToLambdaSubmissionDefinition(any(Submission.class))).thenCallRealMethod();

assertThatThrownBy(() -> submissionsService.getSubmissionInfo(UUID.randomUUID(), UUID.randomUUID()))
.isInstanceOf(NotFoundException.class);
assertThatThrownBy(
() -> submissionsService.getSubmissionInfo(UUID.randomUUID(), UUID.randomUUID(), authHeader))
.isInstanceOf(NotFoundException.class);
}

@Test
void unauthorisedPath() {
when(grantExportRepository.existsById(any(GrantExportId.class))).thenReturn(false);

assertThatThrownBy(() -> submissionsService.getSubmissionInfo(UUID.randomUUID(), UUID.randomUUID()))
.isInstanceOf(NotFoundException.class);
assertThatThrownBy(
() -> submissionsService.getSubmissionInfo(UUID.randomUUID(), UUID.randomUUID(), authHeader))
.isInstanceOf(NotFoundException.class);
}

}
Expand Down

0 comments on commit 17172ed

Please sign in to comment.