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 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.URLUtilities;
58 import org.powermock.api.mockito.PowerMockito;
59 import org.powermock.core.classloader.annotations.PowerMockIgnore;
60 import org.powermock.core.classloader.annotations.PrepareForTest;
61 import org.powermock.modules.junit4.PowerMockRunner;
63 @RunWith(PowerMockRunner.class)
64 @PowerMockIgnore("javax.net.ssl.*")
65 @PrepareForTest({BaseServlet.class, URLUtilities.class})
66 public class SynchronizerTaskTest {
69 private AbstractHttpClient httpClient;
72 private HttpEntity httpEntity;
75 private StatusLine statusLine;
78 private CloseableHttpResponse response;
80 private SynchronizerTask synchronizerTask;
82 private static EntityManagerFactory emf;
83 private static EntityManager em;
86 public static void init() {
87 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
88 em = emf.createEntityManager();
90 "org.onap.dmaap.datarouter.provserver.properties",
91 "src/test/resources/h2Database.properties");
95 public static void tearDownClass() {
103 public void setUp() throws IllegalAccessException, UnknownHostException {
104 SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
105 doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
107 PowerMockito.mockStatic(BaseServlet.class);
108 PowerMockito.mockStatic(URLUtilities.class);
109 when(BaseServlet.getPods()).thenReturn(new String[] {InetAddress.getLocalHost().getHostName(), "stand-by-prov"});
110 when(URLUtilities.generatePeerProvURL()).thenReturn("https://stand-by-prov/internal/prov");
111 when(URLUtilities.generatePeerLogsURL()).thenReturn("https://stand-by-prov/internal/drlogs");
113 synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
114 doReturn(2).when(synchronizerTask).lookupState();
118 public void tearDown() {
122 public void Given_Synch_Task_readRemoteLoglist_Called_And_Valid_BitSet_Returned_Success()
123 throws IOException, IllegalAccessException {
124 mockHttpClientForGetRequest();
125 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
126 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
127 Mockito.when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream("1-55251".getBytes()));
128 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
129 Assert.assertNotNull(rleBitSet);
133 public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Resonse_Code_Failure()
134 throws IOException, IllegalAccessException {
135 mockHttpClientForGetRequest();
136 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
137 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
138 Assert.assertNotNull(rleBitSet);
142 public void Given_Synch_Task_readRemoteLoglist_Called_And_Invalid_Content_Type_Failure()
143 throws IOException, IllegalAccessException {
144 mockHttpClientForGetRequest();
145 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
146 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
147 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
148 Assert.assertNotNull(rleBitSet);
152 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Valid_BitSet_Returned_Success()
153 throws IOException, IllegalAccessException {
154 mockHttpClientForGetRequest();
155 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
156 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "text/plain"));
157 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
158 synchronizerTask.replicateDataRouterLogs(rleBitSet);
162 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Content_Type_Failure()
163 throws IOException, IllegalAccessException {
164 mockHttpClientForGetRequest();
165 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
166 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "invalid_content_type"));
167 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
168 synchronizerTask.replicateDataRouterLogs(rleBitSet);
172 public void Given_Synch_Task_replicateDataRouterLogs_Called_And_Invalid_Resonse_Code_Failure()
173 throws IOException, IllegalAccessException {
174 mockHttpClientForGetRequest();
175 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(404);
176 RLEBitSet rleBitSet = synchronizerTask.readRemoteLoglist();
177 synchronizerTask.replicateDataRouterLogs(rleBitSet);
181 public void Given_Synch_Task_Is_Started_And_LogFileLoader_Is_Idle_Then_Standby_Pod_Synch_Is_Successful()
182 throws IOException, IllegalAccessException {
183 mockHttpClientForGetRequest();
184 Mockito.when(response.getStatusLine().getStatusCode()).thenReturn(200);
185 Mockito.when(httpEntity.getContentType()).thenReturn(new BasicHeader("header", "application/vnd.dmaap-dr.provfeed-full; version=1.0"));
186 mockResponseFromGet();
187 synchronizerTask.run();
191 private void mockHttpClientForGetRequest() throws IllegalAccessException, IOException {
192 FieldUtils.writeField(synchronizerTask, "httpclient", httpClient, true);
193 Mockito.when(httpClient.execute(anyObject())).thenReturn(response);
194 Mockito.when(response.getEntity()).thenReturn(httpEntity);
195 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
199 private void mockResponseFromGet() throws IOException {
200 InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
201 Mockito.when(httpEntity.getContent()).thenReturn(in);