1818 */
1919package org .neo4j .driver .v1 .integration ;
2020
21- import org .junit .Rule ;
21+ import org .junit .BeforeClass ;
22+ import org .junit .ClassRule ;
2223import org .junit .Test ;
23- import org .junit .rules .ExpectedException ;
2424import org .junit .rules .TemporaryFolder ;
2525
2626import java .util .HashMap ;
3434import org .neo4j .driver .v1 .util .Neo4jSettings ;
3535import org .neo4j .driver .v1 .util .TestNeo4j ;
3636
37+ import static junit .framework .TestCase .fail ;
3738import static org .hamcrest .MatcherAssert .assertThat ;
39+ import static org .hamcrest .Matchers .containsString ;
3840import static org .hamcrest .Matchers .equalTo ;
41+ import static org .hamcrest .Matchers .instanceOf ;
3942import static org .neo4j .driver .v1 .AuthTokens .basic ;
4043import static org .neo4j .driver .v1 .AuthTokens .custom ;
4144import static org .neo4j .driver .v1 .Values .ofValue ;
4245import static org .neo4j .driver .v1 .Values .parameters ;
4346
4447public class CredentialsIT
4548{
46- @ Rule
47- public TemporaryFolder tempDir = new TemporaryFolder ();
49+ @ ClassRule
50+ public static TemporaryFolder tempDir = new TemporaryFolder ();
4851
49- @ Rule
50- public TestNeo4j neo4j = new TestNeo4j ();
52+ @ ClassRule
53+ public static TestNeo4j neo4j = new TestNeo4j ();
5154
52- @ Rule
53- public ExpectedException exception = ExpectedException .none ();
55+ private static String password = "secret" ;
5456
5557 @ Test
5658 public void basicCredentialsShouldWork () throws Throwable
5759 {
58- // Given
59- String password = "secret" ;
60- enableAuth ( password );
61-
6260 // When & Then
6361 try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
6462 basic ("neo4j" , password ) );
@@ -73,28 +71,22 @@ public void basicCredentialsShouldWork() throws Throwable
7371 @ Test
7472 public void shouldGetHelpfulErrorOnInvalidCredentials () throws Throwable
7573 {
76- // Given
77- String password = "secret" ;
78- enableAuth ( password );
79-
80- // Expect
81- exception .expect ( ClientException .class );
82- exception .expectMessage ( "The client is unauthorized due to authentication failure." );
83-
8474 // When
8575 try ( Driver driver = GraphDatabase .driver ( neo4j .uri (), basic ("thisisnotthepassword" , password ) );
8676 Session ignored = driver .session () ) {
8777 //empty
78+ fail ( "Should fail with an auth error already" );
79+ }
80+ catch ( Throwable e )
81+ {
82+ assertThat ( e , instanceOf ( ClientException .class ) );
83+ assertThat ( e .getMessage (), containsString ( "The client is unauthorized due to authentication failure." ) );
8884 }
8985 }
9086
9187 @ Test
9288 public void shouldBeAbleToProvideRealmWithBasicAuth () throws Throwable
9389 {
94- // Given
95- String password = "secret" ;
96- enableAuth ( password );
97-
9890 // When & Then
9991 try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
10092 basic ("neo4j" , password , "native" ) );
@@ -108,10 +100,6 @@ public void shouldBeAbleToProvideRealmWithBasicAuth() throws Throwable
108100 @ Test
109101 public void shouldBeAbleToConnectWithCustomToken () throws Throwable
110102 {
111- // Given
112- String password = "secret" ;
113- enableAuth ( password );
114-
115103 // When & Then
116104 try ( Driver driver = GraphDatabase .driver ( neo4j .uri (),
117105 custom ("neo4j" , password , "native" , "basic" ) );
@@ -125,9 +113,6 @@ public void shouldBeAbleToConnectWithCustomToken() throws Throwable
125113 @ Test
126114 public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters () throws Throwable
127115 {
128- // Given
129- String password = "secret" ;
130- enableAuth ( password );
131116 HashMap <String ,Object > parameters = new HashMap <>();
132117 parameters .put ( "secret" , 16 );
133118
@@ -141,7 +126,8 @@ public void shouldBeAbleToConnectWithCustomTokenWithAdditionalParameters() throw
141126 }
142127 }
143128
144- private void enableAuth ( String password ) throws Exception
129+ @ BeforeClass
130+ public static void enableAuth () throws Exception
145131 {
146132 neo4j .restart ( Neo4jSettings .TEST_SETTINGS
147133 .updateWith ( Neo4jSettings .AUTH_ENABLED , "true" )
0 commit comments