Skip to content

Commit

Permalink
Fixes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Aug 21, 2024
1 parent 698c82b commit bed3b94
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
26 changes: 16 additions & 10 deletions server/src/main/java/access/manage/LocalManage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import access.exception.NotFoundException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Builder;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;

import java.util.*;
Expand All @@ -17,22 +21,24 @@
public final class LocalManage implements Manage {

private final Map<EntityType, List<Map<String, Object>>> allProviders;
private final boolean local;
private final DefaultResourceLoader defaultResourceLoader = new DefaultResourceLoader();

public LocalManage(ObjectMapper objectMapper, boolean local) {
this.local = local;
public LocalManage(ObjectMapper objectMapper) {
this(objectMapper, "classpath:/manage");
}

public LocalManage(ObjectMapper objectMapper, String staticManageDirectory) {
this.allProviders = Stream.of(EntityType.values()).collect(Collectors.toMap(
entityType -> entityType,
entityType -> this.initialize(objectMapper, entityType)));
entityType -> this.initialize(objectMapper, entityType, staticManageDirectory)));
}


@SneakyThrows
private List<Map<String, Object>> initialize(ObjectMapper objectMapper, EntityType entityType) {
String collectionName = entityType.collectionName();
if (this.local && collectionName.equals(EntityType.PROVISIONING.collectionName())) {
collectionName += ".local";
}
return objectMapper.readValue(new ClassPathResource("/manage/" + collectionName + ".json").getInputStream(), new TypeReference<>() {
private List<Map<String, Object>> initialize(ObjectMapper objectMapper, EntityType entityType, String staticManageDirectory) {
String resourceName = String.format("%s/%s.json", staticManageDirectory, entityType.collectionName());
Resource resource = defaultResourceLoader.getResource(resourceName);
return objectMapper.readValue(resource.getInputStream(), new TypeReference<>() {
});
}

Expand Down
4 changes: 2 additions & 2 deletions server/src/main/java/access/manage/ManageConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public Manage manage(@Value("${manage.url}") String url,
@Value("${manage.user}") String user,
@Value("${manage.password}") String password,
@Value("${manage.enabled}") boolean enabled,
@Value("${manage.local}") boolean local,
@Value("${manage.staticManageDirectory}") String staticManageDirectory,
ObjectMapper objectMapper) throws IOException {
return enabled ? new RemoteManage(url, user, password, objectMapper) : new LocalManage(objectMapper, local);
return enabled ? new RemoteManage(url, user, password, objectMapper) : new LocalManage(objectMapper, staticManageDirectory);
}

@Bean
Expand Down
3 changes: 2 additions & 1 deletion server/src/main/resources/application-devconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ manage:
url: "https://manage.dev.openconext.local"
user: invite
password: secret
local: False
# If you want to run the mock Manage against a git ignored file with sensitive data, e.g. manage/provisioning.local.json file
# local: True
staticManageDirectory: classpath:/manage

Check failure on line 132 in server/src/main/resources/application-devconf.yml

View workflow job for this annotation

GitHub Actions / Test documentation and generate openapi html documentation

132:1 [empty-lines] too many blank lines (1 > 0)
7 changes: 4 additions & 3 deletions server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ manage:
url: "https://manage.test2.surfconext.nl"
user: invite
password: secret
local: False
# If you want to run the mock Manage against a git ignored file with sensitive data, e.g. manage/provisioning.local.json file
# local: True
# If manage is disabled (e.g. enabled: False) the staticManageDirectory is the directory where the {metadata_type}.json files

Check warning on line 170 in server/src/main/resources/application.yml

View workflow job for this annotation

GitHub Actions / Test documentation and generate openapi html documentation

170:1 [comments-indentation] comment not indented like content
# are located. This can also be an absolute file path, e.g. file:///opt/openconext/oidc-playground/manage
staticManageDirectory: classpath:/manage
# staticManageDirectory: file:///usr/local/etc/manage

springdoc:
pathsToMatch: "/api/external/v1/**"
Expand Down
6 changes: 5 additions & 1 deletion server/src/test/java/access/AbstractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -96,6 +97,9 @@ public abstract class AbstractTest {
public static final String API_TOKEN_HASH = HashGenerator.generateToken();
public static final String API_TOKEN_SUPER_USER_HASH = HashGenerator.generateToken();

@Value("${manage.staticManageDirectory}")
private String staticManageDirectory;

@Autowired
protected ObjectMapper objectMapper;

Expand Down Expand Up @@ -148,7 +152,7 @@ protected void beforeEach() throws Exception {
this.doSeed();
}
if (this.localManage == null) {
this.localManage = new LocalManage(objectMapper, false);
this.localManage = new LocalManage(objectMapper, staticManageDirectory);
}
}

Expand Down
5 changes: 0 additions & 5 deletions server/src/test/java/access/manage/RemoteManageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ class RemoteManageTest extends AbstractTest {
@Autowired
private Manage manage;

private final boolean local = false;

@Override
protected boolean seedDatabase() {
return false;
}

@Test
void providers() throws JsonProcessingException {
LocalManage localManage = new LocalManage(objectMapper, local);
List<Map<String, Object>> serviceProviders = localManage.providers(EntityType.SAML20_SP);
String body = objectMapper.writeValueAsString(serviceProviders);
stubFor(post(urlPathMatching("/manage/api/internal/search/saml20_sp")).willReturn(aResponse()
Expand All @@ -39,7 +36,6 @@ void providers() throws JsonProcessingException {

@Test
void providerById() throws JsonProcessingException {
LocalManage localManage = new LocalManage(objectMapper, local);
Map<String, Object> provider = localManage.providerById(EntityType.SAML20_SP, "1");
String body = objectMapper.writeValueAsString(provider);
stubFor(get(urlPathMatching("/manage/api/internal/metadata/saml20_sp/1")).willReturn(aResponse()
Expand All @@ -53,7 +49,6 @@ void providerById() throws JsonProcessingException {

@Test
void providersByIdIn() throws JsonProcessingException {
LocalManage localManage = new LocalManage(objectMapper, local);
List<Map<String, Object>> providers = localManage.providersByIdIn(EntityType.SAML20_SP,List.of("1","3","4"));
String body = objectMapper.writeValueAsString(providers);
stubFor(get(urlPathMatching("/manage/api/internal/rawSearch/saml20_sp")).willReturn(aResponse()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GraphClientTest {
"test.eduid.nl",
new RSAKeyStore(),
new ObjectMapper());
final LocalManage localManage = new LocalManage( ObjectMapperHolder.objectMapper, false);
final LocalManage localManage = new LocalManage(ObjectMapperHolder.objectMapper);

@Test
void newUserRequest() {
Expand Down

0 comments on commit bed3b94

Please sign in to comment.