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 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;
66 @RunWith(PowerMockRunner.class)
67 @PowerMockIgnore("javax.net.ssl.*")
68 @PrepareForTest({BaseServlet.class, URLUtilities.class})
69 public class SynchronizerTaskTest {
72 private AbstractHttpClient httpClient;
75 private HttpEntity httpEntity;
78 private StatusLine statusLine;
81 private CloseableHttpResponse response;
83 private SynchronizerTask synchronizerTask;
84 private ExecutorService executorService;
86 private static EntityManagerFactory emf;
87 private static EntityManager em;
90 public static void init() {
91 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
92 em = emf.createEntityManager();
94 "org.onap.dmaap.datarouter.provserver.properties",
95 "src/test/resources/h2Database.properties");
99 public static void tearDownClass() {
107 public void setUp() throws IllegalAccessException, UnknownHostException {
108 SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
109 doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
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");
117 synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
118 doReturn(2).when(synchronizerTask).lookupState();
120 executorService = Executors.newCachedThreadPool();
121 executorService.execute(synchronizerTask);
125 public void tearDown() throws InterruptedException {
126 executorService.shutdown();
127 executorService.awaitTermination(2, TimeUnit.SECONDS);
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);
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);
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);
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);
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);
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);
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();
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);
207 private void mockResponseFromGet() throws IOException {
208 InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
209 Mockito.when(httpEntity.getContent()).thenReturn(in);