1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning;
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;
32 import com.att.eelf.configuration.EELFManager;
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.net.InetAddress;
38 import java.net.UnknownHostException;
39 import java.util.concurrent.ExecutorService;
40 import java.util.concurrent.Executors;
41 import java.util.concurrent.TimeUnit;
42 import javax.persistence.EntityManager;
43 import javax.persistence.EntityManagerFactory;
44 import javax.persistence.Persistence;
45 import org.apache.commons.io.IOUtils;
46 import org.apache.commons.lang3.reflect.FieldUtils;
47 import org.apache.http.HttpEntity;
48 import org.apache.http.StatusLine;
49 import org.apache.http.client.methods.CloseableHttpResponse;
50 import org.apache.http.conn.ssl.SSLSocketFactory;
51 import org.apache.http.impl.client.AbstractHttpClient;
52 import org.apache.http.message.BasicHeader;
53 import org.junit.After;
54 import org.junit.AfterClass;
55 import org.junit.Assert;
56 import org.junit.Before;
57 import org.junit.BeforeClass;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.Mock;
61 import org.mockito.Mockito;
62 import org.onap.dmaap.datarouter.provisioning.utils.LogfileLoader;
63 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
64 import org.onap.dmaap.datarouter.provisioning.utils.URLUtilities;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PowerMockIgnore;
67 import org.powermock.core.classloader.annotations.PrepareForTest;
68 import org.powermock.modules.junit4.PowerMockRunner;
70 @RunWith(PowerMockRunner.class)
71 @PowerMockIgnore("javax.net.ssl.*")
72 @PrepareForTest({BaseServlet.class, URLUtilities.class})
73 public class SynchronizerTaskTest {
76 private AbstractHttpClient httpClient;
79 private HttpEntity httpEntity;
82 private StatusLine statusLine;
85 private CloseableHttpResponse response;
88 private ByteArrayOutputStream byteArrayOutputStream;
90 private SynchronizerTask synchronizerTask;
92 private ExecutorService executorService;
94 private static EntityManagerFactory emf;
95 private static EntityManager em;
98 public static void init() {
99 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
100 em = emf.createEntityManager();
102 "org.onap.dmaap.datarouter.provserver.properties",
103 "src/test/resources/h2Database.properties");
107 public static void tearDownClass() {
115 public void setUp() throws IllegalAccessException, UnknownHostException {
116 SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
117 doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
119 PowerMockito.mockStatic(BaseServlet.class);
120 PowerMockito.mockStatic(URLUtilities.class);
121 when(BaseServlet.getPods()).thenReturn(new String[] {InetAddress.getLocalHost().getHostName(), "stand-by-prov"});
122 when(URLUtilities.generatePeerProvURL()).thenReturn("https://stand-by-prov/internal/prov");
123 when(URLUtilities.generatePeerLogsURL()).thenReturn("https://stand-by-prov/internal/drlogs");
125 synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
126 doReturn(2).when(synchronizerTask).lookupState();
128 executorService = Executors.newSingleThreadExecutor();
129 executorService.execute(synchronizerTask);
133 public void tearDown() throws InterruptedException {
134 executorService.shutdown();
135 executorService.awaitTermination(2, TimeUnit.SECONDS);
139 public void Given_Synch_Task_readRemoteLoglist_Called_And_Valid_BitSet_Returned_Success() throws Exception {
140 mockHttpClientForGetRequest();
141 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
142 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
143 Mockito.when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("1-55251".getBytes()));
144 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
145 Assert.assertNotNull(rleBitSet);
149 public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Resonse_Code_Failure() throws Exception {
150 mockHttpClientForGetRequest();
151 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
152 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
153 Assert.assertNotNull(rleBitSet);
157 public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Content_Type_Failure() throws Exception {
158 mockHttpClientForGetRequest();
159 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
160 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
161 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
162 Assert.assertNotNull(rleBitSet);
166 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Valid_BitSet_Returned_Success() throws Exception {
167 mockHttpClientForGetRequest();
168 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
169 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
170 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
171 synchronizerTask.replicateDataRouterLogs(rleBitSet);
175 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Content_Type_Failure() throws Exception {
176 mockHttpClientForGetRequest();
177 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
178 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
179 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
180 synchronizerTask.replicateDataRouterLogs(rleBitSet);
184 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Resonse_Code_Failure() throws Exception {
185 mockHttpClientForGetRequest();
186 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
187 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
188 synchronizerTask.replicateDataRouterLogs(rleBitSet);
192 public void Given_Synch_Task_Is_Started_And_LogFileLoader_Is_Idle_Then_Standby_Pod_Synch_Is_Successful() throws Exception {
193 mockHttpClientForGetRequest();
194 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
195 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0"));
196 mockResponseFromGet();
200 private void mockHttpClientForGetRequest() throws Exception {
201 FieldUtils.writeField(synchronizerTask, "httpclient", httpClient, true);
202 Mockito.when(httpClient.execute(anyObject())).thenReturn(response);
203 Mockito.when(response.getEntity()).thenReturn(httpEntity);
204 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
208 private void mockResponseFromGet() throws IOException {
209 InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
210 Mockito.when(httpEntity.getContent()).thenReturn(in);