Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

story(ccls-1956) get/put application correspondence address #19

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/pr-merge-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ permissions:

jobs:
build-test-release:
if: github.event.pull_request.merged == true
if: |
github.event.pull_request.merged == true && !startsWith(github.event.pull_request.head.ref, 'dependabot/')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
87 changes: 76 additions & 11 deletions caab-api/open-api-specification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ paths:
schema:
$ref: '#/components/schemas/applicationDetail'
parameters:
- name: 'Caab-user-Login-Id'
- name: 'Caab-User-Login-Id'
in: header
required: true
schema:
Expand Down Expand Up @@ -91,13 +91,13 @@ paths:
description: 'Not found'
'500':
description: 'Internal server error'
patch:
put:
tags:
- applications
summary: 'patch application type'
operationId: 'patchApplicationType'
summary: 'put application type'
operationId: 'putApplicationType'
requestBody:
description: patch an application's application type
description: put an application's application type
content:
application/json:
schema:
Expand All @@ -110,7 +110,7 @@ paths:
type: 'integer'
format: 'int64'
example: '1234567890'
- name: 'Caab-user-Login-Id'
- name: 'Caab-User-Login-Id'
in: header
required: true
schema:
Expand Down Expand Up @@ -156,13 +156,13 @@ paths:
description: 'Not found'
'500':
description: 'Internal server error'
patch:
put:
tags:
- applications
summary: 'patch an applications provider details'
operationId: 'patchApplicationProviderDetails'
summary: 'put an applications provider details'
operationId: 'putApplicationProviderDetails'
requestBody:
description: patch an application's application type
description: put an application's application type
porritta marked this conversation as resolved.
Show resolved Hide resolved
content:
application/json:
schema:
Expand All @@ -175,7 +175,72 @@ paths:
type: 'integer'
format: 'int64'
example: '1234567890'
- name: 'Caab-user-Login-Id'
- name: 'Caab-User-Login-Id'
in: header
required: true
schema:
type: 'string'
example: '[email protected]'
porritta marked this conversation as resolved.
Show resolved Hide resolved
responses:
'200':
description: 'Successful operation'
'400':
description: 'Bad request'
'401':
description: 'Unauthorized'
'404':
description: 'Not found'
'500':
description: 'Internal server error'
/applications/{id}/correspondence-address:
get:
tags:
- applications
summary: 'get an applications correspondence address'
operationId: 'getApplicationCorrespondenceAddress'
parameters:
- name: 'id'
in: 'path'
required: true
schema:
type: 'integer'
format: 'int64'
example: '1234567890'
responses:
'200':
description: 'Successful operation'
content:
application/json:
schema:
$ref: "#/components/schemas/address"
'400':
description: 'Bad request'
'401':
description: 'Unauthorized'
'404':
description: 'Not found'
'500':
description: 'Internal server error'
put:
tags:
- applications
summary: 'put an applications correspondence address'
operationId: 'putApplicationCorrespondenceAddress'
requestBody:
description: put an application's correspondence address
content:
application/json:
schema:
$ref: '#/components/schemas/address'
parameters:
- name: 'id'
in: 'path'
required: true
schema:
type: 'integer'
format: 'int64'
example: '1234567890'
- name: 'Caab-User-Login-Id'
in: header
required: true
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
import static uk.gov.laa.ccms.caab.api.audit.AuditorAwareImpl.currentUserHolder;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
Expand All @@ -15,6 +16,7 @@
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -46,6 +48,13 @@ public class ApplicationControllerIntegrationTest extends AbstractIntegrationTes
@Autowired
private ApplicationService applicationService;

private final String caabUserLoginId = "[email protected]";

@BeforeEach
public void setup() {
currentUserHolder.set(caabUserLoginId);
}

