Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #44 from admin-ch/feature/PTAPP-1049-m2m-sperre
Browse files Browse the repository at this point in the history
fix: miss-spelled datetime value in configuration, added validate and…
  • Loading branch information
Armin-Isenring-Bit authored Mar 21, 2022
2 parents 0afd530 + 499e118 commit a66247d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ private boolean isInRange(LocalDateTime now, Endpoint.FromUntil fromUntil) {
LocalDateTime from = (fromUntil.getFrom() == null ? LocalDateTime.MIN : fromUntil.getFrom());
LocalDateTime until = (fromUntil.getUntil() == null ? LocalDateTime.MAX : fromUntil.getUntil());

return Range.between(from, until).contains(now);
boolean isBetween = Range.between(from, until).contains(now);
return isBetween;
}

private boolean sameUri(String requestUri, Endpoint endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;
import javax.naming.ConfigurationException;
import java.util.Collections;
import java.util.List;

Expand All @@ -28,4 +30,28 @@ public class LockdownConfig implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LockdownInterceptor(endpoints));
}

@PostConstruct
public void validate() throws ConfigurationException {
if (endpoints.isEmpty()) {
log.warn("no active lockdowns - please consider deactivating profile 'lockdown'!");
} else {
for (Endpoint endpoint : endpoints) {
if (endpoint.getApplicable().isEmpty()) {
log.warn("no active lockdowns for uri '{}' - please consider removing this endpoint!", endpoint.getUri());
} else {
for (Endpoint.FromUntil fromUntil : endpoint.getApplicable()) {
if (fromUntil.getUntil()==null && fromUntil.getFrom()==null) {
log.warn("endlessly active lockdowns for endpoint '{}' - please consider removing this from/until range!", endpoint.getUri());
} else if (fromUntil.getUntil()!=null && fromUntil.getFrom()!=null) {
if (!fromUntil.getFrom().isBefore(fromUntil.getUntil())) {
log.error("endpoint '{}': invalid active range from '{}' until '{}'", endpoint.getUri(), fromUntil.getFrom(), fromUntil.getUntil());
throw new ConfigurationException("Invalid range from="+fromUntil.getFrom()+" until="+fromUntil.getUntil()+" for Endpoint '"+endpoint.getUri()+"'");
}
}
}
}
}
}
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application-lockdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ lockdown:
applicable:
-
from: 2022-03-22T00:00:00.000Z
until: 2020-03-23T23:59:59.000Z
until: 2022-03-23T23:59:59.000Z
-
from: 2022-04-01T00:00:00.000Z
-
uri: v1/onset
applicable:
-
from: 2022-03-22T00:00:00.000Z
until: 2020-03-23T23:59:59.000Z
until: 2022-03-23T23:59:59.000Z
-
from: 2022-04-01T00:00:00.000Z
-
uri: v2/onset
applicable:
-
from: 2022-03-22T00:00:00.000Z
until: 2020-03-23T23:59:59.000Z
until: 2022-03-23T23:59:59.000Z
-
from: 2022-04-01T00:00:00.000Z
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ch.admin.bag.covidcode.authcodegeneration.lockdown;

import ch.admin.bag.covidcode.authcodegeneration.lockdown.config.Endpoint;
import ch.admin.bag.covidcode.authcodegeneration.lockdown.config.LockdownConfig;
import lombok.Builder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.context.TestPropertySource;

import javax.naming.ConfigurationException;
import java.time.LocalDateTime;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ExtendWith(MockitoExtension.class)
public class LockdownConfigTest {

private LockdownConfig lockdownConfig;

@BeforeEach
private void init() {
lockdownConfig = new LockdownConfig();
}

@Test
public void endpointWithRangeError() {
List<Endpoint> endpoints = EndpointBuilder.have()
.endpoint("xyz").fromUntil(LocalDateTime.MAX, LocalDateTime.MIN).build();

lockdownConfig.setEndpoints(endpoints);

assertThrows(ConfigurationException.class, () -> {
lockdownConfig.validate();
});
}


static class EndpointBuilder {
private Map<String, List<Endpoint.FromUntil>> endpoints = new TreeMap<>();
private List<Endpoint.FromUntil> fromUntils = new ArrayList<>();


protected static EndpointBuilder have() {
return new EndpointBuilder();
}

protected EndpointBuilder fromUntil(LocalDateTime from, LocalDateTime until) {

Endpoint.FromUntil fromUntil = new Endpoint.FromUntil();
fromUntil.setFrom(from);
fromUntil.setUntil(until);
fromUntils.add(fromUntil);

return this;
}

protected EndpointBuilder endpoint(String uri) {

List<Endpoint.FromUntil> current;
if (endpoints.containsKey(uri)) {
// endpoint followed by ranges
current = endpoints.get(uri);
if (current != fromUntils) {
current.addAll(fromUntils);
}
fromUntils = new ArrayList<>();
} else {
// ranges followed by endpoint
endpoints.put(uri, fromUntils);
}

return this;
}

protected List<Endpoint> build() {
List<Endpoint> result = new ArrayList<>();
for (String uri : endpoints.keySet()) {
Endpoint endpoint = new Endpoint();
endpoint.setUri(uri);
endpoint.setApplicable(endpoints.get(uri));
result.add(endpoint);
}

this.endpoints = new TreeMap<>();
this.fromUntils = new ArrayList<>();

return result;
}
}
}

0 comments on commit a66247d

Please sign in to comment.