Skip to content

Commit

Permalink
Tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
dukris committed Jan 19, 2024
1 parent 4890edb commit ac75d45
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* SOFTWARE.
*/

package git.tracehub.pmo;
package git.tracehub.pmo.project;

import git.tracehub.pmo.project.AdjustedText;
import org.hamcrest.MatcherAssert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* SOFTWARE.
*/

package git.tracehub.pmo;
package git.tracehub.pmo.project;

import git.tracehub.pmo.project.DateOf;
import java.time.LocalDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +39,9 @@
@ExtendWith(MockitoExtension.class)
final class ProjectOfTest {

/**
* Result set.
*/
@Mock
private ResultSet set;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* SOFTWARE.
*/

package git.tracehub.pmo;
package git.tracehub.pmo.project;


import git.tracehub.pmo.project.SqlStatement;
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/git/tracehub/pmo/security/ClaimOfTest.java
Original file line number Diff line number Diff line change
@@ -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<>()
);
}

}
83 changes: 83 additions & 0 deletions src/test/java/git/tracehub/pmo/security/ExistsRoleTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}

}

0 comments on commit ac75d45

Please sign in to comment.