-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Moved in a filter the validation of RequestId and Version. (#26)
- Loading branch information
1 parent
5ad2abe
commit 6f29a60
Showing
13 changed files
with
848 additions
and
704 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
57 changes: 57 additions & 0 deletions
57
src/main/java/it/pagopa/swclient/mil/CommonHeadersValidatorFilter.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,57 @@ | ||
/* | ||
* MappedDiagnosticContextFilter.java | ||
* | ||
* 28 nov 2022 | ||
*/ | ||
package it.pagopa.swclient.mil; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
import it.pagopa.swclient.mil.bean.Errors; | ||
import it.pagopa.swclient.mil.bean.HeaderParamName; | ||
import it.pagopa.swclient.mil.bean.ValidationPattern; | ||
import jakarta.annotation.Priority; | ||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.Priorities; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
import jakarta.ws.rs.container.ContainerRequestFilter; | ||
import jakarta.ws.rs.container.PreMatching; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.core.Response.Status; | ||
import jakarta.ws.rs.ext.Provider; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
@Provider | ||
@PreMatching | ||
@Priority(Priorities.USER) | ||
public class CommonHeadersValidatorFilter implements ContainerRequestFilter { | ||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
List<String> errorCodes = new LinkedList<>(); | ||
List<String> errorMsgs = new LinkedList<>(); | ||
|
||
String requestId = requestContext.getHeaderString(HeaderParamName.REQUEST_ID); | ||
if (requestId != null && !requestId.matches(ValidationPattern.REQUEST_ID)) { | ||
errorCodes.add(ErrorCode.REQUEST_ID_MUST_MATCH_REGEXP); | ||
errorCodes.add(ErrorCode.REQUEST_ID_MUST_MATCH_REGEXP_MSG); | ||
} | ||
|
||
String version = requestContext.getHeaderString(HeaderParamName.VERSION); | ||
if (version != null && !version.matches(ValidationPattern.VERSION)) { | ||
errorCodes.add(ErrorCode.VERSION_MUST_MATCH_REGEXP); | ||
errorCodes.add(ErrorCode.VERSION_MUST_MATCH_REGEXP_MSG); | ||
} | ||
|
||
if (!errorCodes.isEmpty()) { | ||
Response response = Response.status(Status.BAD_REQUEST) | ||
.entity(new Errors(errorCodes, errorMsgs)) | ||
.build(); | ||
throw new BadRequestException(response); | ||
} | ||
} | ||
} |
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
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
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
85 changes: 85 additions & 0 deletions
85
src/test/java/it/pagopa/swclient/mil/CommonHeadersValidatorFilterTest.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,85 @@ | ||
/* | ||
* CommonHeadersValidatorFilterTest.java | ||
* | ||
* 29 apr 2024 | ||
*/ | ||
package it.pagopa.swclient.mil; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.UUID; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.junit.QuarkusTest; | ||
import it.pagopa.swclient.mil.bean.HeaderParamName; | ||
import jakarta.ws.rs.BadRequestException; | ||
import jakarta.ws.rs.container.ContainerRequestContext; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
@QuarkusTest | ||
class CommonHeadersValidatorFilterTest { | ||
@Test | ||
void given_request_when_requestIdAndVersionAreOk_then_noExcpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaderString(HeaderParamName.REQUEST_ID)).thenReturn(UUID.randomUUID().toString()); | ||
when(context.getHeaderString(HeaderParamName.VERSION)).thenReturn("1.0"); | ||
|
||
assertDoesNotThrow(() -> new CommonHeadersValidatorFilter().filter(context)); | ||
} | ||
|
||
@Test | ||
void given_request_when_requestIdAndVersionAreNotSet_then_noExcpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
|
||
assertDoesNotThrow(() -> new CommonHeadersValidatorFilter().filter(context)); | ||
} | ||
|
||
@Test | ||
void given_request_when_requestIdIsInvalidAndVersionIsValid_then_excpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaderString(HeaderParamName.REQUEST_ID)).thenReturn(""); | ||
when(context.getHeaderString(HeaderParamName.VERSION)).thenReturn("1.0"); | ||
|
||
CommonHeadersValidatorFilter filter = new CommonHeadersValidatorFilter(); | ||
assertThrows(BadRequestException.class, () -> filter.filter(context)); | ||
} | ||
|
||
@Test | ||
void given_request_when_requestIdIsInvalidAndVersionIsNotSet_then_excpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaderString(HeaderParamName.REQUEST_ID)).thenReturn(""); | ||
|
||
CommonHeadersValidatorFilter filter = new CommonHeadersValidatorFilter(); | ||
assertThrows(BadRequestException.class, () -> filter.filter(context)); | ||
} | ||
|
||
@Test | ||
void given_request_when_requestIdIsValidAndVersionIsInvalid_then_excpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaderString(HeaderParamName.REQUEST_ID)).thenReturn(UUID.randomUUID().toString()); | ||
when(context.getHeaderString(HeaderParamName.VERSION)).thenReturn(new String(new byte[] { | ||
0 | ||
})); | ||
|
||
CommonHeadersValidatorFilter filter = new CommonHeadersValidatorFilter(); | ||
assertThrows(BadRequestException.class, () -> filter.filter(context)); | ||
} | ||
|
||
@Test | ||
void given_request_when_requestIdIsNotSetAndVersionIsInvalid_then_excpetionMustBeThrown() { | ||
ContainerRequestContext context = mock(ContainerRequestContext.class); | ||
when(context.getHeaderString(HeaderParamName.VERSION)).thenReturn(new String(new byte[] { | ||
0 | ||
})); | ||
|
||
CommonHeadersValidatorFilter filter = new CommonHeadersValidatorFilter(); | ||
assertThrows(BadRequestException.class, () -> filter.filter(context)); | ||
} | ||
} |
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
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