Skip to content

Commit

Permalink
adds endpoint to get application form status
Browse files Browse the repository at this point in the history
  • Loading branch information
rachelswart committed Mar 8, 2024
1 parent 56df1aa commit b2f2b63
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ public ResponseEntity<String> getLastUpdatedEmail(@PathVariable final Integer ap
return ResponseEntity.ok(email);
}

@GetMapping("/{applicationId}/status")
@CheckSchemeOwnership
public ResponseEntity<String> getApplicationStatus(@PathVariable final Integer applicationId) {
final ApplicationStatusEnum applicationStatus = applicationFormService.getApplicationStatus(applicationId);
return ResponseEntity.ok(applicationStatus.toString());
}


private void logApplicationEvent(EventType eventType, String sessionId, String applicationId) {

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import gov.cabinetoffice.gap.adminbackend.repositories.TemplateApplicationFormRepository;
import gov.cabinetoffice.gap.adminbackend.utils.ApplicationFormUtils;
import gov.cabinetoffice.gap.adminbackend.utils.HelperUtils;
import static java.lang.Integer.parseInt;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
Expand All @@ -36,6 +35,8 @@
import java.time.Instant;
import java.util.*;

import static java.lang.Integer.parseInt;

@Service
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -381,6 +382,11 @@ public Integer getLastUpdatedBy(Integer applicationId) {
return this.applicationFormRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException("Application with id " + applicationId + " does not exist"))
.getLastUpdateBy();
}

public ApplicationStatusEnum getApplicationStatus(Integer applicationId) {
return this.applicationFormRepository.findById(applicationId)
.orElseThrow(() -> new NotFoundException("Application with id " + applicationId + " does not exist"))
.getApplicationStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,12 @@
import java.util.List;
import java.util.Optional;

import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_ADVERT_ID;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_FORM_DTO;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_FORM_EXISTS_DTO_MULTIPLE_PROPS;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_FORM_EXISTS_DTO_SINGLE_PROP;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_ID;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_NAME;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_POST_FORM_DTO;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_APPLICATION_RESPONSE_SUCCESS;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_CLASS_ERROR_NO_PROPS_PROVIDED;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_PATCH_APPLICATION_DTO;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_PATCH_UPDATED_APPLICATION_DTO;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.SAMPLE_SCHEME_ID;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.*;
import static gov.cabinetoffice.gap.adminbackend.testdata.generators.RandomApplicationFormGenerators.randomApplicationFormFound;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -463,4 +441,21 @@ void getLastUpdatedEmailReturnsNotFoundWhenNoGrantAdminFound() throws Exception
when(userService.getGrantAdminById(anyInt())).thenReturn(Optional.empty());
this.mockMvc.perform(get("/application-forms/1/lastUpdated/email")).andExpect(status().isNotFound());
}

@Test
void getApplicationStatus() throws Exception {
when(applicationFormService.getApplicationStatus(anyInt())).thenReturn(ApplicationStatusEnum.PUBLISHED);

this.mockMvc.perform(get("/application-forms/1/status"))
.andExpect(status().isOk())
.andExpect(content().string(ApplicationStatusEnum.PUBLISHED.toString()));
}

@Test
void getApplicationStatusNotFoundException() throws Exception {
when(applicationFormService.getApplicationStatus(anyInt())).thenThrow(new NotFoundException());

this.mockMvc.perform(get("/application-forms/1/status"))
.andExpect(status().isNotFound());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@
import gov.cabinetoffice.gap.adminbackend.mappers.ApplicationFormMapperImpl;
import gov.cabinetoffice.gap.adminbackend.repositories.ApplicationFormRepository;
import gov.cabinetoffice.gap.adminbackend.repositories.TemplateApplicationFormRepository;
import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.*;
import static gov.cabinetoffice.gap.adminbackend.testdata.generators.RandomApplicationFormGenerators.randomApplicationFormEntity;
import static gov.cabinetoffice.gap.adminbackend.testdata.generators.RandomApplicationFormGenerators.randomApplicationFormFound;
import gov.cabinetoffice.gap.adminbackend.testdata.projectionimpls.TestApplicationFormsFoundView;
import gov.cabinetoffice.gap.adminbackend.utils.ApplicationFormUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Fail.fail;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.*;
import static org.mockito.Mockito.*;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import javax.persistence.EntityNotFoundException;
Expand All @@ -39,6 +31,14 @@
import javax.validation.Validator;
import java.util.*;

import static gov.cabinetoffice.gap.adminbackend.testdata.ApplicationFormTestData.*;
import static gov.cabinetoffice.gap.adminbackend.testdata.generators.RandomApplicationFormGenerators.randomApplicationFormEntity;
import static gov.cabinetoffice.gap.adminbackend.testdata.generators.RandomApplicationFormGenerators.randomApplicationFormFound;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Fail.fail;
import static org.mockito.Mockito.*;

@SpringJUnitConfig
@WithAdminSession
class ApplicationFormServiceTest {
Expand Down Expand Up @@ -842,5 +842,38 @@ void updateSectionOrderUnauthorised() {

}

@Nested
class getApplicationStatus {

@Test
void getApplicationStatusSuccessful() {
final Integer applicationId =1;
final ApplicationStatusEnum expectedStatus = ApplicationStatusEnum.PUBLISHED;
ApplicationFormEntity applicationForm = ApplicationFormEntity.builder()
.grantApplicationId(applicationId)
.applicationStatus(expectedStatus)
.build();

when(applicationFormRepository.findById(anyInt())).thenReturn(Optional.of(applicationForm));

final ApplicationStatusEnum response = applicationFormService.getApplicationStatus(applicationId);

verify(applicationFormRepository).findById(applicationId);
assertThat(response).isEqualTo(expectedStatus);

}

@Test
void getApplicationStatusNotFoundException() {
final Integer applicationId = 1;
when(applicationFormRepository.findById(anyInt())).thenReturn(Optional.empty());
assertThatThrownBy(() -> applicationFormService.getApplicationStatus(applicationId))
.isInstanceOf(NotFoundException.class)
.hasMessage("Application with id " + applicationId + " does not exist");

}

}


}

0 comments on commit b2f2b63

Please sign in to comment.