38
38
public class TransformInternalIndexTests extends ESTestCase {
39
39
40
40
public static ClusterState STATE_WITH_LATEST_VERSIONED_INDEX_TEMPLATE ;
41
+ public static ClusterState STATE_WITH_LATEST_AUDIT_INDEX_TEMPLATE ;
41
42
42
43
static {
43
44
ImmutableOpenMap .Builder <String , IndexTemplateMetaData > mapBuilder = ImmutableOpenMap .builder ();
@@ -51,6 +52,18 @@ public class TransformInternalIndexTests extends ESTestCase {
51
52
ClusterState .Builder csBuilder = ClusterState .builder (ClusterName .DEFAULT );
52
53
csBuilder .metaData (metaBuilder .build ());
53
54
STATE_WITH_LATEST_VERSIONED_INDEX_TEMPLATE = csBuilder .build ();
55
+
56
+ mapBuilder = ImmutableOpenMap .builder ();
57
+ try {
58
+ mapBuilder .put (TransformInternalIndexConstants .AUDIT_INDEX , TransformInternalIndex .getAuditIndexTemplateMetaData ());
59
+ } catch (IOException e ) {
60
+ throw new UncheckedIOException (e );
61
+ }
62
+ metaBuilder = MetaData .builder ();
63
+ metaBuilder .templates (mapBuilder .build ());
64
+ csBuilder = ClusterState .builder (ClusterName .DEFAULT );
65
+ csBuilder .metaData (metaBuilder .build ());
66
+ STATE_WITH_LATEST_AUDIT_INDEX_TEMPLATE = csBuilder .build ();
54
67
}
55
68
56
69
public void testHaveLatestVersionedIndexTemplate () {
@@ -81,8 +94,7 @@ public void testInstallLatestVersionedIndexTemplateIfRequired_GivenRequired() {
81
94
when (clusterService .state ()).thenReturn (ClusterState .EMPTY_STATE );
82
95
83
96
IndicesAdminClient indicesClient = mock (IndicesAdminClient .class );
84
- doAnswer (
85
- invocationOnMock -> {
97
+ doAnswer (invocationOnMock -> {
86
98
@ SuppressWarnings ("unchecked" )
87
99
ActionListener <AcknowledgedResponse > listener = (ActionListener <AcknowledgedResponse >) invocationOnMock .getArguments ()[1 ];
88
100
listener .onResponse (new AcknowledgedResponse (true ));
@@ -112,4 +124,100 @@ public void testInstallLatestVersionedIndexTemplateIfRequired_GivenRequired() {
112
124
verify (indicesClient , times (1 )).putTemplate (any (), any ());
113
125
verifyNoMoreInteractions (indicesClient );
114
126
}
127
+
128
+ public void testHaveLatestAuditIndexTemplate () {
129
+
130
+ assertTrue (TransformInternalIndex .haveLatestAuditIndexTemplate (STATE_WITH_LATEST_AUDIT_INDEX_TEMPLATE ));
131
+ assertFalse (TransformInternalIndex .haveLatestAuditIndexTemplate (ClusterState .EMPTY_STATE ));
132
+ }
133
+
134
+ public void testInstallLatestAuditIndexTemplateIfRequired_GivenNotRequired () {
135
+
136
+ ClusterService clusterService = mock (ClusterService .class );
137
+ when (clusterService .state ()).thenReturn (TransformInternalIndexTests .STATE_WITH_LATEST_AUDIT_INDEX_TEMPLATE );
138
+
139
+ Client client = mock (Client .class );
140
+
141
+ AtomicBoolean gotResponse = new AtomicBoolean (false );
142
+ ActionListener <Void > testListener = ActionListener .wrap (aVoid -> gotResponse .set (true ), e -> fail (e .getMessage ()));
143
+
144
+ TransformInternalIndex .installLatestAuditIndexTemplateIfRequired (clusterService , client , testListener );
145
+
146
+ assertTrue (gotResponse .get ());
147
+ verifyNoMoreInteractions (client );
148
+ }
149
+
150
+ public void testInstallLatestAuditIndexTemplateIfRequired_GivenRequired () {
151
+
152
+ ClusterService clusterService = mock (ClusterService .class );
153
+ when (clusterService .state ()).thenReturn (ClusterState .EMPTY_STATE );
154
+
155
+ IndicesAdminClient indicesClient = mock (IndicesAdminClient .class );
156
+ doAnswer (invocationOnMock -> {
157
+ @ SuppressWarnings ("unchecked" )
158
+ ActionListener <AcknowledgedResponse > listener = (ActionListener <AcknowledgedResponse >) invocationOnMock .getArguments ()[1 ];
159
+ listener .onResponse (new AcknowledgedResponse (true ));
160
+ return null ;
161
+ }).when (indicesClient ).putTemplate (any (), any ());
162
+
163
+ AdminClient adminClient = mock (AdminClient .class );
164
+ when (adminClient .indices ()).thenReturn (indicesClient );
165
+ Client client = mock (Client .class );
166
+ when (client .admin ()).thenReturn (adminClient );
167
+
168
+ ThreadPool threadPool = mock (ThreadPool .class );
169
+ when (threadPool .getThreadContext ()).thenReturn (new ThreadContext (Settings .EMPTY ));
170
+ when (client .threadPool ()).thenReturn (threadPool );
171
+
172
+ AtomicBoolean gotResponse = new AtomicBoolean (false );
173
+ ActionListener <Void > testListener = ActionListener .wrap (aVoid -> gotResponse .set (true ), e -> fail (e .getMessage ()));
174
+
175
+ TransformInternalIndex .installLatestAuditIndexTemplateIfRequired (clusterService , client , testListener );
176
+
177
+ assertTrue (gotResponse .get ());
178
+ verify (client , times (1 )).threadPool ();
179
+ verify (client , times (1 )).admin ();
180
+ verifyNoMoreInteractions (client );
181
+ verify (adminClient , times (1 )).indices ();
182
+ verifyNoMoreInteractions (adminClient );
183
+ verify (indicesClient , times (1 )).putTemplate (any (), any ());
184
+ verifyNoMoreInteractions (indicesClient );
185
+ }
186
+
187
+ public void testInstallLatestIndexTemplateIfRequired_GivenRequired () {
188
+
189
+ ClusterService clusterService = mock (ClusterService .class );
190
+ when (clusterService .state ()).thenReturn (ClusterState .EMPTY_STATE );
191
+
192
+ IndicesAdminClient indicesClient = mock (IndicesAdminClient .class );
193
+ doAnswer (invocationOnMock -> {
194
+ @ SuppressWarnings ("unchecked" )
195
+ ActionListener <AcknowledgedResponse > listener = (ActionListener <AcknowledgedResponse >) invocationOnMock .getArguments ()[1 ];
196
+ listener .onResponse (new AcknowledgedResponse (true ));
197
+ return null ;
198
+ }).when (indicesClient ).putTemplate (any (), any ());
199
+
200
+ AdminClient adminClient = mock (AdminClient .class );
201
+ when (adminClient .indices ()).thenReturn (indicesClient );
202
+ Client client = mock (Client .class );
203
+ when (client .admin ()).thenReturn (adminClient );
204
+
205
+ ThreadPool threadPool = mock (ThreadPool .class );
206
+ when (threadPool .getThreadContext ()).thenReturn (new ThreadContext (Settings .EMPTY ));
207
+ when (client .threadPool ()).thenReturn (threadPool );
208
+
209
+ AtomicBoolean gotResponse = new AtomicBoolean (false );
210
+ ActionListener <Void > testListener = ActionListener .wrap (aVoid -> gotResponse .set (true ), e -> fail (e .getMessage ()));
211
+
212
+ TransformInternalIndex .installLatestIndexTemplatesIfRequired (clusterService , client , testListener );
213
+
214
+ assertTrue (gotResponse .get ());
215
+ verify (client , times (2 )).threadPool ();
216
+ verify (client , times (2 )).admin ();
217
+ verifyNoMoreInteractions (client );
218
+ verify (adminClient , times (2 )).indices ();
219
+ verifyNoMoreInteractions (adminClient );
220
+ verify (indicesClient , times (2 )).putTemplate (any (), any ());
221
+ verifyNoMoreInteractions (indicesClient );
222
+ }
115
223
}
0 commit comments