PageRenderTime 28ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/scheduler/OracleClusteredJobParametersTypeFixerTest.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 198 lines | 153 code | 45 blank | 0 comment | 1 complexity | 744b2e89db73c91ba68b6bbeba755215 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.scheduler;
  2. import com.atlassian.jira.config.database.DatabaseConfig;
  3. import com.atlassian.jira.config.database.DatabaseConfigurationManager;
  4. import com.atlassian.jira.config.database.Datasource;
  5. import com.atlassian.jira.junit.rules.MockitoContainer;
  6. import org.junit.Before;
  7. import org.junit.Rule;
  8. import org.junit.Test;
  9. import org.junit.rules.ExpectedException;
  10. import org.mockito.Mock;
  11. import java.sql.Connection;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import static com.atlassian.jira.matchers.EitherMatchers.left;
  15. import static com.atlassian.jira.matchers.EitherMatchers.right;
  16. import static com.atlassian.jira.scheduler.OracleClusteredJobParametersTypeFixer.INDEXES;
  17. import static org.hamcrest.Matchers.instanceOf;
  18. import static org.hamcrest.Matchers.is;
  19. import static org.junit.Assert.assertThat;
  20. import static org.mockito.Matchers.anyString;
  21. import static org.mockito.Mockito.mock;
  22. import static org.mockito.Mockito.verify;
  23. import static org.mockito.Mockito.verifyNoMoreInteractions;
  24. import static org.mockito.Mockito.verifyZeroInteractions;
  25. import static org.mockito.Mockito.when;
  26. public class OracleClusteredJobParametersTypeFixerTest {
  27. @Rule
  28. public MockitoContainer mockitoContainer = new MockitoContainer(this);
  29. @Rule
  30. public ExpectedException thrown = ExpectedException.none();
  31. @Mock
  32. private Connection conn;
  33. @Mock
  34. private Statement statement;
  35. @Mock
  36. private DatabaseConfigurationManager dbConfigManager;
  37. private DatabaseConfig dbConfig;
  38. private String parametersColumnType;
  39. private String schemaName = "";
  40. private OracleClusteredJobParametersTypeFixer fixture;
  41. @Before
  42. public void setUp() throws SQLException {
  43. when(conn.createStatement()).thenReturn(statement);
  44. dbConfig = new DatabaseConfig("oracle10g", "jira_user", mock(Datasource.class));
  45. when(dbConfigManager.isDatabaseSetup()).thenReturn(true);
  46. when(dbConfigManager.getDatabaseConfiguration()).thenAnswer(invocation -> dbConfig);
  47. fixture = new Fixture();
  48. }
  49. @Test
  50. public void doesNothingOnH2() {
  51. givenH2();
  52. assertThat(fixture.asStartupCheck().isOk(), is(true));
  53. assertThat(fixture.getResult(), right(false));
  54. verifyZeroInteractions(statement);
  55. }
  56. @Test
  57. public void fixDoesNothingIfColumnTypeIsBlob() throws SQLException {
  58. givenEverythingFails();
  59. parametersColumnType = "Blob";
  60. assertThat(fixture.asStartupCheck().isOk(), is(true));
  61. assertThat(fixture.getResult(), right(false));
  62. verifyZeroInteractions(statement);
  63. }
  64. @Test
  65. public void fixPerformedWhenColumnTypeIsLongRaw() throws SQLException {
  66. givenEverythingSucceeds();
  67. parametersColumnType = "LONG RAW";
  68. assertThat(fixture.asStartupCheck().isOk(), is(true));
  69. assertThat(fixture.getResult(), right(true));
  70. assertSql("ALTER TABLE clusteredjob MODIFY (parameters BLOB)");
  71. INDEXES.forEach(index -> assertSql("ALTER INDEX " + index + " REBUILD"));
  72. }
  73. @Test
  74. public void applyFixWorksToo() throws SQLException {
  75. givenEverythingSucceeds();
  76. parametersColumnType = "LONG RAW";
  77. assertThat(fixture.fix(), is(true));
  78. assertThat(fixture.getResult(), right(true));
  79. assertSql("ALTER TABLE clusteredjob MODIFY (parameters BLOB)");
  80. INDEXES.forEach(index -> assertSql("ALTER INDEX " + index + " REBUILD"));
  81. }
  82. @Test
  83. public void schemaNameGetsUsedWhenSet() throws SQLException {
  84. schemaName = "JIRA_USER";
  85. givenEverythingSucceeds();
  86. assertThat(fixture.fix(), is(true));
  87. assertThat(fixture.getResult(), right(true));
  88. assertSql("ALTER TABLE JIRA_USER.clusteredjob MODIFY (parameters BLOB)");
  89. INDEXES.forEach(index -> assertSql("ALTER INDEX JIRA_USER." + index + " REBUILD"));
  90. }
  91. @Test
  92. public void alterTableFailuresAreReported() throws SQLException {
  93. givenEverythingFails();
  94. assertThat(fixture.fix(), is(false));
  95. assertThat(fixture.getResult(), left(instanceOf(SQLException.class)));
  96. assertSql("ALTER TABLE clusteredjob MODIFY (parameters BLOB)");
  97. verify(statement).close();
  98. verifyNoMoreInteractions(statement);
  99. }
  100. @Test
  101. public void indexRebuildFailuresAreIgnored() throws SQLException {
  102. givenIndexRebuildsFail();
  103. assertThat(fixture.fix(), is(true));
  104. assertThat(fixture.getResult(), right(true));
  105. assertSql("ALTER TABLE clusteredjob MODIFY (parameters BLOB)");
  106. INDEXES.forEach(index -> assertSql("ALTER INDEX " + index + " REBUILD"));
  107. }
  108. private void givenH2() {
  109. dbConfig = new DatabaseConfig("h2", "public", mock(Datasource.class));
  110. }
  111. private void givenEverythingFails() throws SQLException {
  112. when(statement.execute(anyString())).thenAnswer(invocation -> {
  113. throw new SQLException("Nope!");
  114. });
  115. }
  116. private void givenEverythingSucceeds() throws SQLException {
  117. when(statement.execute(anyString())).thenReturn(true);
  118. }
  119. private void givenIndexRebuildsFail() throws SQLException {
  120. final SQLException sqle = new SQLException("Nope!");
  121. when(statement.execute(anyString())).thenAnswer(invocation -> {
  122. final String sql = (String) invocation.getArguments()[0];
  123. if (sql.contains("REBUILD")) {
  124. throw sqle;
  125. }
  126. return false;
  127. });
  128. }
  129. private void assertSql(String sql) {
  130. try {
  131. verify(statement).execute(sql);
  132. } catch (SQLException e) {
  133. throw new AssertionError(e);
  134. }
  135. }
  136. class Fixture extends OracleClusteredJobParametersTypeFixer {
  137. public Fixture() {
  138. super(dbConfigManager);
  139. }
  140. @Override
  141. Connection getConnection() throws SQLException {
  142. return conn;
  143. }
  144. @Override
  145. String getSchemaName() {
  146. return schemaName;
  147. }
  148. @Override
  149. String getParametersColumnType() {
  150. return parametersColumnType;
  151. }
  152. }
  153. }