This is the codeAbility Sharing Platform! Learn more about the codeAbility Sharing Platform.

Skip to content
Snippets Groups Projects
Commit e674e3cc authored by Michael Breu's avatar Michael Breu :speech_balloon:
Browse files

Improving Tests

parent 3e87a585
2 merge requests!37Word cloud into production,!35Resolve "Teaser Content as Word Cloud"
......@@ -317,6 +317,12 @@
<version>${archunit-junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.zalando</groupId>
<artifactId>problem-spring-web</artifactId>
......
......@@ -241,12 +241,9 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
JwtDecoder jwtDecoder() {
NimbusJwtDecoder oidcJwtDecoder = (NimbusJwtDecoder) JwtDecoders.fromOidcIssuerLocation(gitLabIssuerUri);
OAuth2TokenValidator<Jwt> audienceValidator = new at.ac.uibk.gitsearch.security.oauth2.AudienceValidator(jHipsterProperties.getSecurity().getOauth2().getAudience());
OAuth2TokenValidator<Jwt> withIssuer = JwtValidators.createDefaultWithIssuer(gitLabIssuerUri);
OAuth2TokenValidator<Jwt> withAudience = new DelegatingOAuth2TokenValidator<>(withIssuer, audienceValidator);
// TODO with Audience ist merkwürdig/unnötig
oidcJwtDecoder.setJwtValidator(withAudience);
oidcJwtDecoder.setJwtValidator(withIssuer);
return oidcJwtDecoder;
}
......
......@@ -7,7 +7,6 @@ import org.gitlab4j.api.GitLabApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import at.ac.uibk.gitsearch.config.ApplicationProperties;
......@@ -17,6 +16,7 @@ import at.ac.uibk.gitsearch.security.jwt.TokenProvider.GitLabAccessInfo;
@Repository
public class GitLabRepository {
@SuppressWarnings("unused")
private final Logger log = LoggerFactory.getLogger(GitLabRepository.class);
@Autowired
......
package at.ac.uibk.gitsearch.security.oauth2;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.core.OAuth2Error;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
import org.springframework.security.oauth2.jwt.Jwt;
public class AudienceValidator implements OAuth2TokenValidator<Jwt> {
private final Logger log = LoggerFactory.getLogger(AudienceValidator.class);
private final OAuth2Error error = new OAuth2Error("invalid_token", "The required audience is missing", null);
private final List<String> allowedAudience;
public AudienceValidator(List<String> allowedAudience) {
// Assert.notEmpty(allowedAudience, "Allowed audience should not be null or empty.");
this.allowedAudience = allowedAudience;
}
public OAuth2TokenValidatorResult validate(Jwt jwt) {
if(allowedAudience.isEmpty() ) {
// we do not really care for audience
return OAuth2TokenValidatorResult.success();
}
List<String> audience = jwt.getAudience();
if (audience.stream().anyMatch(allowedAudience::contains)) {
return OAuth2TokenValidatorResult.success();
} else {
log.warn("Invalid audience: {}", audience);
return OAuth2TokenValidatorResult.failure(error);
}
}
}
......@@ -138,7 +138,7 @@ public class SearchResultDTO {
}
@Override
public int hashCode() {
public final int hashCode() {
int prime = 31;
int result = this.project.hashCode();
result = prime * result + this.file.hashCode();
......@@ -147,7 +147,7 @@ public class SearchResultDTO {
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
......@@ -234,12 +234,12 @@ public class SearchResultDTO {
}
@Override
public int hashCode() {
public final int hashCode() {
return Integer.hashCode(project_id);
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
......@@ -329,7 +329,7 @@ public class SearchResultDTO {
}
@Override
public int hashCode() {
public final int hashCode() {
final int prime = 31;
int result = this.path.hashCode();
result = prime * result + this.commit_id.hashCode();
......@@ -337,7 +337,7 @@ public class SearchResultDTO {
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (this == obj) {
return true;
}
......
......@@ -61,7 +61,7 @@ public class SearchResultsDTO {
}
@Override
public int hashCode() {
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((affiliation == null) ? 0 : affiliation.hashCode());
......@@ -71,12 +71,12 @@ public class SearchResultsDTO {
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
if (!getClass().isAssignableFrom(obj.getClass()) )
return false;
Person other = (Person) obj;
if (affiliation == null) {
......@@ -193,7 +193,7 @@ public class SearchResultsDTO {
}
@Override
public int hashCode() {
public final int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + project_id;
......@@ -201,12 +201,12 @@ public class SearchResultsDTO {
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
if (!getClass().isAssignableFrom(obj.getClass()) )
return false;
GitProject other = (GitProject) obj;
if (project_id != other.project_id)
......
package at.ac.uibk.gitsearch.repository.gitlab;
import static at.ac.uibk.gitsearch.web.rest.AccountResourceIT.TEST_USER_LOGIN;
import static org.junit.Assert.assertNotNull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import at.ac.uibk.gitsearch.GitsearchApp;
@SpringBootTest(classes = GitsearchApp.class)
@WithMockUser(value = TEST_USER_LOGIN, authorities = "sharing")
public class GitLabRepositoryIT {
@SuppressWarnings("unused")
private static final Logger LOGGER = LogManager.getLogger(GitLabRepositoryIT.class);
@Autowired
private GitLabRepository gitLabRepository;
@Test
public void getGitLabAccessInfoTest() {
gitLabRepository.getGitLabAccessInfo();
}
@Test
public void getGitLabApiTest() {
final GitLabApi gitLabApi = gitLabRepository.getGitLabApi();
final ApiVersion apiVersion = gitLabApi.getApiVersion();
assertNotNull(apiVersion);
}
}
......@@ -2,8 +2,14 @@ package at.ac.uibk.gitsearch.service.dto;
import java.lang.reflect.InvocationTargetException;
import org.junit.Assert;
import at.ac.uibk.gitsearch.service.MailService;
import at.ac.uibk.gitsearch.service.dto.SearchResultDTO.GitProject;
import at.ac.uibk.gitsearch.service.dto.SearchResultsDTO.Person;
import at.ac.uibk.gitsearch.testingUtilities.PropertiesTester;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
/**
* Integration tests for {@link MailService}.
......@@ -23,11 +29,30 @@ public class VariousDTOTest {
public void testSearchResultsDTO() throws IllegalAccessException, InvocationTargetException {
propertiesTester.testProperties(SearchResultsDTO.class);
propertiesTester.testProperties(SearchResultDTO.class);
propertiesTester.testProperties(Person.class);
propertiesTester.testProperties(GitProject.class);
}
@org.junit.jupiter.api.Test
public void testSearchInputDTO() throws IllegalAccessException, InvocationTargetException {
propertiesTester.testProperties(SearchInputDTO.class);
}
@org.junit.jupiter.api.Test
public void testVariousEquals() throws IllegalAccessException, InvocationTargetException {
GitProject p1 = new GitProject(); p1.setProjectId(10);
GitProject p2 = new GitProject(); p2.setProjectId(10);
GitProject p3 = new GitProject(); p3.setProjectId(20);
Assert.assertEquals(p1, p2);
Assert.assertNotEquals(p1, p3);
EqualsVerifier.forClass(GitProject.class)
.suppress(Warning.NONFINAL_FIELDS)
.withIgnoredFields("project_name", "namespace","main_group", "last_activity_at", "url", "sub_group").verify();
EqualsVerifier.forClass(Person.class).suppress(Warning.NONFINAL_FIELDS).verify();
EqualsVerifier.forClass(at.ac.uibk.gitsearch.service.dto.SearchResultsDTO.GitProject.class)
.suppress(Warning.NONFINAL_FIELDS)
.withIgnoredFields("namespace", "visibility", "last_activity_at", "url").verify();
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment