Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/dependency-check-maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ on:
- cron: "0 10 * * 6"

jobs:

dependency-check-maven:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
ref: develop

- name: Set up JDK 21
uses: actions/setup-java@v2
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
distribution: 'adopt'
cache: maven

- name: Execute OWASP dependency check
run: mvn clean install -P security-updates -DskipTests=true -B
3 changes: 1 addition & 2 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ on:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
distribution: 'adopt'
cache: maven
- name: Fail build if version contains SNAPSHOT
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
- name: Set up JDK 21
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '21'
distribution: 'adopt'
cache: maven
- run: echo "VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_ENV
- run: echo "BUILD_VERSION=${VERSION%-SNAPSHOT}" >> $GITHUB_ENV
Expand Down
2 changes: 1 addition & 1 deletion oc-sso-notificatie/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<artifactId>oc-sso-notificatie-parent</artifactId>
<groupId>nl.kennisnet.services</groupId>
<version>2.4.1-SNAPSHOT</version>
<version>2.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.web.client.RestClient;

import java.net.http.HttpClient;
import java.time.Duration;

/**
Expand All @@ -38,11 +39,13 @@ public static void main(String[] args) {
}

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder
.connectTimeout(Duration.ofSeconds(connectionTimeout))
.readTimeout(Duration.ofSeconds(connectionTimeout))
.build();
public RestClient restClient() {
HttpClient httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(connectionTimeout)).build();

JdkClientHttpRequestFactory requestFactory = new JdkClientHttpRequestFactory(httpClient);
requestFactory.setReadTimeout(Duration.ofSeconds(connectionTimeout));

return RestClient.builder().requestFactory(requestFactory).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package nl.kennisnet.services.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.info.BuildProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.time.Instant;
import java.util.Properties;

@Configuration
public class BuildPropertiesConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(BuildPropertiesConfig.class);

@Bean
public BuildProperties buildProperties(ResourceLoader resourceLoader) {
try {
Resource resource = resourceLoader.getResource("classpath:META-INF/build-info.properties");
if (!resource.exists()) {
LOGGER.warn("META-INF/build-info.properties not found, using default build properties");
Properties defaultProperties = new Properties();
defaultProperties.put("time", Instant.now().toString());
defaultProperties.put("version", "0.0.0-TEST");
defaultProperties.put("name", "test-build");
defaultProperties.put("group", "test-group");
defaultProperties.put("artifact", "test-artifact");
return new BuildProperties(defaultProperties);
}

Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Properties flattenedProperties = new Properties();
properties.forEach((key, value) -> {
String newKey = key.toString().replaceFirst("^build\\.", "");
flattenedProperties.put(newKey, value);
});

return new BuildProperties(flattenedProperties);
} catch (IOException e) {
LOGGER.error("Error loading build-info.properties", e);
Properties defaultProperties = new Properties();
defaultProperties.put("time", Instant.now().toString());
defaultProperties.put("version", "0.0.0-ERROR");
defaultProperties.put("name", "error-build");
defaultProperties.put("group", "error-group");
defaultProperties.put("artifact", "error-artifact");
return new BuildProperties(defaultProperties);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package nl.kennisnet.services.web.config;

import org.springframework.boot.web.servlet.server.CookieSameSiteSupplier;
import org.springframework.boot.web.server.servlet.CookieSameSiteSupplier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

@Service
public class CacheHashService {
Expand All @@ -27,10 +24,10 @@ public class CacheHashService {
@Value("${api.endpoint.url.cacheHash:#{null}}")
private String url;

private final RestTemplate restTemplate;
private final RestClient restClient;

public CacheHashService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
public CacheHashService(RestClient restClient) {
this.restClient = restClient;
}

public String fetchCacheHash() {
Expand All @@ -45,10 +42,12 @@ public String fetchCacheHash() {
HttpEntity<?> httpEntity = new HttpEntity<>(requestHeaders);

try {
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity,
new ParameterizedTypeReference<>() {});
String result = restClient.get()
.uri(url)
.headers(headers -> headers.addAll(httpEntity.getHeaders()))
.retrieve()
.body(String.class);

String result = response.getBody();
if (null == result) {
LOGGER.warn("Received null from data-services cache-hash, returning empty hash");
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.io.IOException;
Expand All @@ -52,7 +51,7 @@ public class IdPProvider {

private static final ObjectMapper objectMapper = new ObjectMapper();

private final RestTemplate restTemplate;
private final RestClient restClient;

@Value("${data.location}")
private Resource dataSource;
Expand All @@ -69,8 +68,8 @@ public class IdPProvider {
@Value("${api.endpoint.url.all-suffix:#{null}}")
private String endpointAllSuffix;

public IdPProvider(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
public IdPProvider(RestClient restClient) {
this.restClient = restClient;
}

/**
Expand Down Expand Up @@ -102,8 +101,11 @@ private List<IdP> getAllSsoNotificationsFromDataServices() {
URI uri = UriComponentsBuilder.fromUriString(endpointUrl + endpointAllSuffix).build().toUri();

try {
return restTemplate.exchange(uri, HttpMethod.GET, httpEntity,
new ParameterizedTypeReference<List<IdP>>(){}).getBody();
return restClient.get()
.uri(uri)
.headers(headers -> headers.addAll(httpEntity.getHeaders()))
.retrieve()
.body(new ParameterizedTypeReference<List<IdP>>() {});
} catch (HttpClientErrorException hcee) {
if (HttpStatus.NOT_FOUND == hcee.getStatusCode()) {
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,91 @@
import nl.kennisnet.services.web.service.CacheHashService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@SpringBootTest
@ExtendWith(MockitoExtension.class)
class CacheHashServiceTest {

@Mock
RestTemplate restTemplate;
RestClient restClient;

@InjectMocks
CacheHashService cacheHashService;
private CacheHashService cacheHashService;

@BeforeEach
void setUp() {
cacheHashService = new CacheHashService(restClient);

ReflectionTestUtils.setField(cacheHashService, "url", "sso-notification-url");
ReflectionTestUtils.setField(cacheHashService, "API_KEY_HEADER", "api-key");
}

@Test
void fetchCacheHashTest() {
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>("HASH", HttpStatus.OK));
RestClient.RequestHeadersUriSpec<?> spec = buildRequestSpec("HASH");

String result = cacheHashService.fetchCacheHash();
when(restClient.get()).thenReturn((RestClient.RequestHeadersUriSpec) spec);

assertEquals("HASH", result);
assertEquals("HASH", cacheHashService.fetchCacheHash());
}

@Test
void fetchCacheNullReturnTest() {
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(null, HttpStatus.OK));
RestClient.RequestHeadersUriSpec<?> spec = buildRequestSpec(null);

String result = cacheHashService.fetchCacheHash();
when(restClient.get()).thenReturn((RestClient.RequestHeadersUriSpec) spec);

assertEquals("", result);
assertEquals("", cacheHashService.fetchCacheHash());
}

@Test
void fetchCacheNullUrlTest() {
ReflectionTestUtils.setField(cacheHashService, "url", null);

when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenReturn(new ResponseEntity<>(null, HttpStatus.OK));

String result = cacheHashService.fetchCacheHash();

assertEquals("", result);
assertEquals("", cacheHashService.fetchCacheHash());
}

@Test
void fetchCacheHashHttpExceptionTest() {
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenThrow(new HttpClientErrorException(HttpStatus.FORBIDDEN));
when(restClient.get()).thenThrow(new HttpClientErrorException(HttpStatus.FORBIDDEN));

String result = cacheHashService.fetchCacheHash();

assertEquals("", result);
assertEquals("", cacheHashService.fetchCacheHash());
}

@Test
void fetchCacheRestClientExceptionTest() {
when(restTemplate.exchange(anyString(), any(HttpMethod.class), any(HttpEntity.class),
any(ParameterizedTypeReference.class))).thenThrow(new RestClientException("ERROR"));
when(restClient.get()).thenThrow(new RestClientException("ERROR"));

assertEquals("", cacheHashService.fetchCacheHash());
}

@SuppressWarnings({"unchecked", "rawtypes"})
private RestClient.RequestHeadersUriSpec<?> buildRequestSpec(String responseBody) {

RestClient.RequestHeadersUriSpec requestSpec =
mock(RestClient.RequestHeadersUriSpec.class);

RestClient.RequestHeadersSpec headersSpec =
mock(RestClient.RequestHeadersSpec.class);

RestClient.ResponseSpec responseSpec =
mock(RestClient.ResponseSpec.class);

String result = cacheHashService.fetchCacheHash();
when(requestSpec.uri(anyString())).thenReturn(headersSpec);
when(headersSpec.headers(any())).thenReturn(headersSpec);
when(headersSpec.retrieve()).thenReturn(responseSpec);
when(responseSpec.body(String.class)).thenReturn(responseBody);

assertEquals("", result);
return requestSpec;
}

}
Loading
Loading