From ac75d45eb0072074e1627f2590377e0b1a5d821f Mon Sep 17 00:00:00 2001 From: hizmailovich Date: Fri, 19 Jan 2024 15:51:18 +0300 Subject: [PATCH] Tests added --- .../pmo/{ => project}/AdjustedTextTest.java | 2 +- .../pmo/{ => project}/DateOfTest.java | 2 +- .../pmo/{ => project}/ProjectOfTest.java | 5 +- .../pmo/{ => project}/SqlStatementTest.java | 2 +- .../tracehub/pmo/security/ClaimOfTest.java | 76 +++++++++++++++++ .../tracehub/pmo/security/ExistsRoleTest.java | 83 +++++++++++++++++++ 6 files changed, 166 insertions(+), 4 deletions(-) rename src/test/java/git/tracehub/pmo/{ => project}/AdjustedTextTest.java (98%) rename src/test/java/git/tracehub/pmo/{ => project}/DateOfTest.java (98%) rename src/test/java/git/tracehub/pmo/{ => project}/ProjectOfTest.java (97%) rename src/test/java/git/tracehub/pmo/{ => project}/SqlStatementTest.java (97%) create mode 100644 src/test/java/git/tracehub/pmo/security/ClaimOfTest.java create mode 100644 src/test/java/git/tracehub/pmo/security/ExistsRoleTest.java diff --git a/src/test/java/git/tracehub/pmo/AdjustedTextTest.java b/src/test/java/git/tracehub/pmo/project/AdjustedTextTest.java similarity index 98% rename from src/test/java/git/tracehub/pmo/AdjustedTextTest.java rename to src/test/java/git/tracehub/pmo/project/AdjustedTextTest.java index 89b3773..86977da 100644 --- a/src/test/java/git/tracehub/pmo/AdjustedTextTest.java +++ b/src/test/java/git/tracehub/pmo/project/AdjustedTextTest.java @@ -15,7 +15,7 @@ * SOFTWARE. */ -package git.tracehub.pmo; +package git.tracehub.pmo.project; import git.tracehub.pmo.project.AdjustedText; import org.hamcrest.MatcherAssert; diff --git a/src/test/java/git/tracehub/pmo/DateOfTest.java b/src/test/java/git/tracehub/pmo/project/DateOfTest.java similarity index 98% rename from src/test/java/git/tracehub/pmo/DateOfTest.java rename to src/test/java/git/tracehub/pmo/project/DateOfTest.java index c9ed766..9215e43 100644 --- a/src/test/java/git/tracehub/pmo/DateOfTest.java +++ b/src/test/java/git/tracehub/pmo/project/DateOfTest.java @@ -15,7 +15,7 @@ * SOFTWARE. */ -package git.tracehub.pmo; +package git.tracehub.pmo.project; import git.tracehub.pmo.project.DateOf; import java.time.LocalDate; diff --git a/src/test/java/git/tracehub/pmo/ProjectOfTest.java b/src/test/java/git/tracehub/pmo/project/ProjectOfTest.java similarity index 97% rename from src/test/java/git/tracehub/pmo/ProjectOfTest.java rename to src/test/java/git/tracehub/pmo/project/ProjectOfTest.java index a6f8d68..4e37ac7 100644 --- a/src/test/java/git/tracehub/pmo/ProjectOfTest.java +++ b/src/test/java/git/tracehub/pmo/project/ProjectOfTest.java @@ -15,7 +15,7 @@ * SOFTWARE. */ -package git.tracehub.pmo; +package git.tracehub.pmo.project; import git.tracehub.pmo.project.Project; import git.tracehub.pmo.project.ProjectOf; @@ -39,6 +39,9 @@ @ExtendWith(MockitoExtension.class) final class ProjectOfTest { + /** + * Result set. + */ @Mock private ResultSet set; diff --git a/src/test/java/git/tracehub/pmo/SqlStatementTest.java b/src/test/java/git/tracehub/pmo/project/SqlStatementTest.java similarity index 97% rename from src/test/java/git/tracehub/pmo/SqlStatementTest.java rename to src/test/java/git/tracehub/pmo/project/SqlStatementTest.java index d36f059..93a8c70 100644 --- a/src/test/java/git/tracehub/pmo/SqlStatementTest.java +++ b/src/test/java/git/tracehub/pmo/project/SqlStatementTest.java @@ -15,7 +15,7 @@ * SOFTWARE. */ -package git.tracehub.pmo; +package git.tracehub.pmo.project; import git.tracehub.pmo.project.SqlStatement; diff --git a/src/test/java/git/tracehub/pmo/security/ClaimOfTest.java b/src/test/java/git/tracehub/pmo/security/ClaimOfTest.java new file mode 100644 index 0000000..3c03b31 --- /dev/null +++ b/src/test/java/git/tracehub/pmo/security/ClaimOfTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023-2024 Tracehub + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to read + * the Software only. Permissions is hereby NOT GRANTED to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package git.tracehub.pmo.security; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.hamcrest.core.IsNull; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; + +/** + * Test suite for {@link ClaimOf}. + * + * @since 0.0.0 + */ +@ExtendWith(MockitoExtension.class) +final class ClaimOfTest { + + /** + * Token. + */ + @Mock + private JwtAuthenticationToken token; + + /** + * JWT. + */ + @Mock + private Jwt jwt; + + @Test + void returnsValueWhenClaimExists() { + final String value = "value"; + Mockito.when(this.token.getCredentials()).thenReturn(this.jwt); + Mockito.when(this.jwt.getClaim("claim")).thenReturn(value); + final String claim = new ClaimOf(this.token, "claim").value(); + MatcherAssert.assertThat( + "Claim value %s is not %s".formatted(claim, value), + claim, + new IsEqual<>("value") + ); + } + + @Test + void returnsValueWhenClaimDoesNotExist() { + Mockito.when(this.token.getCredentials()).thenReturn(this.jwt); + Mockito.when(this.jwt.getClaim("claim")).thenReturn(null); + final String claim = new ClaimOf(this.token, "claim").value(); + MatcherAssert.assertThat( + "Claim value %s is not null".formatted(claim), + claim, + new IsNull<>() + ); + } + +} diff --git a/src/test/java/git/tracehub/pmo/security/ExistsRoleTest.java b/src/test/java/git/tracehub/pmo/security/ExistsRoleTest.java new file mode 100644 index 0000000..9226aa3 --- /dev/null +++ b/src/test/java/git/tracehub/pmo/security/ExistsRoleTest.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023-2024 Tracehub + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to read + * the Software only. Permissions is hereby NOT GRANTED to use, copy, modify, + * merge, publish, distribute, sublicense, and/or sell copies of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package git.tracehub.pmo.security; + +import org.cactoos.list.ListOf; +import org.cactoos.map.MapEntry; +import org.cactoos.map.MapOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.security.oauth2.jwt.Jwt; +import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; + +/** + * Test suite for {@link ExistsRole}. + * + * @since 0.0.0 + */ +@ExtendWith(MockitoExtension.class) +final class ExistsRoleTest { + + /** + * Token. + */ + @Mock + private JwtAuthenticationToken token; + + /** + * JWT. + */ + @Mock + private Jwt jwt; + + @Test + void returnsTrueIfRoleExists() { + Mockito.when(this.token.getCredentials()).thenReturn(this.jwt); + Mockito.when(this.jwt.getClaimAsMap("realm_access")).thenReturn( + new MapOf<>( + new MapEntry<>("roles", new ListOf<>("role")) + ) + ); + MatcherAssert.assertThat( + "Role doesn't exist", + new ExistsRole(this.token, "role").value(), + new IsEqual<>(true) + ); + } + + @Test + void returnsFalseIfRoleDoesNotExist() { + Mockito.when(this.token.getCredentials()).thenReturn(this.jwt); + Mockito.when(this.jwt.getClaimAsMap("realm_access")).thenReturn( + new MapOf<>( + new MapEntry<>("roles", new ListOf<>()) + ) + ); + MatcherAssert.assertThat( + "Role exists", + new ExistsRole(this.token, "role").value(), + new IsEqual<>(false) + ); + } + +}