|
15 | 15 | import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
16 | 16 | import org.elasticsearch.common.xcontent.XContentParser; |
17 | 17 | import org.elasticsearch.test.AbstractSerializingTestCase; |
| 18 | +import org.elasticsearch.xpack.core.indexlifecycle.Step.StepKey; |
18 | 19 |
|
19 | 20 | import java.io.IOException; |
20 | 21 | import java.util.ArrayList; |
|
27 | 28 | import java.util.function.LongSupplier; |
28 | 29 |
|
29 | 30 | import static org.hamcrest.Matchers.equalTo; |
| 31 | +import static org.hamcrest.Matchers.instanceOf; |
30 | 32 | import static org.mockito.Mockito.mock; |
31 | 33 |
|
32 | 34 | public class LifecyclePolicyTests extends AbstractSerializingTestCase<LifecyclePolicy> { |
@@ -105,38 +107,55 @@ public void testDefaultLifecycleType() { |
105 | 107 | assertSame(TimeseriesLifecycleType.INSTANCE, policy.getType()); |
106 | 108 | } |
107 | 109 |
|
| 110 | + public void testFirstAndLastSteps() { |
| 111 | + Client client = mock(Client.class); |
| 112 | + LongSupplier nowSupplier = () -> 0L; |
| 113 | + lifecycleName = randomAlphaOfLengthBetween(1, 20); |
| 114 | + Map<String, Phase> phases = new LinkedHashMap<>(); |
| 115 | + LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases); |
| 116 | + List<Step> steps = policy.toSteps(client, nowSupplier); |
| 117 | + assertThat(steps.size(), equalTo(2)); |
| 118 | + assertThat(steps.get(0), instanceOf(InitializePolicyContextStep.class)); |
| 119 | + assertThat(steps.get(0).getKey(), equalTo(new StepKey("pre-phase", "pre-action", "init"))); |
| 120 | + assertThat(steps.get(0).getNextStepKey(), equalTo(TerminalPolicyStep.KEY)); |
| 121 | + assertSame(steps.get(1), TerminalPolicyStep.INSTANCE); |
| 122 | + } |
| 123 | + |
108 | 124 | public void testToStepsWithOneStep() { |
109 | 125 | Client client = mock(Client.class); |
110 | 126 | LongSupplier nowSupplier = () -> 0L; |
111 | | - MockStep firstStep = new MockStep(new Step.StepKey("test", "test", "test"), null); |
| 127 | + MockStep mockStep = new MockStep( |
| 128 | + new Step.StepKey("test", "test", "test"), TerminalPolicyStep.KEY); |
112 | 129 |
|
113 | 130 | lifecycleName = randomAlphaOfLengthBetween(1, 20); |
114 | 131 | Map<String, Phase> phases = new LinkedHashMap<>(); |
115 | | - LifecycleAction firstAction = new MockAction(Arrays.asList(firstStep)); |
| 132 | + LifecycleAction firstAction = new MockAction(Arrays.asList(mockStep)); |
116 | 133 | Map<String, LifecycleAction> actions = Collections.singletonMap(MockAction.NAME, firstAction); |
117 | 134 | Phase firstPhase = new Phase("test", TimeValue.ZERO, actions); |
118 | 135 | phases.put(firstPhase.getName(), firstPhase); |
119 | 136 | LifecyclePolicy policy = new LifecyclePolicy(TestLifecycleType.INSTANCE, lifecycleName, phases); |
120 | | - |
| 137 | + StepKey firstStepKey = InitializePolicyContextStep.KEY; |
| 138 | + StepKey secondStepKey = new StepKey("test", "pre-test", "after"); |
121 | 139 | List<Step> steps = policy.toSteps(client, nowSupplier); |
122 | 140 | assertThat(steps.size(), equalTo(4)); |
123 | | - assertThat(steps.get(0).getKey(), equalTo(new Step.StepKey("pre-phase", "pre-action", "init"))); |
124 | | - assertThat(steps.get(0).getNextStepKey(), equalTo(new Step.StepKey("test", "pre-action", "after"))); |
125 | | - assertThat(steps.get(1).getKey(), equalTo(new Step.StepKey("test", "pre-action", "after"))); |
126 | | - assertThat(steps.get(1).getNextStepKey(), equalTo(firstStep.getKey())); |
127 | | - assertThat(steps.get(2), equalTo(firstStep)); |
128 | | - assertNull(steps.get(2).getNextStepKey()); |
| 141 | + assertSame(steps.get(0).getKey(), firstStepKey); |
| 142 | + assertThat(steps.get(0).getNextStepKey(), equalTo(secondStepKey)); |
| 143 | + assertThat(steps.get(1).getKey(), equalTo(secondStepKey)); |
| 144 | + assertThat(steps.get(1).getNextStepKey(), equalTo(mockStep.getKey())); |
| 145 | + assertThat(steps.get(2).getKey(), equalTo(mockStep.getKey())); |
| 146 | + assertThat(steps.get(2).getNextStepKey(), equalTo(TerminalPolicyStep.KEY)); |
| 147 | + assertSame(steps.get(3), TerminalPolicyStep.INSTANCE); |
129 | 148 | } |
130 | 149 |
|
131 | 150 | public void testToStepsWithTwoPhases() { |
132 | 151 | Client client = mock(Client.class); |
133 | 152 | LongSupplier nowSupplier = () -> 0L; |
134 | | - MockStep secondActionStep = new MockStep(new Step.StepKey("second_phase", "test", "test"), null); |
135 | | - MockStep secondAfter = new MockStep(new Step.StepKey("second_phase", "pre-action", "after"), secondActionStep.getKey()); |
136 | | - MockStep firstActionAnotherStep = new MockStep(new Step.StepKey("first_phase", "test", "test"), secondAfter.getKey()); |
137 | | - MockStep firstActionStep = new MockStep(new Step.StepKey("first_phase", "test", "test"), firstActionAnotherStep.getKey()); |
138 | | - MockStep firstAfter = new MockStep(new Step.StepKey("first_phase", "pre-action", "after"), firstActionStep.getKey()); |
139 | | - MockStep init = new MockStep(new Step.StepKey("pre-phase", "pre-action", "init"), firstAfter.getKey()); |
| 153 | + MockStep secondActionStep = new MockStep(new StepKey("second_phase", "test2", "test"), TerminalPolicyStep.KEY); |
| 154 | + MockStep secondAfter = new MockStep(new StepKey("second_phase", "pre-test2", "after"), secondActionStep.getKey()); |
| 155 | + MockStep firstActionAnotherStep = new MockStep(new StepKey("first_phase", "test", "bar"), secondAfter.getKey()); |
| 156 | + MockStep firstActionStep = new MockStep(new StepKey("first_phase", "test", "foo"), firstActionAnotherStep.getKey()); |
| 157 | + MockStep firstAfter = new MockStep(new StepKey("first_phase", "pre-test", "after"), firstActionStep.getKey()); |
| 158 | + MockStep init = new MockStep(InitializePolicyContextStep.KEY, firstAfter.getKey()); |
140 | 159 |
|
141 | 160 | lifecycleName = randomAlphaOfLengthBetween(1, 20); |
142 | 161 | Map<String, Phase> phases = new LinkedHashMap<>(); |
@@ -164,6 +183,6 @@ public void testToStepsWithTwoPhases() { |
164 | 183 | assertThat(steps.get(4).getKey(), equalTo(secondAfter.getKey())); |
165 | 184 | assertThat(steps.get(4).getNextStepKey(), equalTo(secondAfter.getNextStepKey())); |
166 | 185 | assertThat(steps.get(5), equalTo(secondActionStep)); |
167 | | - assertThat(steps.get(6).getClass(), equalTo(TerminalPolicyStep.class)); |
| 186 | + assertSame(steps.get(6), TerminalPolicyStep.INSTANCE); |
168 | 187 | } |
169 | 188 | } |
0 commit comments