Skip to content

Commit 493406d

Browse files
author
Zhen Li
committed
Fixing the flaky test due to certificate change.
Neo4j server is not started until the start of test, so previous beforeAll might receive NPE if certificate test is the first test run.
1 parent 742d280 commit 493406d

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

driver/src/test/java/org/neo4j/driver/v1/util/CertificateExtension.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,51 @@
1818
*/
1919
package org.neo4j.driver.v1.util;
2020

21-
import org.junit.jupiter.api.extension.AfterAllCallback;
22-
import org.junit.jupiter.api.extension.BeforeAllCallback;
21+
import org.junit.jupiter.api.extension.AfterEachCallback;
2322
import org.junit.jupiter.api.extension.ExtensionContext;
2423

24+
import java.io.IOException;
2525
import java.nio.file.Files;
2626
import java.nio.file.Path;
2727
import java.nio.file.StandardCopyOption;
28+
import java.util.Arrays;
2829

29-
public class CertificateExtension extends DatabaseExtension implements BeforeAllCallback, AfterAllCallback
30+
import static org.neo4j.driver.v1.util.Neo4jRunner.debug;
31+
32+
public class CertificateExtension extends DatabaseExtension implements AfterEachCallback
3033
{
31-
private static Path originalKeyFile;
32-
private static Path originalCertFile;
34+
private Path originalKeyFile;
35+
private Path originalCertFile;
3336

3437
@Override
35-
public void beforeAll( ExtensionContext extensionContext ) throws Exception
38+
public void beforeEach( ExtensionContext context ) throws Exception
3639
{
40+
super.beforeEach( context );
41+
3742
originalKeyFile = Files.createTempFile( "key-file-", "" );
3843
originalCertFile = Files.createTempFile( "cert-file-", "" );
3944

40-
Files.copy( tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING);
45+
Files.copy( tlsKeyFile().toPath(), originalKeyFile, StandardCopyOption.REPLACE_EXISTING );
4146
Files.copy( tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING );
4247
}
4348

4449
@Override
45-
public void afterAll( ExtensionContext extensionContext ) throws Exception
50+
public void afterEach( ExtensionContext context ) throws Exception
4651
{
47-
updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() );
52+
// if the key and cert file changed, then we restore the file and restart the server.
53+
if ( !smallFileContentEquals( tlsKeyFile().toPath(), originalKeyFile ) || !smallFileContentEquals( tlsCertFile().toPath(), originalCertFile ) )
54+
{
55+
debug( "Restoring original key and certificate file after certificate test." );
56+
updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() );
57+
}
4858
Files.deleteIfExists( originalKeyFile );
4959
Files.deleteIfExists( originalCertFile );
5060
}
61+
62+
private boolean smallFileContentEquals( Path path, Path pathAnother ) throws IOException
63+
{
64+
byte[] fileContent = Files.readAllBytes( path );
65+
byte[] fileContentAnother = Files.readAllBytes( pathAnother );
66+
return Arrays.equals( fileContent, fileContentAnother );
67+
}
5168
}

0 commit comments

Comments
 (0)