-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from companieshouse/idva6-388-put-status-v2
Idva6 388 put status v2
- Loading branch information
Showing
29 changed files
with
1,607 additions
and
44 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
4 changes: 3 additions & 1 deletion
4
...ava/uk/gov/companieshouse/accounts/association/AccountsAssociationServiceApplication.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
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
30 changes: 30 additions & 0 deletions
30
src/main/java/uk/gov/companieshouse/accounts/association/configuration/MongoConfig.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,30 @@ | ||
package uk.gov.companieshouse.accounts.association.configuration; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.auditing.DateTimeProvider; | ||
import org.springframework.data.mongodb.config.EnableMongoAuditing; | ||
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener; | ||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; | ||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Optional; | ||
|
||
@Configuration | ||
@EnableMongoRepositories("uk.gov.companieshouse.accounts.association.repositories") | ||
@EnableMongoAuditing(dateTimeProviderRef = "mongodbDatetimeProvider") | ||
public class MongoConfig { | ||
|
||
@Bean | ||
public ValidatingMongoEventListener validatingMongoEventListener( | ||
final LocalValidatorFactoryBean factory) { | ||
return new ValidatingMongoEventListener(factory); | ||
} | ||
|
||
@Bean(name = "mongodbDatetimeProvider") | ||
public DateTimeProvider dateTimeProvider() { | ||
return () -> Optional.of(LocalDateTime.now()); | ||
} | ||
|
||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/uk/gov/companieshouse/accounts/association/controller/ControllerAdvice.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,110 @@ | ||
package uk.gov.companieshouse.accounts.association.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
import uk.gov.companieshouse.accounts.association.AccountsAssociationServiceApplication; | ||
import uk.gov.companieshouse.accounts.association.exceptions.BadRequestRuntimeException; | ||
import uk.gov.companieshouse.accounts.association.exceptions.NotFoundRuntimeException; | ||
import uk.gov.companieshouse.accounts.association.utils.CamelCaseSnakeCase; | ||
import uk.gov.companieshouse.logging.Logger; | ||
import uk.gov.companieshouse.logging.LoggerFactory; | ||
import uk.gov.companieshouse.service.rest.err.Err; | ||
import uk.gov.companieshouse.service.rest.err.Errors; | ||
|
||
@org.springframework.web.bind.annotation.ControllerAdvice | ||
public class ControllerAdvice extends ResponseEntityExceptionHandler { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(AccountsAssociationServiceApplication.applicationNameSpace); | ||
public static final String X_REQUEST_ID = "X-Request-Id"; | ||
|
||
private String getJsonStringFromErrors(String requestId, Errors errors) { | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
try { | ||
return objectMapper.writeValueAsString(errors); | ||
} | ||
catch (IOException e) { | ||
LOG.errorContext(requestId, String.format("Fail to parse Errors object to JSON %s", e.getMessage()), e, null); | ||
return ""; | ||
} | ||
} | ||
|
||
@ExceptionHandler(NotFoundRuntimeException.class) | ||
@ResponseStatus(HttpStatus.NOT_FOUND) | ||
@ResponseBody | ||
public Errors onNotFoundRuntimeException(NotFoundRuntimeException e, HttpServletRequest r) { | ||
String requestId = r.getHeader(X_REQUEST_ID); | ||
|
||
Map<String, Object> contextMap = new HashMap<>(); | ||
contextMap.put("url", r.getRequestURL().toString()); | ||
contextMap.put("query-parameters", r.getQueryString() != null ? "?" + r.getQueryString() : ""); | ||
|
||
LOG.errorContext(requestId, e.getMessage(), null, contextMap); | ||
|
||
Errors errors = new Errors(); | ||
errors.addError(Err.invalidBodyBuilderWithLocation(e.getFieldLocation()).withError(e.getMessage()).build()); | ||
return errors; | ||
} | ||
|
||
@ExceptionHandler(BadRequestRuntimeException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ResponseBody | ||
public Errors onBadRequestRuntimeException(BadRequestRuntimeException e, HttpServletRequest request) { | ||
String requestId = request.getHeader(X_REQUEST_ID); | ||
|
||
Map<String, Object> contextMap = new HashMap<>(); | ||
contextMap.put("url", request.getRequestURL().toString()); | ||
contextMap.put("query-parameters", request.getQueryString() != null ? "?" + request.getQueryString() : ""); | ||
|
||
LOG.errorContext(requestId, e.getMessage(), null, contextMap); | ||
|
||
Errors errors = new Errors(); | ||
errors.addError(Err.invalidBodyBuilderWithLocation("accounts_association_api").withError(e.getMessage()).build()); | ||
return errors; | ||
} | ||
|
||
@ExceptionHandler(ConstraintViolationException.class) | ||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ResponseBody | ||
public Errors onConstraintViolationException( ConstraintViolationException exception, HttpServletRequest request) { | ||
|
||
Errors errors = new Errors(); | ||
for (ConstraintViolation<?> constraintViolation : exception.getConstraintViolations()) { | ||
final var location = CamelCaseSnakeCase.toSnakeCase(constraintViolation.getPropertyPath().toString()); | ||
var errorMessage = getConstraintViolationExceptionErrorMessage( location ); | ||
errors.addError(Err.invalidBodyBuilderWithLocation(location).withError(errorMessage).build()); | ||
} | ||
|
||
String requestId = request.getHeader(X_REQUEST_ID); | ||
String errorsJsonString = getJsonStringFromErrors(requestId, errors); | ||
LOG.errorContext(requestId, String.format("Validation Failed with [%s]", errorsJsonString), exception, null ); | ||
|
||
return errors; | ||
} | ||
|
||
private String getConstraintViolationExceptionErrorMessage(String location) { | ||
return switch (location) { | ||
case "update_association_status_for_user_and_company.arg0" -> "Please check the request and try again."; | ||
case "update_association_status_for_user_and_company.arg1" -> "Please check the request and try again."; | ||
default -> "One of the inputs is incorrectly formatted"; | ||
}; | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public void onException(Exception e, HttpServletRequest r) { | ||
String requestId = r.getHeader(X_REQUEST_ID); | ||
String msg = r.getRequestURL() + (r.getQueryString()!=null ? "?"+r.getQueryString() : "") + ". " + e.getMessage(); | ||
LOG.errorContext(requestId, msg, e, null); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...ain/java/uk/gov/companieshouse/accounts/association/controller/HealthCheckController.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,16 @@ | ||
package uk.gov.companieshouse.accounts.association.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/associations") | ||
public class HealthCheckController { | ||
|
||
@GetMapping("/healthcheck") | ||
public ResponseEntity<String> checking() { | ||
return ResponseEntity.ok().body("OK"); | ||
} | ||
} |
Oops, something went wrong.