PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/testsuite/server-technical-testsuite/src/test/java/org/marvelution/jji/data/access/model/v3/T13_CopyAutomationAppDataIT.java

https://bitbucket.org/marvelution/jira-jenkins-integration
Java | 225 lines | 188 code | 20 blank | 17 comment | 0 complexity | f8d963d552ac842bd3924f6f5be2cc86 MD5 | raw file
  1. /*
  2. * Copyright (c) 2012-present Marvelution B.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.marvelution.jji.data.access.model.v3;
  17. import java.time.*;
  18. import org.marvelution.jji.api.*;
  19. import org.marvelution.jji.api.utils.*;
  20. import org.marvelution.jji.automation.*;
  21. import org.marvelution.jji.automation.api.execution.*;
  22. import org.marvelution.jji.data.access.*;
  23. import org.marvelution.jji.data.access.model.*;
  24. import org.marvelution.jji.test.data.*;
  25. import org.marvelution.jji.test.data.converters.*;
  26. import org.marvelution.testing.*;
  27. import org.marvelution.testing.inject.*;
  28. import com.atlassian.pocketknife.api.querydsl.*;
  29. import com.atlassian.pocketknife.api.querydsl.util.*;
  30. import com.google.inject.Module;
  31. import com.google.inject.*;
  32. import net.java.ao.*;
  33. import net.java.ao.builder.*;
  34. import net.java.ao.schema.*;
  35. import net.java.ao.test.jdbc.*;
  36. import org.junit.jupiter.api.*;
  37. import org.junit.jupiter.api.extension.*;
  38. import static org.marvelution.jji.data.access.model.v3.T13_CopyAutomationAppData.*;
  39. import static org.assertj.core.api.Assertions.*;
  40. import static org.mockito.Mockito.*;
  41. @ExtendWith({ ActiveObjectsExtension.class, InjectorExtension.class })
  42. @Data(T13_CopyAutomationAppDataIT.AutomationAppTablesLoader.class)
  43. public class T13_CopyAutomationAppDataIT
  44. extends TestSupport
  45. implements Module
  46. {
  47. private final ActiveObjectsExtension.EntityManagerContext entityManagerContext;
  48. @Inject
  49. private DatabaseAccessor databaseAccessor;
  50. @Inject
  51. private ServerPersistedRuleDAO ruleDAO;
  52. @Inject
  53. private ServerPersistedRuleExecutionResultDAO resultDAO;
  54. @Inject
  55. private T13_CopyAutomationAppData underTest;
  56. public T13_CopyAutomationAppDataIT(ActiveObjectsExtension.EntityManagerContext entityManagerContext)
  57. {
  58. this.entityManagerContext = entityManagerContext;
  59. }
  60. @Override
  61. public void configure(Binder binder)
  62. {
  63. binder.install(new DataAccessModule(entityManagerContext));
  64. binder.bind(T13_CopyAutomationAppData.class).in(Singleton.class);
  65. binder.bind(AddonHelper.class).toInstance(mock(AddonHelper.class));
  66. binder.bind(ReloadableRulesEngine.class).toInstance(mock(ReloadableRulesEngine.class));
  67. }
  68. @Test
  69. void testUpgrade_NoExistingData()
  70. {
  71. assertThat(ruleDAO.getRules()).isEmpty();
  72. underTest.doUpgrade();
  73. assertThat(ruleDAO.getRules()).isEmpty();
  74. }
  75. @Test
  76. void testUpgrade_ExistingData()
  77. {
  78. loadAutomationAppData();
  79. assertThat(ruleDAO.getRules()).isEmpty();
  80. underTest.doUpgrade();
  81. assertThat(ruleDAO.getRules()).hasSize(2)
  82. .extracting(PersistedRule::getId, PersistedRule::getName, PersistedRule::isEnabled, PersistedRule::getDescription)
  83. .containsOnly(tuple("rule-1", "Rule 1", true, "Test Rule 1"), tuple("rule-2", "Rule 2", false, "Test Rule 2"));
  84. assertThat(resultDAO.getResults("rule-1", 0, 100)).hasSize(2);
  85. assertThat(resultDAO.getResults("rule-2", 0, 100)).hasSize(1);
  86. }
  87. public void loadAutomationAppData()
  88. {
  89. PersistedRuleTable ruleTable = new PersistedRuleTable(OLD_NAMESPACE);
  90. RuleExecutionResultTable resultTable = new RuleExecutionResultTable(OLD_NAMESPACE);
  91. RuleExecutionQueueTable queueTable = new RuleExecutionQueueTable(OLD_NAMESPACE);
  92. databaseAccessor.runInTransaction(conn -> {
  93. conn.insert(ruleTable)
  94. .set(ruleTable.id(), "rule-1")
  95. .set(ruleTable.name(), "Rule 1")
  96. .set(ruleTable.enabled(), true)
  97. .set(ruleTable.description(), "Test Rule 1")
  98. .set(ruleTable.whenJson(), "{\"handlerId\":\"build.synchronized\",\"configuration\":{}}")
  99. .set(ruleTable.ifJson(), "[{\"conditionId\":\"build.of.job\",\"configuration\":{\"jobIds\":\"job-1\"}}]")
  100. .set(ruleTable.thenJson(), "[{\"actionId\":\"transition.issue\",\"configuration\":{\"target_status\":\"10000\"," +
  101. "\"transition_match_pattern\":\"\",\"issue_selector\":\"linkedToBuild\"," +
  102. "\"issue_selector_expression\":\"\",\"comment\":\"\",\"advanced_fields\":\"\"}}]")
  103. .addBatch()
  104. .set(ruleTable.id(), "rule-2")
  105. .set(ruleTable.name(), "Rule 2")
  106. .set(ruleTable.enabled(), false)
  107. .set(ruleTable.description(), "Test Rule 2")
  108. .set(ruleTable.whenJson(), "{\"handlerId\":\"build.synchronized\",\"configuration\":{}}")
  109. .set(ruleTable.ifJson(), (String) null)
  110. .set(ruleTable.thenJson(), "[]")
  111. .addBatch()
  112. .execute();
  113. conn.insert(resultTable)
  114. .set(resultTable.id(), "result-1")
  115. .set(resultTable.ruleId(), "rule-2")
  116. .set(resultTable.executionTimestamp(), TimeUtils.toMillis(LocalDateTime.now()))
  117. .set(resultTable.durationInMillis(), TimeUtils.toMillis(Duration.ofMillis(100)))
  118. .set(resultTable.eventJson(), "{\"handlerId\":\"build.synchronized\",\"eventType\":\"org.marvelution.jji.events" +
  119. ".BuildSynchronizedEvent\",\"event\":{\"syncable\":{\"id\":\"8620460e6ed34a4ba41e142f5d5c06ed\"," +
  120. "\"job\":{\"id\":\"c77d997b73dd4503b783b0c42e3ef03f\",\"site\":{\"id\":\"bcf0479d2cc04ce29272ce61a428d682\"," +
  121. "\"name\":\"Local\",\"sharedSecret\":\"#~OBFUSCATED~#\",\"type\":\"JENKINS\"," +
  122. "\"rpcUrl\":\"https://6f56da141706.eu.ngrok.io/\",\"useCrumbs\":true,\"jenkinsPluginInstalled\":true," +
  123. "\"registrationComplete\":true,\"autoLinkNewJobs\":true,\"hasJobs\":false,\"hasDeletedJobs\":false," +
  124. "\"firewalled\":true},\"name\":\"testing\",\"fullName\":\"testing\",\"urlName\":\"testing\"," +
  125. "\"displayUrl\":\"https://6f56da141706.eu.ngrok.io/job/testing/\",\"lastBuild\":1,\"oldestBuild\":-1," +
  126. "\"linked\":true,\"deleted\":false},\"number\":1,\"displayUrl\":\"https://6f56da141706.eu.ngrok" +
  127. ".io/job/testing/1/\",\"deleted\":false,\"cause\":\"Started by user admin@example.com\",\"building\":false," +
  128. "\"result\":\"SUCCESS\",\"builtOn\":\"master\",\"duration\":100,\"timestamp\":1599490625972,\"branches\":[]," +
  129. "\"changeSet\":[],\"deploymentEnvironments\":[]},\"timestamp\":{\"date\":{\"year\":2020,\"month\":9," +
  130. "\"day\":7},\"time\":{\"hour\":14,\"minute\":57,\"second\":7,\"nano\":87595000}}}}")
  131. .set(resultTable.conditionResultsJson(), "[]")
  132. .set(resultTable.actionResultsJson(), "[]")
  133. .addBatch()
  134. .set(resultTable.id(), "result-2")
  135. .set(resultTable.ruleId(), "rule-1")
  136. .set(resultTable.executionTimestamp(), TimeUtils.toMillis(LocalDateTime.now()))
  137. .set(resultTable.durationInMillis(), TimeUtils.toMillis(Duration.ofMillis(100)))
  138. .set(resultTable.eventJson(), "{\"handlerId\":\"build.synchronized\",\"eventType\":\"org.marvelution.jji.events" +
  139. ".BuildSynchronizedEvent\",\"event\":{\"syncable\":{\"id\":\"8620460e6ed34a4ba41e142f5d5c06ed\"," +
  140. "\"job\":{\"id\":\"c77d997b73dd4503b783b0c42e3ef03f\",\"site\":{\"id\":\"bcf0479d2cc04ce29272ce61a428d682\"," +
  141. "\"name\":\"Local\",\"sharedSecret\":\"#~OBFUSCATED~#\",\"type\":\"JENKINS\"," +
  142. "\"rpcUrl\":\"https://6f56da141706.eu.ngrok.io/\",\"useCrumbs\":true,\"jenkinsPluginInstalled\":true," +
  143. "\"registrationComplete\":true,\"autoLinkNewJobs\":true,\"hasJobs\":false,\"hasDeletedJobs\":false," +
  144. "\"firewalled\":true},\"name\":\"testing\",\"fullName\":\"testing\",\"urlName\":\"testing\"," +
  145. "\"displayUrl\":\"https://6f56da141706.eu.ngrok.io/job/testing/\",\"lastBuild\":1,\"oldestBuild\":-1," +
  146. "\"linked\":true,\"deleted\":false},\"number\":1,\"displayUrl\":\"https://6f56da141706.eu.ngrok" +
  147. ".io/job/testing/1/\",\"deleted\":false,\"cause\":\"Started by user admin@example.com\",\"building\":false," +
  148. "\"result\":\"SUCCESS\",\"builtOn\":\"master\",\"duration\":100,\"timestamp\":1599490625972,\"branches\":[]," +
  149. "\"changeSet\":[],\"deploymentEnvironments\":[]},\"timestamp\":{\"date\":{\"year\":2020,\"month\":9," +
  150. "\"day\":7},\"time\":{\"hour\":14,\"minute\":57,\"second\":7,\"nano\":87595000}}}}")
  151. .set(resultTable.conditionResultsJson(), "[{\"conditionId\":\"build.of.job\",\"duration\":2,\"matches\":false}]")
  152. .set(resultTable.actionResultsJson(), "[]")
  153. .addBatch()
  154. .set(resultTable.id(), "result-3")
  155. .set(resultTable.ruleId(), "rule-1")
  156. .set(resultTable.executionTimestamp(), TimeUtils.toMillis(LocalDateTime.now()))
  157. .set(resultTable.durationInMillis(), TimeUtils.toMillis(Duration.ofMillis(100)))
  158. .set(resultTable.eventJson(), "{\"handlerId\":\"build.synchronized\",\"eventType\":\"org.marvelution.jji.events" +
  159. ".BuildSynchronizedEvent\",\"event\":{\"syncable\":{\"id\":\"8620460e6ed34a4ba41e142f5d5c06ed\"," +
  160. "\"job\":{\"id\":\"job-1\",\"site\":{\"id\":\"bcf0479d2cc04ce29272ce61a428d682\",\"name\":\"Local\"," +
  161. "\"sharedSecret\":\"#~OBFUSCATED~#\",\"type\":\"JENKINS\",\"rpcUrl\":\"https://6f56da141706.eu.ngrok.io/\"," +
  162. "\"useCrumbs\":true,\"jenkinsPluginInstalled\":true,\"registrationComplete\":true,\"autoLinkNewJobs\":true," +
  163. "\"hasJobs\":false,\"hasDeletedJobs\":false,\"firewalled\":true},\"name\":\"testing\"," +
  164. "\"fullName\":\"testing\",\"urlName\":\"testing\",\"displayUrl\":\"https://6f56da141706.eu.ngrok" +
  165. ".io/job/testing/\",\"lastBuild\":1,\"oldestBuild\":-1,\"linked\":true,\"deleted\":false},\"number\":1," +
  166. "\"displayUrl\":\"https://6f56da141706.eu.ngrok.io/job/testing/1/\",\"deleted\":false,\"cause\":\"Started by " +
  167. "user admin@example.com\",\"building\":false,\"result\":\"SUCCESS\",\"builtOn\":\"master\",\"duration\":100," +
  168. "\"timestamp\":1599490625972,\"branches\":[],\"changeSet\":[],\"deploymentEnvironments\":[]}," +
  169. "\"timestamp\":{\"date\":{\"year\":2020,\"month\":9,\"day\":7},\"time\":{\"hour\":14,\"minute\":57," +
  170. "\"second\":7,\"nano\":87595000}}}}")
  171. .set(resultTable.conditionResultsJson(), "[{\"conditionId\":\"build.of.job\",\"duration\":2,\"matches\":true}]")
  172. .set(resultTable.actionResultsJson(), "[{\"actionId\":\"transition.issue\",\"duration\":2813,\"state\":\"SUCCESS\"," +
  173. "\"messages\":[{\"message\":\"rule.action.transition.issue.transitioned.succeeded\"," +
  174. "\"arguments\":[\"DEV-15\",\"To Do\"]},{\"message\":\"rule.action.transition.issue.transitioned.succeeded\"," +
  175. "\"arguments\":[\"DEV-12\",\"To Do\"]},{\"message\":\"rule.action.transition.issue.transitioned.succeeded\"," +
  176. "\"arguments\":[\"DEV-11\",\"To Do\"]},{\"message\":\"rule.action.transition.issue.transitioned.succeeded\"," +
  177. "\"arguments\":[\"DEV-14\",\"To Do\"]},{\"message\":\"rule.action.transition.issue.transitioned.succeeded\"," +
  178. "\"arguments\":[\"DEV-13\",\"To Do\"]}]}]")
  179. .addBatch()
  180. .execute();
  181. return null;
  182. }, OnRollback.NOOP);
  183. }
  184. public static class AutomationAppTablesLoader
  185. extends ModelDatabaseUpdater
  186. {
  187. @Override
  188. public void update(EntityManager entityManager)
  189. throws Exception
  190. {
  191. // Migrate Integration entities
  192. super.update(entityManager);
  193. // Migrate Automation entities
  194. SchemaGenerator.migrate(entityManager.getProvider(), entityManager.getSchemaConfiguration(),
  195. getNameConverters(entityManager.getNameConverters()), false, PersistedRuleEntity.class,
  196. PersistedRuleExecutionResultEntity.class, RuleExecutionQueueEntity.class);
  197. }
  198. private NameConverters getNameConverters(NameConverters nameConverters)
  199. {
  200. return new SimpleNameConverters(new PrefixesTableNameConverter(OLD_NAMESPACE), nameConverters.getFieldNameConverter(),
  201. nameConverters.getSequenceNameConverter(), nameConverters.getTriggerNameConverter(),
  202. nameConverters.getIndexNameConverter(), nameConverters.getUniqueNameConverter());
  203. }
  204. }
  205. }