lr3
This commit is contained in:
1 parent
6c16140f0c
commit
55c9f16841
25 files changed
+1057
No files matched your search
@@ -0,0 +1,25 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum Codes {
|
||||
|
||||
SUCCESS("success"),
|
||||
FAILED("failed");
|
||||
|
||||
private final String name;
|
||||
|
||||
Codes(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum ErrorCodes {
|
||||
|
||||
EMPTY(""),
|
||||
VALIDATION_EXCEPTION("ValidationException"),
|
||||
UNKNOWN_EXCEPTION("UnknownException"),
|
||||
UNSUPPORTED_EXCEPTION("UnsupportedException");
|
||||
|
||||
private final String name;
|
||||
|
||||
ErrorCodes(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum ErrorMessages {
|
||||
|
||||
EMPTY(""),
|
||||
VALIDATION("Ошибка валидации"),
|
||||
UNKNOWN("Не поддерживаемая ошибка");
|
||||
|
||||
private final String description;
|
||||
|
||||
ErrorMessages(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getName() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Request {
|
||||
@NotBlank(message = "UID не может быть пустым")
|
||||
@Size(max = 32, message = "UID не должно быть больше 32 символов")
|
||||
private String uid;
|
||||
|
||||
@NotBlank(message = "operationUid не может быть пустым")
|
||||
@Size(max = 32, message = "operationUid не должно быть больше 32 символов")
|
||||
private String operationUid;
|
||||
|
||||
private Systems systemName;
|
||||
|
||||
@NotBlank
|
||||
private String systemTime;
|
||||
|
||||
private String source;
|
||||
|
||||
@NotNull
|
||||
@Min(1)
|
||||
@Max(100000)
|
||||
private Integer communicationId;
|
||||
|
||||
private Integer templateId;
|
||||
private Integer productCode;
|
||||
private Integer smsCode;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{" +
|
||||
"uid='" + uid + '\'' +
|
||||
", operationUid='" + operationUid + '\'' +
|
||||
", systemName='" + systemName + '\'' +
|
||||
", systemTime='" + systemTime + '\'' +
|
||||
", source='" + source + '\'' +
|
||||
", communicationId=" + communicationId +
|
||||
", templateId=" + templateId +
|
||||
", productCode=" + productCode +
|
||||
", smsCode=" + smsCode +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class Response {
|
||||
private String uid;
|
||||
private String operationUid;
|
||||
private String systemTime;
|
||||
private Codes code;
|
||||
private ErrorCodes errorCode;
|
||||
private ErrorMessages errorMessage;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.testapp.SecondAppV2.Model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
public enum Systems {
|
||||
ERP("Enterprise Resource Planning"),
|
||||
CRM("Customer Relationship Management"),
|
||||
WMS("Warehouse Management System");
|
||||
|
||||
private final String description;
|
||||
|
||||
Systems(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
@JsonValue
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@JsonCreator
|
||||
public static Systems fromValue(String value) {
|
||||
for (Systems system : values()) {
|
||||
if (system.name().equalsIgnoreCase(value) || system.description.equalsIgnoreCase(value)) {
|
||||
return system;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("Unknown systemName: " + value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.testapp.SecondAppV2;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SecondAppV2Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SecondAppV2Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.testapp.SecondAppV2.controller;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Codes;
|
||||
import com.testapp.SecondAppV2.Model.ErrorCodes;
|
||||
import com.testapp.SecondAppV2.Model.ErrorMessages;
|
||||
import com.testapp.SecondAppV2.Model.Request;
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
import com.testapp.SecondAppV2.exception.UnsupportedCodeException;
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
import com.testapp.SecondAppV2.service.RequestValidationService;
|
||||
import com.testapp.SecondAppV2.service.ValidationService;
|
||||
import com.testapp.SecondAppV2.util.DateTimeUtil;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import java.util.Date;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
public class MyController {
|
||||
private final RequestValidationService requestValidationService;
|
||||
private final ValidationService validationService;
|
||||
|
||||
@Autowired
|
||||
public MyController(ValidationService validationService, RequestValidationService requestValidationService) {
|
||||
this.validationService = validationService;
|
||||
this.requestValidationService = requestValidationService;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/feedback")
|
||||
public ResponseEntity<Response> feedback(@Valid @RequestBody Request request, BindingResult bindingResult) {
|
||||
log.info("request: {}", request);
|
||||
Response response = Response.builder()
|
||||
.uid(request.getUid())
|
||||
.operationUid(request.getOperationUid())
|
||||
.systemTime(DateTimeUtil.getCustomFormat().format(new Date(0)))
|
||||
.code(Codes.SUCCESS)
|
||||
.errorCode(ErrorCodes.EMPTY)
|
||||
.errorMessage(ErrorMessages.EMPTY)
|
||||
.build();
|
||||
log.info("response: {}", response);
|
||||
try {
|
||||
validationService.isValid(bindingResult);
|
||||
log.info("request after validation: {}", request);
|
||||
if ("123".equals(request.getUid())) {
|
||||
log.error("unsupported uid: {}", request.getUid());
|
||||
throw new UnsupportedCodeException("Не поддерживаемая ошибка");
|
||||
}
|
||||
} catch (ValidationFailedException e) {
|
||||
log.error("validation failed: {}", e.getMessage());
|
||||
response.setCode(Codes.FAILED);
|
||||
response.setErrorCode(ErrorCodes.VALIDATION_EXCEPTION);
|
||||
response.setErrorMessage(ErrorMessages.VALIDATION);
|
||||
log.info("response: {}", response);
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
} catch (UnsupportedCodeException e) {
|
||||
log.error("unsupported exception: {}", e.getMessage());
|
||||
response.setCode(Codes.FAILED);
|
||||
response.setErrorCode(ErrorCodes.UNSUPPORTED_EXCEPTION);
|
||||
response.setErrorMessage(ErrorMessages.UNKNOWN);
|
||||
log.info("response: {}", response);
|
||||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
log.error("unknown exception: {}", e.getMessage());
|
||||
response.setCode(Codes.FAILED);
|
||||
response.setErrorCode(ErrorCodes.UNKNOWN_EXCEPTION);
|
||||
response.setErrorMessage(ErrorMessages.UNKNOWN);
|
||||
log.info("response: {}", response);
|
||||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
log.info("response: {}", response);
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.testapp.SecondAppV2.exception;
|
||||
|
||||
public class UnsupportedCodeException extends Exception {
|
||||
public UnsupportedCodeException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.testapp.SecondAppV2.exception;
|
||||
|
||||
public class ValidationFailedException extends Exception {
|
||||
public ValidationFailedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Qualifier("ModifyOperationUidResponseService")
|
||||
public class ModifyOperationUidResponseService implements ModifyResponseService {
|
||||
@Override
|
||||
public Response modify(Response response) {
|
||||
log.info("response before: {}", response);
|
||||
UUID uuid = UUID.randomUUID();
|
||||
response.setOperationUid(uuid.toString());
|
||||
log.info("response after: {}", response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
|
||||
@Service
|
||||
public interface ModifyResponseService {
|
||||
Response modify(Response response);
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.testapp.SecondAppV2.Model.Response;
|
||||
import com.testapp.SecondAppV2.util.DateTimeUtil;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@Qualifier("ModifySystemTimeResponseService")
|
||||
public class ModifySystemTimeResponseService implements ModifyResponseService {
|
||||
@Override
|
||||
public Response modify(Response response) {
|
||||
log.info("response before: {}", response);
|
||||
response.setSystemTime(DateTimeUtil.getCustomFormat().format(new Date(0)));
|
||||
log.info("response after: {}", response);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class RequestValidationService implements ValidationService {
|
||||
|
||||
@Override
|
||||
public void isValid(BindingResult bindingResult) throws ValidationFailedException {
|
||||
if (bindingResult.hasErrors()) {
|
||||
log.error("bindingResult: {}", bindingResult.getAllErrors());
|
||||
throw new ValidationFailedException(bindingResult.getFieldError().toString());
|
||||
}
|
||||
log.info("validation passed");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.testapp.SecondAppV2.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import com.testapp.SecondAppV2.exception.ValidationFailedException;
|
||||
|
||||
@Service
|
||||
public interface ValidationService {
|
||||
void isValid(BindingResult bindingResult) throws ValidationFailedException;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.testapp.SecondAppV2.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class DateTimeUtil {
|
||||
public static SimpleDateFormat getCustomFormat() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
spring.application.name=SecondApp
|
||||
server.port=8082
|
||||
|
||||
logging.level.org.springframework=ERROR
|
||||
logging.level.com.testapp=DEBUG
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.testapp.SecondApp;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SecondAppApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user