Skip to content
Snippets Groups Projects
Commit a254e978 authored by Martin Weise's avatar Martin Weise
Browse files

Templateinterceptor

Former-commit-id: 96ee52b8
parent ca76cc8e
No related branches found
No related tags found
1 merge request!42Fixed the query service tests
......@@ -14,6 +14,8 @@ COPY --from=dependency /root/.m2/repository/at/tuwien /root/.m2/repository/at/tu
COPY ./rest-service ./rest-service
COPY ./services ./services
COPY ./report ./report
COPY ./gateway ./gateway
COPY ./api ./api
RUN mvn -q clean package -DskipTests
......
......@@ -11,9 +11,4 @@
<artifactId>api</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
......@@ -10,6 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>at.tuwien</groupId>
......@@ -19,9 +20,4 @@
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>
\ No newline at end of file
package at.tuwien.config;
import at.tuwien.gateway.ZenodoTemplateInterceptor;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
......@@ -9,6 +10,7 @@ import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriTemplateHandler;
import javax.validation.constraints.NotNull;
import java.util.List;
@Configuration
public class ZenodoConfig {
......@@ -27,9 +29,11 @@ public class ZenodoConfig {
public RestTemplate zenodo() {
DefaultUriTemplateHandler defaultUriTemplateHandler = new DefaultUriTemplateHandler();
defaultUriTemplateHandler.setBaseUrl(zenodoEndpoint);
return new RestTemplateBuilder()
final RestTemplate template = new RestTemplateBuilder()
.uriTemplateHandler(defaultUriTemplateHandler)
.build();
template.setInterceptors(List.of(new ZenodoTemplateInterceptor()));
return template;
}
}
......@@ -5,12 +5,12 @@ import at.tuwien.config.ZenodoConfig;
import at.tuwien.exception.ZenodoApiException;
import at.tuwien.exception.ZenodoAuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Component
......@@ -37,4 +37,17 @@ public class ZenodoGateway {
return Arrays.asList(response.getBody());
}
public DepositDto createDeposit() throws ZenodoAuthenticationException, ZenodoApiException {
final ResponseEntity<DepositDto> response = zenodoRestTemplate.exchange("/api/deposit/depositions?access_token={token}",
HttpMethod.POST, null, DepositDto.class, zenodoConfig.getZenodoApiKey());
if (response.getStatusCode().is4xxClientError()) {
throw new ZenodoAuthenticationException("Token is missing or invalid.");
}
if (response.getBody() == null) {
throw new ZenodoApiException("Endpoint returned null body");
}
return response.getBody();
}
}
package at.tuwien.gateway;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.io.IOException;
@Service
public class ZenodoTemplateInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
ClientHttpResponse response = execution.execute(request, body);
response.getHeaders().add("Content-Type", "application/json");
response.getHeaders().add("Accept", "application/json");
return response;
}
}
......@@ -45,8 +45,13 @@ public class ZenodoIntegrationTest extends BaseUnitTest {
/* test */
final List<DepositDto> response = zenodoGateway.listDeposits();
assertEquals(1, response.size());
assertEquals(DEPOSIT_1_ID, response.get(0).getId());
}
@Test
public void createDeposit_succeeds() throws ZenodoApiException, ZenodoAuthenticationException {
/* test */
final DepositDto response = zenodoGateway.createDeposit();
}
}
\ No newline at end of file
......@@ -74,4 +74,24 @@ public class ZenodoUnitTest extends BaseUnitTest {
});
}
@Test
public void createDeposit_succeed() throws ZenodoApiException, ZenodoAuthenticationException {
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.POST), eq(null), eq(DepositDto.class), anyString()))
.thenReturn(ResponseEntity.ok(DEPOSIT_1_DTO));
/* test */
final DepositDto response = zenodoGateway.createDeposit();
}
@Test
public void createDeposit_noToken_fails() {
when(zenodoTemplate.exchange(anyString(), eq(HttpMethod.POST), eq(null), eq(DepositDto.class), anyString()))
.thenReturn(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
/* test */
assertThrows(ZenodoAuthenticationException.class, () -> {
zenodoGateway.createDeposit();
});
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment