Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust RestClientBaseTest temp directory handling #31996

Merged
merged 1 commit into from
Mar 21, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package io.quarkus.restclient.runtime;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -25,10 +26,10 @@
import org.eclipse.microprofile.rest.client.RestClientBuilder;
import org.eclipse.microprofile.rest.client.ext.QueryParamStyle;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import io.quarkus.restclient.config.RestClientConfig;
Expand All @@ -39,26 +40,29 @@ public class RestClientBaseTest {
private static final String TRUSTSTORE_PASSWORD = "truststorePassword";
private static final String KEYSTORE_PASSWORD = "keystorePassword";

@TempDir
static File tempDir;
private static File truststoreFile;
private static File keystoreFile;
private static Path truststorePath;
private static Path keystorePath;
private static Config createdConfig;

@BeforeAll
public static void beforeAll() throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException {
// prepare keystore and truststore

truststoreFile = new File(tempDir, "truststore.jks");
keystoreFile = new File(tempDir, "keystore.jks");
truststorePath = Files.createTempFile("truststore", ".jks");

KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(null, TRUSTSTORE_PASSWORD.toCharArray());
truststore.store(new FileOutputStream(truststoreFile), TRUSTSTORE_PASSWORD.toCharArray());
try (OutputStream truststoreOs = Files.newOutputStream(truststorePath)) {
KeyStore truststore = KeyStore.getInstance("JKS");
truststore.load(null, TRUSTSTORE_PASSWORD.toCharArray());
truststore.store(truststoreOs, TRUSTSTORE_PASSWORD.toCharArray());
}

keystorePath = Files.createTempFile("keystore", ".jks");

KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null, KEYSTORE_PASSWORD.toCharArray());
keystore.store(new FileOutputStream(keystoreFile), KEYSTORE_PASSWORD.toCharArray());
try (OutputStream keystoreOs = Files.newOutputStream(keystorePath)) {
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null, KEYSTORE_PASSWORD.toCharArray());
keystore.store(keystoreOs, KEYSTORE_PASSWORD.toCharArray());
}
}

@AfterEach
Expand All @@ -69,6 +73,24 @@ public void afterEach() {
}
}

@AfterAll
public static void afterAll() {
if (truststorePath != null) {
try {
Files.deleteIfExists(truststorePath);
} catch (IOException e) {
// ignore it
}
}
if (keystorePath != null) {
try {
Files.deleteIfExists(keystorePath);
} catch (IOException e) {
// ignore it
}
}
}

@Test
public void testClientSpecificConfigs() throws Exception {
// given
Expand Down Expand Up @@ -149,10 +171,10 @@ private static RestClientsConfig createSampleConfigRoot() {
configRoot.providers = Optional.of("io.quarkus.restclient.runtime.RestClientBaseTest$MyResponseFilter2");
configRoot.queryParamStyle = Optional.of(QueryParamStyle.MULTI_PAIRS);

configRoot.trustStore = Optional.of(truststoreFile.getAbsolutePath());
configRoot.trustStore = Optional.of(truststorePath.toAbsolutePath().toString());
configRoot.trustStorePassword = Optional.of("truststorePassword");
configRoot.trustStoreType = Optional.of("JKS");
configRoot.keyStore = Optional.of(keystoreFile.getAbsolutePath());
configRoot.keyStore = Optional.of(keystorePath.toAbsolutePath().toString());
configRoot.keyStorePassword = Optional.of("keystorePassword");
configRoot.keyStoreType = Optional.of("JKS");

Expand All @@ -177,10 +199,10 @@ private static RestClientConfig createSampleClientConfig() {
clientConfig.providers = Optional.of("io.quarkus.restclient.runtime.RestClientBaseTest$MyResponseFilter1");
clientConfig.queryParamStyle = Optional.of(QueryParamStyle.COMMA_SEPARATED);

clientConfig.trustStore = Optional.of(truststoreFile.getAbsolutePath());
clientConfig.trustStore = Optional.of(truststorePath.toAbsolutePath().toString());
clientConfig.trustStorePassword = Optional.of("truststorePassword");
clientConfig.trustStoreType = Optional.of("JKS");
clientConfig.keyStore = Optional.of(keystoreFile.getAbsolutePath());
clientConfig.keyStore = Optional.of(keystorePath.toAbsolutePath().toString());
clientConfig.keyStorePassword = Optional.of("keystorePassword");
clientConfig.keyStoreType = Optional.of("JKS");

Expand Down