Increasing test coverage
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / sync / ElasticSearchIndexCleanerTest.java
1 package org.onap.aai.sparky.sync;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.mockito.Mockito;
8 import org.onap.aai.restclient.client.OperationResult;
9 import org.onap.aai.sparky.dal.ElasticSearchAdapter;
10 import org.onap.aai.sparky.sync.config.ElasticSearchEndpointConfig;
11 import org.onap.aai.sparky.sync.config.ElasticSearchSchemaConfig;
12 import org.onap.aai.sparky.sync.enumeration.OperationState;
13 import org.onap.aai.sparky.util.TestResourceLoader;
14
15 public class ElasticSearchIndexCleanerTest {
16
17         private ElasticSearchIndexCleaner esIndexCleaner;
18
19         private ElasticSearchAdapter esAdapter;
20         private ElasticSearchEndpointConfig esRestEndpointConfig;
21         private ElasticSearchSchemaConfig esSchemaConfig;
22
23         @Before
24         public void init() throws Exception {
25
26                 esAdapter = Mockito.mock( ElasticSearchAdapter.class);
27                 esRestEndpointConfig = new ElasticSearchEndpointConfig();
28                 esSchemaConfig = new ElasticSearchSchemaConfig();
29                 
30                 esRestEndpointConfig.setScrollContextBatchRequestSize(5000);
31                 esRestEndpointConfig.setEsIpAddress("127.0.0.1");
32                 esRestEndpointConfig.setEsServerPort("9200");
33                 esRestEndpointConfig.setScrollContextTimeToLiveInMinutes(5);
34                 
35         }
36
37         @Test
38         public void validateBasicConstruction() throws Exception {
39                 esIndexCleaner = new ElasticSearchIndexCleaner(esAdapter, esRestEndpointConfig, esSchemaConfig);
40         }
41         
42         @Test
43         public void validatePreOperationCollection() throws Exception {
44
45                 String beforeSyncScrollApiResponse = TestResourceLoader.getTestResourceDataJson(
46                                 "/sync/ElasticSearch/BeforeSync_ElasticSearch_ScrollApi_Successful.json");
47
48                 OperationResult scrollApiOpResult = new OperationResult();
49                 scrollApiOpResult.setResultCode(200);
50                 scrollApiOpResult.setResult(beforeSyncScrollApiResponse);
51
52                 Mockito.when(esAdapter.doPost(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject()))
53                                 .thenReturn(scrollApiOpResult);
54
55                 esIndexCleaner = new ElasticSearchIndexCleaner(esAdapter, esRestEndpointConfig, esSchemaConfig);
56                 OperationState opState = esIndexCleaner.populatePreOperationCollection();
57
58                 assertEquals(OperationState.OK, opState);
59
60         }
61         
62         @Test
63         public void validatePostOperationCollection() throws Exception {
64
65                 String afterSyncScrollApiResponse = TestResourceLoader.getTestResourceDataJson(
66                                 "/sync/ElasticSearch/AfterSync_ElasticSearch_ScrollApi_Successful.json");
67
68                 OperationResult scrollApiOpResult = new OperationResult();
69                 scrollApiOpResult.setResultCode(200);
70                 scrollApiOpResult.setResult(afterSyncScrollApiResponse);
71
72                 Mockito.when(esAdapter.doPost(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject()))
73                                 .thenReturn(scrollApiOpResult);
74
75                 esIndexCleaner = new ElasticSearchIndexCleaner(esAdapter, esRestEndpointConfig, esSchemaConfig);
76                 OperationState opState = esIndexCleaner.populatePostOperationCollection();
77
78                 assertEquals(OperationState.OK, opState);
79
80         }
81         
82
83         @Test
84         public void validatePerformCleanup() throws Exception {
85
86                 String beforeSyncScrollApiResponse = TestResourceLoader.getTestResourceDataJson(
87                                 "/sync/ElasticSearch/BeforeSync_ElasticSearch_ScrollApi_Successful.json");
88
89                 OperationResult beforeScrollApiOpResult = new OperationResult();
90                 beforeScrollApiOpResult.setResultCode(200);
91                 beforeScrollApiOpResult.setResult(beforeSyncScrollApiResponse);
92
93                 String afterSyncScrollApiResponse = TestResourceLoader.getTestResourceDataJson(
94                                 "/sync/ElasticSearch/AfterSync_ElasticSearch_ScrollApi_Successful.json");
95                 
96                 OperationResult afterScrollApiOpResult = new OperationResult();
97                 afterScrollApiOpResult.setResultCode(200);
98                 afterScrollApiOpResult.setResult(afterSyncScrollApiResponse);
99
100                 Mockito.when(esAdapter.doPost(Mockito.anyString(), Mockito.anyString(), Mockito.anyObject()))
101                                 .thenReturn(beforeScrollApiOpResult,afterScrollApiOpResult);
102                 
103                 esIndexCleaner = new ElasticSearchIndexCleaner(esAdapter, esRestEndpointConfig, esSchemaConfig);
104                 
105                 OperationState beforeOpState = esIndexCleaner.populatePreOperationCollection();
106                 OperationState afterOpState = esIndexCleaner.populatePostOperationCollection();
107
108                 assertEquals(OperationState.OK, beforeOpState);
109                 assertEquals(OperationState.OK, afterOpState);
110                 
111                 /*
112                  * Now we can start the test work
113                  */
114                 
115                 OperationState cleanupState = esIndexCleaner.performCleanup();
116                 assertEquals(OperationState.OK, cleanupState);
117                 
118
119         }
120
121
122 }