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.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;
64 @RunWith(PowerMockRunner.class)
65 @PowerMockIgnore("javax.net.ssl.*")
66 @PrepareForTest({BaseServlet.class, URLUtilities.class})
67 public class SynchronizerTaskTest {
70 private AbstractHttpClient httpClient;
73 private HttpEntity httpEntity;
76 private StatusLine statusLine;
79 private CloseableHttpResponse response;
81 private SynchronizerTask synchronizerTask;
83 private static EntityManagerFactory emf;
84 private static EntityManager em;
87 public static void init() {
88 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
89 em = emf.createEntityManager();
91 "org.onap.dmaap.datarouter.provserver.properties",
92 "src/test/resources/h2Database.properties");
96 public static void tearDownClass() {
104 public void setUp() throws IllegalAccessException, UnknownHostException {
105 SSLSocketFactory sslSocketFactory = mock(SSLSocketFactory.class);
106 doNothing().when(sslSocketFactory).setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
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");
114 synchronizerTask = Mockito.spy(SynchronizerTask.getSynchronizer());
115 doReturn(2).when(synchronizerTask).lookupState();
119 public void tearDown() {
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);
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);
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);
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);
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);
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);
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();
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);
200 private void mockResponseFromGet() throws IOException {
201 InputStream in = getClass().getClassLoader().getResourceAsStream("prov_data.json");
202 Mockito.when(httpEntity.getContent()).thenReturn(in);