Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
galaxina committed Nov 30, 2023
1 parent 635726f commit bfab688
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
13 changes: 6 additions & 7 deletions oap-stdlib/src/main/java/oap/http/server/nio/NioHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.URL;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -343,10 +342,10 @@ public enum PortType {
public static class DefaultPort {
public final int httpPort;
public final int httpsPort;
public final Path keyStore;
public final URL keyStore;
public final String password;

public DefaultPort( int httpPort, int httpsPort, Path keyStore, String password ) {
public DefaultPort( int httpPort, int httpsPort, URL keyStore, String password ) {
this.httpPort = httpPort;
this.httpsPort = httpsPort;
this.keyStore = keyStore;
Expand All @@ -359,7 +358,7 @@ public DefaultPort( int httpPort ) {
}

@SneakyThrows
private static KeyStore loadKeyStore( Path keyStoreLocation, String storePassword ) {
private static KeyStore loadKeyStore( URL keyStoreLocation, String storePassword ) {
final KeyStore loadedKeystore;
final String type = "JKS";
try {
Expand All @@ -368,7 +367,7 @@ private static KeyStore loadKeyStore( Path keyStoreLocation, String storePasswor
log.error( "loadKeyStore KeyStore.getInstance({}) exception: {}", type, ex.toString() );
throw ex;
}
try( InputStream stream = Files.newInputStream( keyStoreLocation ) ) {
try( InputStream stream = keyStoreLocation.openStream() ) {
loadedKeystore.load( stream, storePassword.toCharArray() );
return loadedKeystore;
} catch( NoSuchAlgorithmException | CertificateException | IOException ex ) {
Expand Down Expand Up @@ -397,7 +396,7 @@ private static KeyManager[] getKeyManagers( KeyStore keyStore, final String stor
}

@SneakyThrows
private static KeyManager[] makeKeyManagers( Path keyStoreLocation, final String password ) {
private static KeyManager[] makeKeyManagers( URL keyStoreLocation, final String password ) {
KeyStore keyStore = loadKeyStore( keyStoreLocation, password );
KeyManager[] managers = getKeyManagers( keyStore, password );
log.info( "makeKeyManagers({}, {}) created KeyManagers {}", keyStoreLocation, password.length(), managers );
Expand Down
32 changes: 19 additions & 13 deletions oap-stdlib/src/main/java/oap/reflect/Coercions.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,22 +392,28 @@ public Object apply( Object value ) {
private static class URLConvertor implements Function<Object, Object> {
@Override
public Object apply( Object value ) {
if( value instanceof URL ) return value;
else if( value instanceof String ) {
try {

return new URL( ( String ) value );
} catch( MalformedURLException e ) {
var url = getClass().getResource( ( String ) value );
if( url != null ) return url;

return switch( value ) {
case URL url -> url;
case String str -> {
try {
return Paths.get( ( String ) value ).toUri().toURL();
} catch( MalformedURLException malformedURLException ) {
throw new ReflectException( "cannot cast " + value + " to URL.class" );
yield new URL( str );
} catch( MalformedURLException e ) {
if( str.startsWith( "classpath(" ) ) {
str = str.substring( 10, str.length() - 1 );
}

var url = getClass().getResource( str );
if( url != null ) yield url;

try {
yield Paths.get( str ).toUri().toURL();
} catch( MalformedURLException malformedURLException ) {
throw new ReflectException( "cannot cast " + value + " to URL.class" );
}
}
}
} else throw new ReflectException( "cannot cast " + value + " to URL.class" );
default -> throw new ReflectException( "cannot cast " + value + " to URL.class" );
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@
import oap.application.testng.KernelFixture;
import oap.http.Client;
import oap.http.Http;
import oap.io.Resources;
import oap.testng.EnvFixture;
import oap.testng.Fixtures;
import oap.testng.TestDirectoryFixture;
import org.testng.annotations.Test;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.List;

import static oap.http.Http.Headers.CONNECTION;
Expand Down Expand Up @@ -102,7 +105,7 @@ public void testBindToSpecificPort() throws IOException {
* keytool -genkey -alias ssl -keyalg RSA -keysize 2048 -dname "CN=localhost,OU=IT" -keystore master.jks -storepass 1234567 -keypass 1234567
*/
@Test
public void testHttps() {
public void testHttps() throws URISyntaxException {
TestDirectoryFixture.deployTestData( getClass() );

var kernelFixture = new KernelFixture( urlOfTestResource( getClass(), "test-application.conf" ),
Expand All @@ -115,7 +118,7 @@ public void testHttps() {
int httpsPort = kernelFixture.portFor( "TEST_HTTPS_PORT" );

try( Client client = Client
.custom( TestDirectoryFixture.testPath( "master.jks" ), "1234567", 10000, 10000 )
.custom( Paths.get(Resources.url( getClass(), "/oap/http/test_https.jks" ).get().toURI()), "1234567", 10000, 10000 )
.build() ) {

kernelFixture.beforeMethod();
Expand Down
2 changes: 2 additions & 0 deletions oap-stdlib/src/test/java/oap/reflect/CoercionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public void testUrl() throws MalformedURLException {

assertThat( coercions.cast( Reflect.reflect( URL.class ), "/oap/reflect/CoercionsTest.class" ) )
.isEqualTo( Coercions.class.getResource( "/oap/reflect/CoercionsTest.class" ) );
assertThat( coercions.cast( Reflect.reflect( URL.class ), "classpath(/oap/reflect/CoercionsTest.class)" ) )
.isEqualTo( Coercions.class.getResource( "/oap/reflect/CoercionsTest.class" ) );
}

@ToString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services {
defaultPort {
httpPort = ${TEST_HTTP_PORT}
httpsPort = ${TEST_HTTPS_PORT}
keyStore = ${TEST_DIRECTORY}/master.jks
keyStore = classpath(/oap/http/test_https.jks)
password = ${TEST_PASSWORD}
}
additionalHttpPorts.httpprivate = ${TEST_HTTP_PORT}
Expand Down

0 comments on commit bfab688

Please sign in to comment.