Refactor Prov DB handling
[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 javax.persistence.EntityManager;
38 import javax.persistence.EntityManagerFactory;
39 import javax.persistence.Persistence;
40 import org.apache.commons.lang3.reflect.FieldUtils;
41 import org.apache.http.HttpEntity;
42 import org.apache.http.StatusLine;
43 import org.apache.http.client.methods.CloseableHttpResponse;
44 import org.apache.http.conn.ssl.SSLSocketFactory;
45 import org.apache.http.impl.client.AbstractHttpClient;
46 import org.apache.http.message.BasicHeader;
47 import org.junit.After;
48 import org.junit.AfterClass;
49 import org.junit.Assert;
50 import org.junit.Before;
51 import org.junit.BeforeClass;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
57 import org.onap.dmaap.datarouter.provisioning.utils.SynchronizerTask;
58 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
59 import org.powermock.api.mockito.PowerMockito;
60 import org.powermock.core.classloader.annotations.PowerMockIgnore;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
63
64 @RunWith(PowerMockRunner.class)
65 @PowerMockIgnore("javax.net.ssl.*")
66 @PrepareForTest({BaseServlet.class, URLUtilities.class})
67 public class SynchronizerTaskTest {
68
69     @Mock
70     private AbstractHttpClient httpClient;
71
72     @Mock
73     private HttpEntity httpEntity;
74
75     @Mock
76     private StatusLine statusLine;
77
78     @Mock
79     private CloseableHttpResponse response;
80
81     private SynchronizerTask synchronizerTask;
82
83     private static EntityManagerFactory emf;
84     private static EntityManager em;
85
86     @BeforeClass
87     public static void init() {
88         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
89         em = emf.createEntityManager();
90         System.setProperty(
91                 "org.onap.dmaap.datarouter.provserver.properties",
92                 "src/test/resources/h2Database.properties");
93     }
94
95     @AfterClass
96     public static void tearDownClass() {
97         em.clear();
98         em.close();
99         emf.close();
100     }
101
102
103     @Before
104     public void setUp() throws IllegalAccessException, UnknownHostException {
105         SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
106         doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
107
108         PowerMockito.mockStatic(BaseServlet.class);
109         PowerMockito.mockStatic(URLUtilities.class);
110         when(BaseServlet.getPods()).thenReturn(new String[] {InetAddress.getLocalHost().getHostName(), "stand-by-prov"});
111         when(URLUtilities.generatePeerProvURL()).thenReturn("https://stand-by-prov/internal/prov");
112         when(URLUtilities.generatePeerLogsURL()).thenReturn("https://stand-by-prov/internal/drlogs");
113
114         synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
115         doReturn(2).when(synchronizerTask).lookupState();
116     }
117
118     @After
119     public void tearDown() {
120     }
121
122     @Test
123     public void Given_Synch_Task_readRemoteLoglist_Called_And_Valid_BitSet_Returned_Success()
124             throws IOException, IllegalAccessException {
125         mockHttpClientForGetRequest();
126         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
127         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
128         Mockito.when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("1-55251".getBytes()));
129         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
130         Assert.assertNotNull(rleBitSet);
131     }
132
133     @Test
134     public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Resonse_Code_Failure()
135             throws IOException, IllegalAccessException {
136         mockHttpClientForGetRequest();
137         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
138         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
139         Assert.assertNotNull(rleBitSet);
140     }
141
142     @Test
143     public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Content_Type_Failure()
144             throws IOException, IllegalAccessException {
145         mockHttpClientForGetRequest();
146         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
147         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
148         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
149         Assert.assertNotNull(rleBitSet);
150     }
151
152     @Test
153     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Valid_BitSet_Returned_Success()
154             throws IOException, IllegalAccessException {
155         mockHttpClientForGetRequest();
156         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
157         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
158         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
159         synchronizerTask.replicateDataRouterLogs(rleBitSet);
160     }
161
162     @Test
163     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Content_Type_Failure()
164             throws IOException, IllegalAccessException {
165         mockHttpClientForGetRequest();
166         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
167         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
168         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
169         synchronizerTask.replicateDataRouterLogs(rleBitSet);
170     }
171
172     @Test
173     public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Resonse_Code_Failure()
174             throws IOException, IllegalAccessException {
175         mockHttpClientForGetRequest();
176         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
177         RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
178         synchronizerTask.replicateDataRouterLogs(rleBitSet);
179     }
180
181     @Test
182     public void Given_Synch_Task_Is_Started_And_LogFileLoader_Is_Idle_Then_Standby_Pod_Synch_Is_Successful()
183             throws IOException, IllegalAccessException {
184         mockHttpClientForGetRequest();
185         Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
186         Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0"));
187         mockResponseFromGet();
188         synchronizerTask.run();
189     }
190
191
192     private void mockHttpClientForGetRequest() throws IllegalAccessException, IOException {
193         FieldUtils.writeField(synchronizerTask, "httpclient", httpClient, true);
194         Mockito.when(httpClient.execute(anyObject())).thenReturn(response);
195         Mockito.when(response.getEntity()).thenReturn(httpEntity);
196         Mockito.when(response.getStatusLine()).thenReturn(statusLine);
197
198     }
199
200     private void mockResponseFromGet() throws IOException {
201         InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
202         Mockito.when(httpEntity.getContent()).thenReturn(in);
203     }
204 }