/**
* Loads the JSON file from the classpath and parses it into an ApplicationDetail object.
*
Expand Down Expand Up @@ -178,8 +187,7 @@ private void assertAuditTrailRecursive(Object object, String[] methodCalls, int
Method method = object.getClass().getMethod(methodCalls[index]);
object = method.invoke(object);

if (object instanceof List) {
List<?> list = (List<?>) object;
if (object instanceof List<?> list) {
for (Object item : list) {
assertAuditTrailRecursive(item, methodCalls, index + 1, expectedAuditUser);
}
Expand Down Expand Up @@ -231,9 +239,8 @@ private static void nullifyFieldRecursive(Object object, String[] fields, int in
return;
}

if (object instanceof List) {
if (object instanceof List<?> list) {
// Handle List objects
List<?> list = (List<?>) object;
for (Object item : list) {
nullifyFieldRecursive(item, fields, index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.BEFORE_TEST_METHOD;
import static org.springframework.test.context.jdbc.SqlMergeMode.MergeMode.MERGE;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup;
import static uk.gov.laa.ccms.caab.api.audit.AuditorAwareImpl.currentUserHolder;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.transaction.Transactional;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.Instant;
Expand All @@ -26,9 +23,7 @@
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlMergeMode;
import uk.gov.laa.ccms.caab.api.CaabApiApplication;
Expand Down Expand Up @@ -327,7 +322,7 @@ public void testGetApplicationType() {

@Test
@Sql(scripts = "/sql/application_insert.sql")
public void testPatchApplicationType() {
public void testPutApplicationType() {
Long applicationId = 24L;
String amendedApplicationTypeId = "APP TYPE";
String amendedApplicationTypeDisplay = "Application Type";
Expand All @@ -338,7 +333,7 @@ public void testPatchApplicationType() {
.displayValue(amendedApplicationTypeDisplay);

// Call the service method
applicationService.patchApplicationType(applicationId, amendedApplicationType);
applicationService.putApplicationType(applicationId, amendedApplicationType);

Application result = applicationRepository.findById(applicationId).orElse(null);

Expand All @@ -355,7 +350,7 @@ public void testPatchApplicationType() {
"41, NewProviderCaseRef3, New Provider Display3, 13579, 24680, FeeEarnerId3, SupervisorId3, ProviderContactId3, ThirdUser"
})
@Sql(scripts = "/sql/application_insert.sql")
public void testPatchProviderDetails(
public void testPutProviderDetails(
Long applicationId,
String newProviderCaseReference,
String newProviderDisplayValue,
Expand Down Expand Up @@ -384,7 +379,7 @@ public void testPatchProviderDetails(
currentUserHolder.set(patchedCaabUserLoginId);

// Call the patchProviderDetails method
applicationService.patchProviderDetails(applicationId, providerDetails);
applicationService.putProviderDetails(applicationId, providerDetails);

// Fetch the application from the database after the update
Application updatedApplication = applicationRepository.findById(applicationId).orElse(null);
Expand All @@ -407,14 +402,14 @@ public void testPatchProviderDetails(

@Test
@Sql(scripts = "/sql/application_insert.sql")
public void testPatchProviderDetails_ApplicationNotFound() {
public void testPutProviderDetails_ApplicationNotFound() {
Long nonExistentApplicationId = 999L;

ApplicationProviderDetails providerDetails = new ApplicationProviderDetails();

// Use assertThrows to check if the method throws the expected exception
CaabApiException exception = assertThrows(CaabApiException.class, () -> {
applicationService.patchProviderDetails(nonExistentApplicationId, providerDetails);
applicationService.putProviderDetails(nonExistentApplicationId, providerDetails);
});

assertEquals(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package uk.gov.laa.ccms.caab.api.advice;

import static uk.gov.laa.ccms.caab.api.audit.AuditorAwareImpl.currentUserHolder;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;

/**
* Controller advice class responsible for setting the current user holder if available.
*/
@ControllerAdvice
public class AuditAdvice {

/**
* Sets the current user holder if available from the request header.
*
* @param caabUserLoginId the caab user login id
*/
@ModelAttribute
public void setCurrentUserHolderIfAvailable(
@RequestHeader(value = "Caab-User-Login-Id", required = false) String caabUserLoginId) {
if (caabUserLoginId != null) {
currentUserHolder.set(caabUserLoginId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package uk.gov.laa.ccms.caab.api.controller;


import static uk.gov.laa.ccms.caab.api.audit.AuditorAwareImpl.currentUserHolder;

import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
Expand All @@ -12,6 +10,7 @@
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import uk.gov.laa.ccms.caab.api.ApplicationsApi;
import uk.gov.laa.ccms.caab.api.service.ApplicationService;
import uk.gov.laa.ccms.caab.model.Address;
import uk.gov.laa.ccms.caab.model.ApplicationDetail;
import uk.gov.laa.ccms.caab.model.ApplicationProviderDetails;
import uk.gov.laa.ccms.caab.model.ApplicationType;
Expand All @@ -30,10 +29,9 @@ public class ApplicationController implements ApplicationsApi {

@Override
public ResponseEntity<Void> createApplication(
final String caabUserLoginId,
final ApplicationDetail applicationDetail) {
final String caabUserLoginId,
final ApplicationDetail applicationDetail) {

currentUserHolder.set(caabUserLoginId);
Long id = applicationService.createApplication(applicationDetail);

URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
Expand All @@ -57,7 +55,17 @@ public ResponseEntity<ApplicationDetail> getApplication(
}

@Override
public ResponseEntity<ApplicationProviderDetails> getApplicationProviderDetails(Long id) {
public ResponseEntity<Address> getApplicationCorrespondenceAddress(
final Long id) {
Address correspondenceAddress =
applicationService.getApplicationCorrespondenceAddress(id);

return new ResponseEntity<>(correspondenceAddress, HttpStatus.OK);
}

@Override
public ResponseEntity<ApplicationProviderDetails> getApplicationProviderDetails(
final Long id) {

ApplicationProviderDetails applicationProviderDetails =
applicationService.getApplicationProviderDetails(id);
Expand All @@ -75,25 +83,34 @@ public ResponseEntity<ApplicationType> getApplicationType(
}

@Override
public ResponseEntity<Void> patchApplicationProviderDetails(
Long id,
String caabUserLoginId,
ApplicationProviderDetails applicationProviderDetails) {
public ResponseEntity<Void> putApplicationCorrespondenceAddress(
final Long id,
final String caabUserLoginId,
final Address address) {

applicationService.putCorrespondenceAddress(id, address);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Override
public ResponseEntity<Void> putApplicationProviderDetails(
final Long id,
final String caabUserLoginId,
final ApplicationProviderDetails applicationProviderDetails) {

currentUserHolder.set(caabUserLoginId);
applicationService.patchProviderDetails(id, applicationProviderDetails);
applicationService.putProviderDetails(id, applicationProviderDetails);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Override
public ResponseEntity<Void> patchApplicationType(
public ResponseEntity<Void> putApplicationType(
final Long id,
final String caabUserLoginId,
final ApplicationType applicationType) {

currentUserHolder.set(caabUserLoginId);
applicationService.patchApplicationType(id, applicationType);
applicationService.putApplicationType(id, applicationType);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
Expand Down
Loading