1414import  org .elasticsearch .ExceptionsHelper ;
1515import  org .elasticsearch .ResourceAlreadyExistsException ;
1616import  org .elasticsearch .action .ActionListener ;
17+ import  org .elasticsearch .action .LatchedActionListener ;
1718import  org .elasticsearch .action .bulk .BulkItemResponse ;
1819import  org .elasticsearch .action .bulk .BulkRequest ;
1920import  org .elasticsearch .action .bulk .BulkResponse ;
2324import  org .elasticsearch .action .ingest .PutPipelineRequest ;
2425import  org .elasticsearch .action .search .SearchRequest ;
2526import  org .elasticsearch .action .search .SearchResponse ;
27+ import  org .elasticsearch .action .support .SubscribableListener ;
28+ import  org .elasticsearch .action .support .master .AcknowledgedResponse ;
2629import  org .elasticsearch .common .Strings ;
2730import  org .elasticsearch .common .bytes .BytesReference ;
2831import  org .elasticsearch .common .settings .MockSecureSettings ;
3134import  org .elasticsearch .core .TimeValue ;
3235import  org .elasticsearch .ingest .EnterpriseGeoIpTask ;
3336import  org .elasticsearch .ingest .geoip .direct .DatabaseConfiguration ;
37+ import  org .elasticsearch .ingest .geoip .direct .DeleteDatabaseConfigurationAction ;
3438import  org .elasticsearch .ingest .geoip .direct .PutDatabaseConfigurationAction ;
3539import  org .elasticsearch .persistent .PersistentTasksService ;
3640import  org .elasticsearch .plugins .Plugin ;
4246import  org .elasticsearch .xcontent .XContentBuilder ;
4347import  org .elasticsearch .xcontent .XContentType ;
4448import  org .elasticsearch .xcontent .json .JsonXContent ;
49+ import  org .junit .After ;
4550import  org .junit .ClassRule ;
4651
4752import  java .io .IOException ;
4853import  java .util .Collection ;
4954import  java .util .List ;
5055import  java .util .Map ;
56+ import  java .util .concurrent .CountDownLatch ;
57+ import  java .util .concurrent .TimeUnit ;
5158
5259import  static  org .elasticsearch .ingest .EnterpriseGeoIpTask .ENTERPRISE_GEOIP_DOWNLOADER ;
5360import  static  org .elasticsearch .ingest .geoip .EnterpriseGeoIpDownloaderTaskExecutor .IPINFO_TOKEN_SETTING ;
@@ -59,6 +66,8 @@ public class EnterpriseGeoIpDownloaderIT extends ESIntegTestCase {
5966
6067    private  static  final  String  MAXMIND_DATABASE_TYPE  = "GeoIP2-City" ;
6168    private  static  final  String  IPINFO_DATABASE_TYPE  = "asn" ;
69+     private  static  final  String  MAXMIND_CONFIGURATION  = "test-1" ;
70+     private  static  final  String  IPINFO_CONFIGURATION  = "test-2" ;
6271
6372    @ ClassRule 
6473    public  static  final  EnterpriseGeoIpHttpFixture  fixture  = new  EnterpriseGeoIpHttpFixture (
@@ -145,6 +154,28 @@ public void testEnterpriseDownloaderTask() throws Exception {
145154        });
146155    }
147156
157+     @ After 
158+     public  void  cleanup () throws  InterruptedException  {
159+         /* 
160+          * This method cleans up the database configurations that the test created. This allows the test to be run repeatedly. 
161+          */ 
162+         CountDownLatch  latch  = new  CountDownLatch (1 );
163+         LatchedActionListener <AcknowledgedResponse > listener  = new  LatchedActionListener <>(ActionListener .noop (), latch );
164+         SubscribableListener .<AcknowledgedResponse >newForked (l  -> deleteDatabaseConfiguration (MAXMIND_CONFIGURATION , l ))
165+             .<AcknowledgedResponse >andThen (l  -> deleteDatabaseConfiguration (IPINFO_CONFIGURATION , l ))
166+             .addListener (listener );
167+         latch .await (10 , TimeUnit .SECONDS );
168+     }
169+ 
170+     private  void  deleteDatabaseConfiguration (String  configurationName , ActionListener <AcknowledgedResponse > listener ) {
171+         admin ().cluster ()
172+             .execute (
173+                 DeleteDatabaseConfigurationAction .INSTANCE ,
174+                 new  DeleteDatabaseConfigurationAction .Request (TimeValue .MAX_VALUE , TimeValue .timeValueSeconds (10 ), configurationName ),
175+                 listener 
176+             );
177+     }
178+ 
148179    private  void  startEnterpriseGeoIpDownloaderTask () {
149180        PersistentTasksService  persistentTasksService  = internalCluster ().getInstance (PersistentTasksService .class );
150181        persistentTasksService .sendStartRequest (
@@ -168,7 +199,7 @@ private void configureMaxmindDatabase(String databaseType) {
168199                new  PutDatabaseConfigurationAction .Request (
169200                    TimeValue .MAX_VALUE ,
170201                    TimeValue .MAX_VALUE ,
171-                     new  DatabaseConfiguration ("test-1" , databaseType , new  DatabaseConfiguration .Maxmind ("test_account" ))
202+                     new  DatabaseConfiguration (MAXMIND_CONFIGURATION , databaseType , new  DatabaseConfiguration .Maxmind ("test_account" ))
172203                )
173204            )
174205            .actionGet ();
@@ -181,7 +212,7 @@ private void configureIpinfoDatabase(String databaseType) {
181212                new  PutDatabaseConfigurationAction .Request (
182213                    TimeValue .MAX_VALUE ,
183214                    TimeValue .MAX_VALUE ,
184-                     new  DatabaseConfiguration ("test-2" , databaseType , new  DatabaseConfiguration .Ipinfo ())
215+                     new  DatabaseConfiguration (IPINFO_CONFIGURATION , databaseType , new  DatabaseConfiguration .Ipinfo ())
185216                )
186217            )
187218            .actionGet ();
0 commit comments