-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
myconext-server/src/main/java/myconext/manage/ResourceServiceProviderResolver.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,44 @@ | ||
package myconext.manage; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
import myconext.model.ServiceProvider; | ||
import org.apache.commons.io.IOUtil; | ||
import org.springframework.core.io.Resource; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class ResourceServiceProviderResolver implements ServiceProviderResolver { | ||
|
||
private final List<Map<String, Map<String, String>>> spNames; | ||
|
||
@SneakyThrows | ||
public ResourceServiceProviderResolver(Resource resource, ObjectMapper objectMapper) { | ||
spNames = objectMapper.readValue(IOUtil.toString(resource.getInputStream()), new TypeReference<>() { | ||
}); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
//noop | ||
} | ||
|
||
@Override | ||
public Optional<ServiceProvider> resolve(String entityId) { | ||
return spNames.stream().filter(m -> m.containsKey(entityId)) | ||
.findFirst() | ||
.map(entry -> { | ||
Map<String, String> data = entry.get(entityId); | ||
return new ServiceProvider( | ||
data.get("en"), | ||
data.get("nl"), | ||
data.get("logo_url"), | ||
data.get("home_url"), | ||
data.get("institution_guid")); | ||
}); | ||
|
||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
myconext-server/src/test/java/myconext/manage/ManageServiceProviderConfigurationTest.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,29 @@ | ||
package myconext.manage; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import myconext.model.ServiceProvider; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.mock.env.MockEnvironment; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ManageServiceProviderConfigurationTest { | ||
|
||
@Test | ||
void serviceProviderResolver() { | ||
ManageServiceProviderConfiguration configuration = new ManageServiceProviderConfiguration(); | ||
ServiceProviderResolver serviceProviderResolver = configuration.serviceProviderResolver(null, null, null, false, | ||
new ClassPathResource("metadata/sp-names.json"), | ||
new ObjectMapper(), | ||
new MockEnvironment()); | ||
ServiceProvider serviceProvider = serviceProviderResolver.resolve("http://mock-sp").get(); | ||
assertEquals("SURFconext Mujina SP", serviceProvider.getName()); | ||
|
||
Optional<ServiceProvider> optionalServiceProvider = serviceProviderResolver.resolve("nope"); | ||
assertFalse(optionalServiceProvider.isPresent()); | ||
|
||
} | ||
} |