Skip to content

Commit

Permalink
Use var for local variables in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steinarb committed Feb 19, 2024
1 parent 370110d commit 9fae33d
Show file tree
Hide file tree
Showing 24 changed files with 1,067 additions and 1,117 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2023 Steinar Bang
* Copyright 2019-2024 Steinar Bang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,6 @@

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Expand All @@ -40,13 +39,13 @@ class ProductionLiquibaseRunnerTest {

@Test
void testCreateSchema() throws Exception {
ProductionLiquibaseRunner runner = new ProductionLiquibaseRunner();
var runner = new ProductionLiquibaseRunner();
runner.activate();
var database = createDataSource("authservice1");
runner.prepare(database);
try(Connection connection = database.getConnection()) {
try(PreparedStatement statment = connection.prepareStatement("select * from users")) {
ResultSet results = statment.executeQuery();
try(var connection = database.getConnection()) {
try(var statment = connection.prepareStatement("select * from users")) {
var results = statment.executeQuery();
int usercount = 0;
while(results.next()) {
++usercount;
Expand All @@ -56,9 +55,9 @@ void testCreateSchema() throws Exception {
}
}

try(Connection connection = database.getConnection()) {
try(PreparedStatement statment = connection.prepareStatement("select * from roles")) {
ResultSet results = statment.executeQuery();
try(var connection = database.getConnection()) {
try(var statment = connection.prepareStatement("select * from roles")) {
var results = statment.executeQuery();
int rolecount = 0;
while(results.next()) {
++rolecount;
Expand All @@ -71,59 +70,59 @@ void testCreateSchema() throws Exception {

@Test
void testCreateSchemaWhenSQLExceptionIsThrown() throws Exception {
ProductionLiquibaseRunner runner = new ProductionLiquibaseRunner();
Connection connection = mock(Connection.class);
DataSource datasource = mock(DataSource.class);
var runner = new ProductionLiquibaseRunner();
var connection = mock(Connection.class);
var datasource = mock(DataSource.class);
when(connection.getMetaData()).thenThrow(SQLException.class);
when(datasource.getConnection()).thenReturn(connection);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to create schema in authservice postgresql database");
}

@Test
void testFailWhenInsertingData() throws Exception {
ProductionLiquibaseRunner runner = new ProductionLiquibaseRunner();
var runner = new ProductionLiquibaseRunner();
var datasource = spy(createDataSource("authservice2"));
Connection connection = mock(Connection.class);
var connection = mock(Connection.class);
when(connection.getMetaData()).thenThrow(SQLException.class);
when(datasource.getConnection()).thenCallRealMethod().thenReturn(connection);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to create schema in authservice postgresql database");
}

@Test
void testFailWhenUpdatingSchema() throws Exception {
ProductionLiquibaseRunner runner = new ProductionLiquibaseRunner();
var runner = new ProductionLiquibaseRunner();
var datasource = spy(createDataSource("authservice3"));
Connection connection = mock(Connection.class);
var connection = mock(Connection.class);
when(connection.getMetaData()).thenThrow(SQLException.class);
when(datasource.getConnection()).thenCallRealMethod().thenCallRealMethod().thenReturn(connection);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to update schma in authservice postgresql database");
}

Connection createMockConnection() throws Exception {
Connection connection = mock(Connection.class);
DatabaseMetaData metadata = mock(DatabaseMetaData.class);
var connection = mock(Connection.class);
var metadata = mock(DatabaseMetaData.class);
when(metadata.getDatabaseProductName()).thenReturn("mockdb");
when(metadata.getSQLKeywords()).thenReturn("insert, select, delete");
when(metadata.getURL()).thenReturn("jdbc:mock:///authservice");
ResultSet tables = mock(ResultSet.class);
var tables = mock(ResultSet.class);
when(metadata.getTables(anyString(), anyString(), anyString(), any(String[].class))).thenReturn(tables);
Statement stmnt = mock(Statement.class);
ResultSet results = mock(ResultSet.class);
var stmnt = mock(Statement.class);
var results = mock(ResultSet.class);
when(results.next()).thenReturn(true).thenReturn(false);
when(stmnt.executeQuery(anyString())).thenReturn(results);
when(stmnt.getUpdateCount()).thenReturn(-1);
Expand All @@ -133,7 +132,7 @@ Connection createMockConnection() throws Exception {
}

private DataSource createDataSource(String dbname) throws SQLException {
Properties properties = new Properties();
var properties = new Properties();
properties.setProperty(DataSourceFactory.JDBC_URL, "jdbc:derby:memory:" + dbname + ";create=true");
return derbyDataSourceFactory.createDataSource(properties);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 Steinar Bang
* Copyright 2018-2024 Steinar Bang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

Expand All @@ -38,15 +35,15 @@ class TestLiquibaseRunnerTest {

@Test
void testCreateSchema() throws Exception {
TestLiquibaseRunner runner = new TestLiquibaseRunner();
var runner = new TestLiquibaseRunner();
runner.activate();
DataSource datasource = createDatasource();
var datasource = createDatasource();
runner.prepare(datasource);

try(Connection connection = datasource.getConnection()) {
try(PreparedStatement statment = connection.prepareStatement("select * from users")) {
ResultSet results = statment.executeQuery();
int usercount = 0;
try(var connection = datasource.getConnection()) {
try(var statment = connection.prepareStatement("select * from users")) {
var results = statment.executeQuery();
var usercount = 0;
while(results.next()) {
++usercount;
}
Expand All @@ -55,10 +52,10 @@ void testCreateSchema() throws Exception {
}
}

try(Connection connection = datasource.getConnection()) {
try(PreparedStatement statment = connection.prepareStatement("select * from roles")) {
ResultSet results = statment.executeQuery();
int rolecount = 0;
try(var connection = datasource.getConnection()) {
try(var statment = connection.prepareStatement("select * from roles")) {
var results = statment.executeQuery();
var rolecount = 0;
while(results.next()) {
++rolecount;
}
Expand All @@ -70,52 +67,52 @@ void testCreateSchema() throws Exception {

@Test
void testCreateSchemaWhenSQLExceptionIsThrown() throws Exception {
TestLiquibaseRunner runner = new TestLiquibaseRunner();
DataSource datasource = mock(DataSource.class);
var runner = new TestLiquibaseRunner();
var datasource = mock(DataSource.class);
when(datasource.getConnection()).thenThrow(SQLException.class);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to create schema for authservice Derby test database component");
}

@Test
void testFailWhenInsertingMockData() throws Exception {
TestLiquibaseRunner runner = new TestLiquibaseRunner();
DataSource realdb = createDatasource();
DataSource datasource = spy(realdb);
var runner = new TestLiquibaseRunner();
var realdb = createDatasource();
var datasource = spy(realdb);
when(datasource.getConnection())
.thenCallRealMethod()
.thenThrow(SQLException.class);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to insert mock data in authservice Derby test database component");
}

@Test
void testFailWhenUpdatingSchema() throws Exception {
TestLiquibaseRunner runner = new TestLiquibaseRunner();
DataSource realdb = createDatasource();
DataSource datasource = spy(realdb);
var runner = new TestLiquibaseRunner();
var realdb = createDatasource();
var datasource = spy(realdb);
when(datasource.getConnection())
.thenCallRealMethod()
.thenCallRealMethod()
.thenThrow(SQLException.class);

runner.activate();
AuthserviceException ex = assertThrows(
var ex = assertThrows(
AuthserviceException.class,
() -> runner.prepare(datasource));
assertThat(ex.getMessage()).startsWith("Failed to update schema of authservice Derby test database component");
}

private DataSource createDatasource() throws SQLException {
Properties properties = new Properties();
var properties = new Properties();
properties.setProperty(DataSourceFactory.JDBC_URL, "jdbc:derby:memory:authservice;create=true");
return derbyDataSourceFactory.createDataSource(properties);
}
Expand Down

0 comments on commit 9fae33d

Please sign in to comment.