Skip to content

Commit

Permalink
Add tests for deep unmarshaling of provider options. Add serializatio…
Browse files Browse the repository at this point in the history
…n converter from Jackson to JSR 353 JSON-P world including tests. IQSS#6694
  • Loading branch information
poikilotherm committed Mar 16, 2020
1 parent 1413223 commit 7b4843e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- Should be refactored and removed once on Java EE 8 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr353</artifactId>
</dependency>
<dependency>
<!-- required by org.swordapp.server.sword2-server -->
<groupId>com.io7m.xom</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package edu.harvard.iq.dataverse.authorization.providers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.datatype.jsr353.JSR353Module;
import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationProviderConfigurationException;
import org.everit.json.schema.Schema;
import org.everit.json.schema.ValidationException;
Expand All @@ -9,6 +11,8 @@
import org.json.JSONObject;
import org.json.JSONTokener;

import javax.json.Json;
import javax.json.JsonObject;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -44,6 +48,11 @@ public static boolean validate(String rawJson) throws AuthenticationProviderConf
}
}

public static JsonObject serialize(AuthenticationProviderConfiguration config) {
mapper.registerModule(new JSR353Module());
return mapper.convertValue(config, JsonObject.class);
}

/**
* Fetch the schema and process into usable validator based on https://github.com/everit-org/json-schema
* @return The schema object, to be used as validator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ public boolean equals(Object o) {
return false;
}

public String toString() {
return
"client_id: "+this.oauthClientId+", "+
"client_secret: "+this.oauthClientSecret+", "+
"user_endpoint: "+this.oauthUserEndpoint+", "+
"issuer: "+this.oidcIssuerUrl+", "+
"passive_login: "+this.shibPassiveLogin;
}

public static class AuthenticationProviderOptionsConverter implements AttributeConverter<AuthenticationProviderOptions, String> {
@Override
public String convertToDatabaseColumn(AuthenticationProviderOptions authenticationProviderOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
package edu.harvard.iq.dataverse.authorization.providers;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.harvard.iq.dataverse.authorization.exceptions.AuthenticationProviderConfigurationException;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.json.JsonObject;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

class AuthenticationProviderConfigurationParserTest {

@BeforeAll
static void loadAuthSchema() throws URISyntaxException, IOException {
/* Non working code. NPE. Can't we access resources from main in a test?
Path file = Paths.get(ClassLoader.getSystemResource("schemas/auth-provider-schema.json").toURI());
String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
System.out.println(content);
*/
}
ObjectMapper mapper = new ObjectMapper();

@ParameterizedTest
@ValueSource(strings = {"minimal-valid.json", "title-i18n-valid.json"})
Expand All @@ -40,6 +34,32 @@ void parseValid(String filename) throws Exception {
assertNotNull(config);
}

@Test
void testUnmarshallingDeepForOptions() throws Exception {
Path file = Paths.get(ClassLoader.getSystemResource("auth-providers/options-unmarshal-test.json").toURI());
String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);

AuthenticationProviderConfiguration config = AuthenticationProviderConfigurationParser.parse(content);

List<String> expect = Arrays.asList("client_id","test","client_secret","test");
assertThat(config.options().toString(), stringContainsInOrder(expect));
}

@Test
void testSerialization() throws Exception {
// given
Path file = Paths.get(ClassLoader.getSystemResource("auth-providers/options-unmarshal-test.json").toURI());
String content = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
AuthenticationProviderConfiguration config = AuthenticationProviderConfigurationParser.parse(content);

// when
JsonObject test = AuthenticationProviderConfigurationParser.serialize(config);

//then
assertNotNull(test);
assertEquals(mapper.writeValueAsString(config), test.toString());
}

@Test
void testValidatorLoading() throws Exception {
assertDoesNotThrow(() -> AuthenticationProviderConfigurationParser.getValidator());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ void testUnmarshaling() throws Exception {
assertEquals(expect, opts);
}

@Test
void testUnmarshalingPartial() throws Exception {
// given
String json = "{\"client_id\": \"test\", \"client_secret\": \"test\" }";
AuthenticationProviderOptions expect = new AuthenticationProviderOptions();
expect.oauthClientId = "test";
expect.oauthClientSecret = "test";

// when
AuthenticationProviderOptions opts = mapper.readValue(json, AuthenticationProviderOptions.class);
// then
assertEquals(expect, opts);
}

@Test
void testMarshalingComplete() throws Exception {
// given
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/auth-providers/options-unmarshal-test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "test",
"type": "test",
"enabled": true,
"title": {
"en": "test"
},
"options": {
"client_id": "test",
"client_secret": "test"
}
}

0 comments on commit 7b4843e

Please sign in to comment.