Skip to content

Commit 5b58956

Browse files
author
Zhen Li
committed
Fixing the flaky test that is caused by certificate file changes.
1 parent f9a6fca commit 5b58956

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

driver/src/test/java/org/neo4j/driver/v1/integration/TrustCustomCertificateIT.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@
1818
*/
1919
package org.neo4j.driver.v1.integration;
2020

21-
import org.junit.jupiter.api.AfterAll;
22-
import org.junit.jupiter.api.BeforeAll;
2321
import org.junit.jupiter.api.Test;
2422
import org.junit.jupiter.api.extension.RegisterExtension;
2523

2624
import java.io.File;
27-
import java.io.IOException;
28-
import java.nio.file.Files;
29-
import java.nio.file.Path;
30-
import java.nio.file.StandardCopyOption;
3125
import java.util.function.Supplier;
3226

3327
import org.neo4j.driver.v1.Config;
@@ -36,6 +30,7 @@
3630
import org.neo4j.driver.v1.Session;
3731
import org.neo4j.driver.v1.StatementResult;
3832
import org.neo4j.driver.v1.exceptions.SecurityException;
33+
import org.neo4j.driver.v1.util.CertificateExtension;
3934
import org.neo4j.driver.v1.util.CertificateToolUtil.CertificateKeyPair;
4035
import org.neo4j.driver.v1.util.DatabaseExtension;
4136
import org.neo4j.driver.v1.util.ParallelizableIT;
@@ -51,28 +46,7 @@
5146
class TrustCustomCertificateIT
5247
{
5348
@RegisterExtension
54-
static final DatabaseExtension neo4j = new DatabaseExtension();
55-
56-
private static Path originalKeyFile;
57-
private static Path originalCertFile;
58-
59-
@BeforeAll
60-
static void beforeAll() throws IOException
61-
{
62-
originalKeyFile = Files.createTempFile( "key-file-", "" );
63-
originalCertFile = Files.createTempFile( "cert-file-", "" );
64-
65-
Files.copy( neo4j.tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING);
66-
Files.copy( neo4j.tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING );
67-
}
68-
69-
@AfterAll
70-
static void afterAll() throws Exception
71-
{
72-
neo4j.updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() );
73-
Files.deleteIfExists( originalKeyFile );
74-
Files.deleteIfExists( originalCertFile );
75-
}
49+
static final DatabaseExtension neo4j = new CertificateExtension();
7650

7751
@Test
7852
void shouldAcceptServerWithCertificateSignedByDriverCertificate() throws Throwable

driver/src/test/java/org/neo4j/driver/v1/integration/TrustOnFirstUseIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.neo4j.driver.v1.Session;
3131
import org.neo4j.driver.v1.StatementResult;
3232
import org.neo4j.driver.v1.exceptions.SecurityException;
33+
import org.neo4j.driver.v1.util.CertificateExtension;
3334
import org.neo4j.driver.v1.util.CertificateToolUtil;
3435
import org.neo4j.driver.v1.util.DatabaseExtension;
3536
import org.neo4j.driver.v1.util.ParallelizableIT;
@@ -46,7 +47,7 @@
4647
class TrustOnFirstUseIT
4748
{
4849
@RegisterExtension
49-
static final DatabaseExtension neo4j = new DatabaseExtension();
50+
static final DatabaseExtension neo4j = new CertificateExtension();
5051

5152
@Test
5253
void shouldTrustOnFirstUse() throws Throwable
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (c) 2002-2019 "Neo4j,"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.driver.v1.util;
21+
22+
import org.junit.jupiter.api.extension.AfterAllCallback;
23+
import org.junit.jupiter.api.extension.BeforeAllCallback;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
25+
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.StandardCopyOption;
29+
30+
public class CertificateExtension extends DatabaseExtension implements BeforeAllCallback, AfterAllCallback
31+
{
32+
private static Path originalKeyFile;
33+
private static Path originalCertFile;
34+
35+
@Override
36+
public void beforeAll( ExtensionContext extensionContext ) throws Exception
37+
{
38+
originalKeyFile = Files.createTempFile( "key-file-", "" );
39+
originalCertFile = Files.createTempFile( "cert-file-", "" );
40+
41+
Files.copy( tlsKeyFile().toPath(), originalKeyFile , StandardCopyOption.REPLACE_EXISTING);
42+
Files.copy( tlsCertFile().toPath(), originalCertFile, StandardCopyOption.REPLACE_EXISTING );
43+
}
44+
45+
@Override
46+
public void afterAll( ExtensionContext extensionContext ) throws Exception
47+
{
48+
updateEncryptionKeyAndCert( originalKeyFile.toFile(), originalCertFile.toFile() );
49+
Files.deleteIfExists( originalKeyFile );
50+
Files.deleteIfExists( originalCertFile );
51+
}
52+
}

0 commit comments

Comments
 (0)