8bbf231a166e0b5526c9a01e06e4310f5ee8df7b
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / SynchronizerTaskTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23
24 package org.onap.dmaap.datarouter.provisioning;
25
26 import static org.mockito.Matchers.anyObject;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.powermock.api.mockito.PowerMockito.when;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.net.InetAddress;
36 import java.net.UnknownHostException;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import java.util.concurrent.TimeUnit;
40 import javax.persistence.EntityManager;
41 import javax.persistence.EntityManagerFactory;
42 import javax.persistence.Persistence;
43 import org.apache.commons.lang3.reflect.FieldUtils;
44 import org.apache.http.HttpEntity;
45 import org.apache.http.StatusLine;
46 import org.apache.http.client.methods.CloseableHttpResponse;
47 import org.apache.http.conn.ssl.SSLSocketFactory;
48 import org.apache.http.impl.client.AbstractHttpClient;
49 import org.apache.http.message.BasicHeader;
50 import org.junit.After;
51 import org.junit.AfterClass;
52 import org.junit.Assert;
53 import org.junit.Before;
54 import org.junit.BeforeClass;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.Mockito;
59 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
60 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PowerMockIgnore;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66 @RunWith(PowerMockRunner.class)
67 @PowerMockIgnore("javax.net.ssl.*")
68 @PrepareForTest({BaseServlet.class, URLUtilities.class})
69 public class SynchronizerTaskTest {
70
71     @Mock
72     private AbstractHttpClient httpClient;
73
74     @Mock
75     private HttpEntity httpEntity;
76
77     @Mock
78     private StatusLine statusLine;
79
80     @Mock
81     private CloseableHttpResponse response;
82
83     private SynchronizerTask synchronizerTask;
84     private ExecutorService executorService;
85
86     private static EntityManagerFactory emf;
87     private static EntityManager em;
88
89     @BeforeClass
90     public static void init() {
91         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
92         em = emf.createEntityManager();
93         System.setProperty(
94                 "org.onap.dmaap.datarouter.provserver.properties",
95                 "src/test/resources/h2Database.properties");
96     }
97
98     @AfterClass
99     public static void tearDownClass() {
100         em.clear();
101         em.close();
102         emf.close();
103     }
104
105
106     @Before
107     public void setUp() throws IllegalAccessException, UnknownHostException {
108         SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
109         doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
110
111         PowerMockito.mockStatic(BaseServlet.class);
112         PowerMockito.mockStatic(URLUtilities.class);
113         when(BaseServlet.getPods()).thenReturn(new String[] {InetAddress.getLocalHost().getHostName(), "stand-by-prov"});
114         when(URLUtilities.generatePeerProvURL()).thenReturn("https://stand-by-prov/internal/prov");
115         when(URLUtilities.generatePeerLogsURL()).thenReturn("https://stand-by-prov/internal/drlogs");
116
117         synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
118         doReturn(2).when(synchronizerTask).lookupState();
119
120         executorService = Executors.newCachedThreadPool();
121         executorService.execute(synchronizerTask);
122     }
123
124     @After
125     public void tearDown() throws InterruptedException {
126         executorService.shutdown();
127         executorService.awaitTermination(2, TimeUnit.SECONDS);
128     }
129
130     @Test
131     public void Given_Synch_Task_readRemoteLoglist_Called_And_Valid_BitSet_Returned_Success()
132             throws IOException, IllegalAccessException {
133         mockHttpClientForGetRequest();
134         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
135         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
136         Mockito.when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("1-55251".getBytes()));
137         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
138         Assert.assertNotNull(rleBitSet);
139     }
140
141     @Test
142     public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Resonse_Code_Failure()
143             throws IOException, IllegalAccessException {
144         mockHttpClientForGetRequest();
145         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
146         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
147         Assert.assertNotNull(rleBitSet);
148     }
149
150     @Test
151     public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Content_Type_Failure()
152             throws IOException, IllegalAccessException {
153         mockHttpClientForGetRequest();
154         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
155         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
156         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
157         Assert.assertNotNull(rleBitSet);
158     }
159
160     @Test
161     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Valid_BitSet_Returned_Success()
162             throws IOException, IllegalAccessException {
163         mockHttpClientForGetRequest();
164         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
165         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
166         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
167         synchronizerTask.replicateDataRouterLogs(rleBitSet);
168     }
169
170     @Test
171     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Content_Type_Failure()
172             throws IOException, IllegalAccessException {
173         mockHttpClientForGetRequest();
174         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
175         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
176         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
177         synchronizerTask.replicateDataRouterLogs(rleBitSet);
178     }
179
180     @Test
181     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Resonse_Code_Failure()
182             throws IOException, IllegalAccessException {
183         mockHttpClientForGetRequest();
184         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
185         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
186         synchronizerTask.replicateDataRouterLogs(rleBitSet);
187     }
188
189     @Test
190     public void Given_Synch_Task_Is_Started_And_LogFileLoader_Is_Idle_Then_Standby_Pod_Synch_Is_Successful()
191             throws IOException, IllegalAccessException {
192         mockHttpClientForGetRequest();
193         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
194         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0"));
195         mockResponseFromGet();
196     }
197
198
199     private void mockHttpClientForGetRequest() throws IllegalAccessException, IOException {
200         FieldUtils.writeField(synchronizerTask, "httpclient", httpClient, true);
201         Mockito.when(httpClient.execute(anyObject())).thenReturn(response);
202         Mockito.when(response.getEntity()).thenReturn(httpEntity);
203         Mockito.when(response.getStatusLine()).thenReturn(statusLine);
204
205     }
206
207     private void mockResponseFromGet() throws IOException {
208         InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
209         Mockito.when(httpEntity.getContent()).thenReturn(in);
210     }
211 }