PageRenderTime 53ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/digdag-standards/src/test/java/io/digdag/standards/operator/td/TDOperatorTest.java

https://gitlab.com/github-cloud-corp/digdag
Java | 203 lines | 167 code | 32 blank | 4 comment | 0 complexity | 72d65963a755ceaa514bfebf4ec2b134 MD5 | raw file
  1. package io.digdag.standards.operator.td;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import com.fasterxml.jackson.datatype.guava.GuavaModule;
  4. import com.google.common.base.Optional;
  5. import com.treasuredata.client.TDClient;
  6. import com.treasuredata.client.model.TDJob;
  7. import com.treasuredata.client.model.TDJobSummary;
  8. import io.digdag.client.api.JacksonTimeModule;
  9. import io.digdag.client.config.Config;
  10. import io.digdag.client.config.ConfigException;
  11. import io.digdag.client.config.ConfigFactory;
  12. import io.digdag.spi.TaskExecutionException;
  13. import org.junit.Rule;
  14. import org.junit.Test;
  15. import org.junit.rules.ExpectedException;
  16. import org.junit.runner.RunWith;
  17. import org.mockito.Mock;
  18. import org.mockito.runners.MockitoJUnitRunner;
  19. import static org.hamcrest.Matchers.is;
  20. import static org.junit.Assert.assertThat;
  21. import static org.mockito.Matchers.any;
  22. import static org.mockito.Matchers.anyString;
  23. import static org.mockito.Mockito.verify;
  24. import static org.mockito.Mockito.verifyNoMoreInteractions;
  25. import static org.mockito.Mockito.verifyZeroInteractions;
  26. import static org.mockito.Mockito.when;
  27. @RunWith(MockitoJUnitRunner.class)
  28. public class TDOperatorTest
  29. {
  30. @Rule public final ExpectedException exception = ExpectedException.none();
  31. @Mock TDClient client;
  32. @Mock TDOperator.JobStarter jobStarter;
  33. private final ObjectMapper mapper = new ObjectMapper()
  34. .registerModule(new GuavaModule())
  35. .registerModule(new JacksonTimeModule());
  36. private final ConfigFactory configFactory = new ConfigFactory(mapper);
  37. private Config newConfig()
  38. {
  39. return configFactory.create();
  40. }
  41. @Test
  42. public void verifyEmptyDatabaseParameterIsRejected()
  43. throws Exception
  44. {
  45. Config config = newConfig()
  46. .set("database", "")
  47. .set("apikey", "foobar");
  48. exception.expect(ConfigException.class);
  49. TDOperator.fromConfig(config);
  50. }
  51. @Test
  52. public void verifyWhitespaceDatabaseParameterIsRejected()
  53. throws Exception
  54. {
  55. Config config = newConfig()
  56. .set("database", " \t\n")
  57. .set("apikey", "foobar");
  58. exception.expect(ConfigException.class);
  59. TDOperator.fromConfig(config);
  60. }
  61. @Test
  62. public void verifyEmptyApiKeyParameterIsRejected()
  63. throws Exception
  64. {
  65. Config config = newConfig()
  66. .set("database", "foobar")
  67. .set("apikey", "");
  68. exception.expect(ConfigException.class);
  69. TDOperator.fromConfig(config);
  70. }
  71. @Test
  72. public void verifyWhitespaceApiKeyParameterIsRejected()
  73. throws Exception
  74. {
  75. Config config = newConfig()
  76. .set("database", "foobar")
  77. .set("apikey", " \n\t");
  78. exception.expect(ConfigException.class);
  79. TDOperator.fromConfig(config);
  80. }
  81. @Test
  82. public void testFromConfig()
  83. throws Exception
  84. {
  85. Config config = newConfig()
  86. .set("database", "foobar")
  87. .set("apikey", "quux");
  88. TDOperator.fromConfig(config);
  89. }
  90. @Test
  91. public void testRunJob()
  92. throws Exception
  93. {
  94. TDOperator operator = new TDOperator(client, "foobar");
  95. Config state0 = configFactory.create();
  96. String jobStateKey = "fooJob";
  97. // 1. Create domain key
  98. TDOperator.JobState jobState1;
  99. Config state1;
  100. {
  101. TaskExecutionException e = runJobIteration(operator, state0, jobStateKey, jobStarter);
  102. verifyZeroInteractions(jobStarter);
  103. state1 = e.getStateParams(configFactory).get();
  104. assertThat(state1.has(jobStateKey), is(true));
  105. jobState1 = state1.get(jobStateKey, TDOperator.JobState.class);
  106. assertThat(jobState1.domainKey().isPresent(), is(true));
  107. }
  108. // 2. Start job with domain key created above
  109. String jobId = "badf00d4711";
  110. Config state2;
  111. TDOperator.JobState jobState2;
  112. {
  113. when(jobStarter.startJob(any(TDOperator.class), anyString())).thenReturn(jobId);
  114. TaskExecutionException e = runJobIteration(operator, state1, jobStateKey, jobStarter);
  115. state2 = e.getStateParams(configFactory).get();
  116. verify(jobStarter).startJob(operator, jobState1.domainKey().get());
  117. jobState2 = state2.get(jobStateKey, TDOperator.JobState.class);
  118. assertThat(jobState2.jobId(), is(Optional.of(jobId)));
  119. }
  120. // 3. Check job status (RUNNING)
  121. Config state3;
  122. TDOperator.JobState jobState3;
  123. {
  124. when(client.jobStatus(jobId)).thenReturn(summary(jobId, TDJob.Status.RUNNING));
  125. TaskExecutionException e = runJobIteration(operator, state2, jobStateKey, jobStarter);
  126. state3 = e.getStateParams(configFactory).get();
  127. jobState3 = state3.get(jobStateKey, TDOperator.JobState.class);
  128. assertThat(jobState3.pollIteration(), is(Optional.of(1)));
  129. }
  130. // 3. Check job status (SUCCESS)
  131. when(client.jobStatus(jobId)).thenReturn(summary(jobId, TDJob.Status.SUCCESS));
  132. TDJobOperator jobOperator = operator.runJob(state3, jobStateKey, jobStarter);
  133. assertThat(jobOperator.getJobId(), is(jobId));
  134. verifyNoMoreInteractions(jobStarter);
  135. }
  136. @Test
  137. public void testRunJobMigrateState()
  138. throws Exception
  139. {
  140. TDOperator operator = new TDOperator(client, "foobar");
  141. Config state0 = configFactory.create();
  142. String jobId = "4711";
  143. String domainKey = "badf00d";
  144. int pollIteration = 17;
  145. state0.set("jobId", jobId);
  146. state0.set("domainKey", domainKey);
  147. state0.set("pollIteration", pollIteration);
  148. when(client.jobStatus(jobId)).thenReturn(summary(jobId, TDJob.Status.RUNNING));
  149. TaskExecutionException e = runJobIteration(operator, state0, "foobar", jobStarter);
  150. Config state1 = e.getStateParams(configFactory).get();
  151. assertThat(state1.has("jobId"), is(false));
  152. assertThat(state1.has("domainKey"), is(false));
  153. assertThat(state1.has("pollIteration"), is(false));
  154. assertThat(state1.get("foobar", TDOperator.JobState.class), is(TDOperator.JobState.empty()
  155. .withJobId(jobId)
  156. .withDomainKey(domainKey)
  157. .withPollIteration(pollIteration + 1)));
  158. }
  159. private TDJobSummary summary(String jobId, TDJob.Status status) {return new TDJobSummary(status, 0, 0, jobId, "", "", "", "");}
  160. private static TaskExecutionException runJobIteration(TDOperator operator, Config state, String key, TDOperator.JobStarter jobStarter)
  161. {
  162. try {
  163. operator.runJob(state, key, jobStarter);
  164. }
  165. catch (TaskExecutionException e) {
  166. assertThat(e.getRetryInterval().isPresent(), is(true));
  167. return e;
  168. }
  169. return null;
  170. }
  171. }