public static <T, E extends Exception> void assertEventually( String reason, ThrowingSupplier<T, E> actual, Matcher<? super T> matcher, long timeout, TimeUnit timeUnit ) throws E, InterruptedException { assertEventually( ignored -> reason, actual, matcher, timeout, timeUnit ); }
@Test public void shouldFailSetPasswordWithTooManyArguments() { String[] arguments = {"", "123", "321"}; assertException( () -> setPasswordCommand.execute( arguments ), IncorrectUsage.class, "unrecognized arguments: '123 321'" ); }
public static void assertObjectOrArrayEquals( Object expected, Object actual ) { assertObjectOrArrayEquals( "", expected, actual ); }
public static void assertObjectOrArrayEquals( String message, Object expected, Object actual ) { if ( expected.getClass().isArray() ) { if ( !ArrayUtil.equals( expected, actual ) ) { throw newAssertionError( message, expected, actual ); } } else { if ( !Objects.equals( expected, actual ) ) { throw newAssertionError( message, expected, actual ); } } }
@Test public void shouldFailForNoArguments() { assertException( () -> setDefaultAdmin.execute( new String[0] ), IncorrectUsage.class, "not enough arguments" ); }
} ).start(); assertEventually( "Wrapped was not started", running::get, is( true ), 10, TimeUnit.SECONDS ); assertThat( "Bootstrapper exited early", exited.get(), is( false ) ); assertEventually( "Wrapped was not stopped", running::get, is( false ), 10, TimeUnit.SECONDS ); assertEventually( "Bootstrapper did not exit", exited::get, is( true ), 10, TimeUnit.SECONDS ); assertThat( "Bootstrapper did not propagate exit status", status.get(), is( 0 ) );
for ( String key : properties.keySet() ) assertObjectOrArrayEquals( properties.get( key ), listedProperties.get( key ) );
@Test public void shouldFailForTooManyArguments() { String[] arguments = {"", "123", "321"}; assertException( () -> setDefaultAdmin.execute( arguments ), IncorrectUsage.class, "unrecognized arguments: '123 321'" ); }
@Test public void shouldBlockUntilTheIndexIsOnline() throws IndexNotFoundKernelException, InterruptedException { when( tokenRead.nodeLabel( anyString() ) ).thenReturn( 0 ); when( tokenRead.propertyKey( anyString() ) ).thenReturn( 0 ); when( schemaRead.index( anyInt(), any() ) ).thenReturn( anyIndex ); AtomicReference<InternalIndexState> state = new AtomicReference<>( POPULATING ); when( schemaRead.indexGetState( any( IndexReference.class ) ) ).then( invocationOnMock -> state.get() ); AtomicBoolean done = new AtomicBoolean( false ); new Thread( () -> { try { procedure.awaitIndexByPattern( ":Person(name)", TIMEOUT, TIME_UNIT ); } catch ( ProcedureException e ) { throw new RuntimeException( e ); } done.set( true ); } ).start(); assertThat( done.get(), is( false ) ); state.set( ONLINE ); assertEventually( "Procedure did not return after index was online", done::get, is( true ), TIMEOUT, TIME_UNIT ); }
@Test public void shouldFailSetPasswordWithNoArguments() { assertException( () -> setPasswordCommand.execute( new String[0] ), IncorrectUsage.class, "not enough arguments" ); }
} ).start(); assertEventually( "Blocked unexpectedly", exited::get, is( true ), 10, TimeUnit.SECONDS ); assertThat( "Bootstrapper did not propagate exit status", status.get(), is( 1 ) );
@Test public void shouldNotLeakTransactionOnSecurityContextFreezeFailure() throws Throwable { KernelTransactions kernelTransactions = newKernelTransactions(); LoginContext loginContext = mock( LoginContext.class ); when( loginContext.authorize( any(), any() ) ).thenThrow( new AuthorizationExpiredException( "Freeze failed." ) ); assertException(() -> kernelTransactions.newInstance(KernelTransaction.Type.explicit, loginContext, 0L), AuthorizationExpiredException.class, "Freeze failed."); assertThat("We should not have any transaction", kernelTransactions.activeTransactions(), is(empty())); }
@Test public void shouldTimeoutIfTheIndexTakesTooLongToComeOnline() throws InterruptedException, IndexNotFoundKernelException { when( tokenRead.nodeLabel( anyString() ) ).thenReturn( 0 ); when( tokenRead.propertyKey( anyString() ) ).thenReturn( 0 ); when( schemaRead.index( anyInt(), anyInt() ) ).thenReturn( anyIndex ); when( schemaRead.indexGetState( any( IndexReference.class ) ) ).thenReturn( POPULATING ); AtomicReference<ProcedureException> exception = new AtomicReference<>(); new Thread( () -> { try { // We wait here, because we expect timeout procedure.awaitIndexByPattern( ":Person(name)", 0, TIME_UNIT ); } catch ( ProcedureException e ) { exception.set( e ); } } ).start(); assertEventually( "Procedure did not time out", exception::get, not( nullValue() ), TIMEOUT, TIME_UNIT ); //noinspection ThrowableResultOfMethodCallIgnored assertThat( exception.get().status(), is( Status.Procedure.ProcedureTimedOut ) ); } }
@Test public void shouldCreateGeneralIndex() { // GIVEN IndexDescriptor descriptor = forLabel( LABEL_ID, PROPERTY_ID_1 ); StoreIndexDescriptor indexRule = descriptor.withId( RULE_ID ); // THEN assertThat( indexRule.getId(), equalTo( RULE_ID ) ); assertFalse( indexRule.canSupportUniqueConstraint() ); assertThat( indexRule.schema(), equalTo( descriptor.schema() ) ); assertThat( indexRule, equalTo( descriptor ) ); assertThat( indexRule.providerDescriptor(), equalTo( PROVIDER_DESCRIPTOR ) ); assertException( indexRule::getOwningConstraint, IllegalStateException.class ); assertException( () -> indexRule.withOwningConstraint( RULE_ID_2 ), IllegalStateException.class ); }
@Test public void shouldStartStopNeoServerWithoutAnyConfigFiles() throws Throwable { // When int resultCode = ServerBootstrapper.start( bootstrapper, withConnectorsOnRandomPortsConfig( "--home-dir", tempDir.newFolder( "home-dir" ).getAbsolutePath(), "-c", configOption( data_directory, tempDir.getRoot().getAbsolutePath() ), "-c", configOption( logs_directory, tempDir.getRoot().getAbsolutePath() ), "-c", "dbms.backup.enabled=false" ) ); // Then assertEquals( ServerBootstrapper.OK, resultCode ); assertEventually( "Server was not started", bootstrapper::isRunning, is( true ), 1, TimeUnit.MINUTES ); }
@Test public void shouldNotAllowComplexNames() throws Exception { // Given FileUserRepository users = new FileUserRepository( fs, authFile, logProvider ); // When users.assertValidUsername( "neo4j" ); users.assertValidUsername( "johnosbourne" ); users.assertValidUsername( "john_osbourne" ); assertException( () -> users.assertValidUsername( null ), InvalidArgumentsException.class, "The provided username is empty." ); assertException( () -> users.assertValidUsername( "" ), InvalidArgumentsException.class, "The provided username is empty." ); assertException( () -> users.assertValidUsername( "," ), InvalidArgumentsException.class, "Username ',' contains illegal characters. Use ascii characters that are not ',', ':' or whitespaces" + "." ); assertException( () -> users.assertValidUsername( "with space" ), InvalidArgumentsException.class, "Username 'with space' contains illegal characters. Use ascii characters that are not ',', ':' or " + "whitespaces." ); assertException( () -> users.assertValidUsername( "with:colon" ), InvalidArgumentsException.class, "Username 'with:colon' contains illegal characters. Use ascii characters that are not ',', ':' or " + "whitespaces." ); assertException( () -> users.assertValidUsername( "withå" ), InvalidArgumentsException.class, "Username 'withå' contains illegal characters. Use ascii characters that are not ',', ':' or " + "whitespaces." ); }
@Test public void shouldFailIfWrongCredentials() throws Throwable { // When connection.connect( address ) .send( util.defaultAcceptedVersions() ) .send( util.chunk( new InitMessage( "TestClient/1.1", map( "principal", "neo4j", "credentials", "wrong", "scheme", "basic" ) ) ) ); // Then assertThat( connection, util.eventuallyReceivesSelectedProtocolVersion() ); assertThat( connection, util.eventuallyReceives( msgFailure( Status.Security.Unauthorized, "The client is unauthorized due to authentication failure." ) ) ); assertThat( connection, eventuallyDisconnects() ); assertEventually( ignore -> "Matching log call not found in\n" + logProvider.serialize(), this::authFailureLoggedToUserLog, is( true ), 30, SECONDS ); }
private void assertParseRelationshipPropertyExistsRule( String serialized, String name ) throws Exception { // GIVEN long ruleId = 51; int propertyKey = 6119; int relTypeId = 8512; ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForRelType( relTypeId, propertyKey ); byte[] bytes = decodeBase64( serialized ); // WHEN ConstraintRule deserialized = assertConstraintRule( SchemaRuleSerialization.deserialize( ruleId, ByteBuffer.wrap( bytes ) ) ); // THEN assertThat( deserialized.getId(), equalTo( ruleId ) ); assertThat( deserialized.getConstraintDescriptor(), equalTo( constraint ) ); assertThat( deserialized.schema(), equalTo( constraint.schema() ) ); assertException( deserialized::getOwnedIndex, IllegalStateException.class ); assertThat( deserialized.getName(), is( name ) ); }
private void testStartupWithConnectors( boolean httpEnabled, boolean httpsEnabled, boolean boltEnabled ) throws Exception { int resultCode = ServerBootstrapper.start( bootstrapper, "--home-dir", tempDir.newFolder( "home-dir" ).getAbsolutePath(), "-c", configOption( data_directory, tempDir.getRoot().getAbsolutePath() ), "-c", configOption( logs_directory, tempDir.getRoot().getAbsolutePath() ), "-c", "dbms.connector.http.enabled=" + httpEnabled, "-c", "dbms.connector.http.listen_address=:0", "-c", "dbms.connector.https.enabled=" + httpsEnabled, "-c", "dbms.connector.https.listen_address=:0", "-c", "dbms.connector.bolt.enabled=" + boltEnabled, "-c", "dbms.connector.bolt.listen_address=:0" ); assertEquals( ServerBootstrapper.OK, resultCode ); assertEventually( "Server was not started", bootstrapper::isRunning, is( true ), 1, TimeUnit.MINUTES ); assertDbAccessibleAsEmbedded(); verifyConnector( db(), "http", httpEnabled ); verifyConnector( db(), "https", httpsEnabled ); verifyConnector( db(), "bolt", boltEnabled ); }
public static <E extends Exception> void assertException( ThrowingAction<E> f, Class<?> typeOfException, String partOfErrorMessage ) { try { f.apply(); fail( "Expected exception of type " + typeOfException + ", but no exception was thrown" ); } catch ( Exception e ) { if ( typeOfException.isInstance( e ) ) { if ( partOfErrorMessage != null ) { assertThat( e.getMessage(), containsString( partOfErrorMessage ) ); } } else { fail( "Got unexpected exception " + e.getClass() + "\nExpected: " + typeOfException ); } } }