ReplaceCustomChangeUnitTest
This commit is contained in:
		| @@ -23,9 +23,6 @@ public class ReplaceCustomChange implements CustomTaskChange { | ||||
|     private String searchFor; | ||||
|     private String replaceWith; | ||||
|  | ||||
|     @SuppressWarnings("unused") | ||||
|     private ResourceAccessor resourceAccessor; | ||||
|  | ||||
|     @Override | ||||
|     public void execute(final Database database) throws CustomChangeException { | ||||
|         final JdbcConnection conn = (JdbcConnection) database.getConnection(); | ||||
| @@ -34,8 +31,9 @@ public class ReplaceCustomChange implements CustomTaskChange { | ||||
|             conn.setAutoCommit(false); | ||||
|             final Statement statement = conn.createStatement(); | ||||
|             for (String columnName : columnNames.split(",")) { | ||||
|                 final String sql = "UPDATE " + tableName + " SET " + columnName + "= replace(" + columnName + ", '|', " + | ||||
|                         (isH2 ? "STRINGDECODE('\n') " : "E'\\n'") + ")"; | ||||
|                 final String sql = "UPDATE " + tableName + " SET " + columnName + "= replace(" + columnName + ", '" + searchFor | ||||
|                         + "', " + | ||||
|                         (isH2 ? "STRINGDECODE('" + replaceWith + "')" : "E'" + replaceWith + "'") + ")"; | ||||
|                 statement.executeUpdate(sql); | ||||
|             } | ||||
|             conn.commit(); | ||||
| @@ -46,7 +44,7 @@ public class ReplaceCustomChange implements CustomTaskChange { | ||||
|  | ||||
|     @Override | ||||
|     public String getConfirmationMessage() { | ||||
|         return "table " + tableName + " / columns " + columnNames + ": replaced all '" + searchFor + "' to '" + replaceWith | ||||
|         return "in table " + tableName + " / columns " + columnNames + ": replaced all '" + searchFor + "' to '" + replaceWith | ||||
|                 + "'"; | ||||
|     } | ||||
|  | ||||
| @@ -65,33 +63,33 @@ public class ReplaceCustomChange implements CustomTaskChange { | ||||
|         return null; | ||||
|     } | ||||
|  | ||||
|     public String getTableName() { | ||||
|         return tableName; | ||||
|     } | ||||
|     // public String getTableName() { | ||||
|     // return tableName; | ||||
|     // } | ||||
|  | ||||
|     public void setTableName(final String tableName) { | ||||
|         this.tableName = tableName; | ||||
|     } | ||||
|  | ||||
|     public String getColumnNames() { | ||||
|         return columnNames; | ||||
|     } | ||||
|     // public String getColumnNames() { | ||||
|     // return columnNames; | ||||
|     // } | ||||
|  | ||||
|     public void setColumnNames(final String columns) { | ||||
|         this.columnNames = columns; | ||||
|     } | ||||
|  | ||||
|     public String getSearchFor() { | ||||
|         return searchFor; | ||||
|     } | ||||
|     // public String getSearchFor() { | ||||
|     // return searchFor; | ||||
|     // } | ||||
|  | ||||
|     public void setSearchFor(final String searchFor) { | ||||
|         this.searchFor = searchFor; | ||||
|     } | ||||
|  | ||||
|     public String getReplaceWith() { | ||||
|         return replaceWith; | ||||
|     } | ||||
|     // public String getReplaceWith() { | ||||
|     // return replaceWith; | ||||
|     // } | ||||
|  | ||||
|     public void setReplaceWith(final String replaceWith) { | ||||
|         this.replaceWith = replaceWith; | ||||
|   | ||||
| @@ -0,0 +1,148 @@ | ||||
| // Licensed under Apache-2.0 | ||||
| package org.hostsharing.hsadminng.liquibase; | ||||
|  | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| import static org.assertj.core.api.ThrowableAssert.catchThrowable; | ||||
| import static org.mockito.ArgumentMatchers.anyString; | ||||
| import static org.mockito.BDDMockito.given; | ||||
| import static org.mockito.Mockito.verify; | ||||
|  | ||||
| import liquibase.database.Database; | ||||
| import liquibase.database.jvm.JdbcConnection; | ||||
| import liquibase.exception.CustomChangeException; | ||||
| import liquibase.exception.DatabaseException; | ||||
| import liquibase.exception.SetupException; | ||||
|  | ||||
| import org.junit.Before; | ||||
| import org.junit.Rule; | ||||
| import org.junit.Test; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.junit.MockitoJUnit; | ||||
| import org.mockito.junit.MockitoRule; | ||||
|  | ||||
| import java.sql.SQLException; | ||||
| import java.sql.Statement; | ||||
|  | ||||
| public class ReplaceCustomChangeUnitTest { | ||||
|  | ||||
|     private static final String POSTGRES_DATABASE_PRODUCT_NAME = "PostgreSQL"; | ||||
|     private static final String H2_DATABASE_PRODUCT_NAME = "H2"; | ||||
|  | ||||
|     @Rule | ||||
|     public MockitoRule mockitoRule = MockitoJUnit.rule(); | ||||
|  | ||||
|     @Mock | ||||
|     private Database database; | ||||
|  | ||||
|     @Mock | ||||
|     private JdbcConnection connection; | ||||
|  | ||||
|     @Mock | ||||
|     private Statement statement; | ||||
|  | ||||
|     @Before | ||||
|     public void initMocks() throws DatabaseException { | ||||
|         given(database.getConnection()).willReturn(connection); | ||||
|         given(connection.createStatement()).willReturn(statement); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void updatesForPostgres() throws Exception { | ||||
|         // given | ||||
|         given(database.getDatabaseProductName()).willReturn(POSTGRES_DATABASE_PRODUCT_NAME); | ||||
|         final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); | ||||
|  | ||||
|         // when | ||||
|         replaceCustomChange.execute(database); | ||||
|  | ||||
|         // then | ||||
|         verify(statement).executeUpdate("UPDATE some_table SET address= replace(address, '|', E'\\n')"); | ||||
|         verify(statement).executeUpdate("UPDATE some_table SET remark= replace(remark, '|', E'\\n')"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void updatesForH2() throws Exception { | ||||
|         // given | ||||
|         given(database.getDatabaseProductName()).willReturn(H2_DATABASE_PRODUCT_NAME); | ||||
|         final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); | ||||
|  | ||||
|         // when | ||||
|         replaceCustomChange.execute(database); | ||||
|  | ||||
|         // then | ||||
|         verify(statement).executeUpdate("UPDATE some_table SET address= replace(address, '|', STRINGDECODE('\\n'))"); | ||||
|         verify(statement).executeUpdate("UPDATE some_table SET remark= replace(remark, '|', STRINGDECODE('\\n'))"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void getConfirmationMessage() throws Exception { | ||||
|         // given | ||||
|         final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); | ||||
|  | ||||
|         // when | ||||
|         final String actual = replaceCustomChange.getConfirmationMessage(); | ||||
|  | ||||
|         // then | ||||
|         assertThat(actual).isEqualTo("in table some_table / columns address,remark: replaced all '|' to '\\n'"); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void onDatabaseExceptionThrowsCustomChangeException() throws Exception { | ||||
|         // given | ||||
|         given(database.getDatabaseProductName()).willReturn(POSTGRES_DATABASE_PRODUCT_NAME); | ||||
|         final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); | ||||
|         final Exception givenCausingException = new DatabaseException("dummy"); | ||||
|         given(connection.createStatement()).willThrow(givenCausingException); | ||||
|  | ||||
|         // when | ||||
|         final Throwable actual = catchThrowable(() -> replaceCustomChange.execute(database)); | ||||
|  | ||||
|         // then | ||||
|         assertThat(actual).isInstanceOfSatisfying( | ||||
|                 CustomChangeException.class, | ||||
|                 (cce) -> assertThat(cce.getCause()).isSameAs(givenCausingException)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void onSQLExceptionThrowsCustomChangeException() throws Exception { | ||||
|         // given | ||||
|         given(database.getDatabaseProductName()).willReturn(POSTGRES_DATABASE_PRODUCT_NAME); | ||||
|         final ReplaceCustomChange replaceCustomChange = givenReplaceCustomChange("some_table", "address,remark", "|", "\\n"); | ||||
|         final Exception givenCausingException = new SQLException("dummy"); | ||||
|         given(statement.executeUpdate(anyString())).willThrow(givenCausingException); | ||||
|  | ||||
|         // when | ||||
|         final Throwable actual = catchThrowable(() -> replaceCustomChange.execute(database)); | ||||
|  | ||||
|         // then | ||||
|         assertThat(actual).isInstanceOfSatisfying( | ||||
|                 CustomChangeException.class, | ||||
|                 (cce) -> assertThat(cce.getCause()).isSameAs(givenCausingException)); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void setFileOpenerDoesNothing() { | ||||
|         new ReplaceCustomChange().setFileOpener(null); | ||||
|     } | ||||
|  | ||||
|     @Test | ||||
|     public void validateDoesNothing() { | ||||
|         new ReplaceCustomChange().validate(null); | ||||
|     } | ||||
|  | ||||
|     // --- only test fixture below --- | ||||
|  | ||||
|     private ReplaceCustomChange givenReplaceCustomChange( | ||||
|             final String some_table, | ||||
|             final String columns, | ||||
|             final String searchFor, | ||||
|             final String replaceWith) throws SetupException { | ||||
|         final ReplaceCustomChange replaceCustomChange = new ReplaceCustomChange(); | ||||
|         replaceCustomChange.setUp(); | ||||
|         replaceCustomChange.setTableName(some_table); | ||||
|         replaceCustomChange.setColumnNames(columns); | ||||
|         replaceCustomChange.setSearchFor(searchFor); | ||||
|         replaceCustomChange.setReplaceWith(replaceWith); | ||||
|         return replaceCustomChange; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user