X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=dcae-analytics-dmaap%2Fsrc%2Ftest%2Fjava%2Forg%2Fopenecomp%2Fdcae%2Fapod%2Fanalytics%2Fdmaap%2Fservice%2Fpublisher%2FDMaaPMRPublisherImplTest.java;h=d38d59ccb5c4a7516b49a31a3b3176b9ca8a69d2;hb=06044df56fb07f4b368888581752855595e7b147;hp=557742f7502613047efbf8570ce3e7fcb83e9e06;hpb=475cb8c867038acd73ff540173d54bac3947c610;p=dcaegen2%2Fanalytics%2Ftca.git diff --git a/dcae-analytics-dmaap/src/test/java/org/openecomp/dcae/apod/analytics/dmaap/service/publisher/DMaaPMRPublisherImplTest.java b/dcae-analytics-dmaap/src/test/java/org/openecomp/dcae/apod/analytics/dmaap/service/publisher/DMaaPMRPublisherImplTest.java index 557742f..d38d59c 100644 --- a/dcae-analytics-dmaap/src/test/java/org/openecomp/dcae/apod/analytics/dmaap/service/publisher/DMaaPMRPublisherImplTest.java +++ b/dcae-analytics-dmaap/src/test/java/org/openecomp/dcae/apod/analytics/dmaap/service/publisher/DMaaPMRPublisherImplTest.java @@ -1,212 +1,210 @@ -/* - * ===============================LICENSE_START====================================== - * dcae-analytics - * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============================LICENSE_END=========================================== - */ - -package org.openecomp.dcae.apod.analytics.dmaap.service.publisher; - -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.http.client.ResponseHandler; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.impl.client.CloseableHttpClient; -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; -import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; -import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest; -import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; -import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse; - -import java.io.IOException; -import java.util.ArrayList; - -import static org.hamcrest.CoreMatchers.isA; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -/** - * @author Rajiv Singla . Creation Date: 10/21/2016. - */ -@RunWith(MockitoJUnitRunner.class) -public class DMaaPMRPublisherImplTest extends BaseAnalyticsDMaaPUnitTest { - - @Mock - DMaaPMRPublisherQueueFactory dmaapMRPublisherQueueFactory; - @Mock - CloseableHttpClient closeableHttpClient; - @Mock - DMaaPMRPublisherQueue dmaapMRPublisherQueue; - - @Before - public void setUp() throws Exception { - given(dmaapMRPublisherQueueFactory.create(Mockito.anyInt(), Mockito.anyInt())) - .willReturn(dmaapMRPublisherQueue); - } - - @After - public void tearDown() throws Exception { - } - - @Test - public void testPublishSmallMessageList() throws Exception { - given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(10); - given(dmaapMRPublisherQueue.addBatchMessages(Mockito.anyList())).willReturn(2); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - - DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages()); - - assertThat(dmaapMRPublisherResponse.getResponseCode(), is(202)); - assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(2)); - assertThat(dmaapMRPublisherResponse.getResponseMessage(), - is("Accepted - Messages queued for batch publishing to MR Topic")); - } - - @Test - public void testPublishBigMessageList() throws Exception { - - given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(0); - given(dmaapMRPublisherQueue.getMessageForPublishing()).willReturn(getTwoSampleMessages()); - Mockito.when( - closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - - DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages()); - - assertThat(dmaapMRPublisherResponse.getResponseCode(), is(200)); - assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(200)); - assertThat(dmaapMRPublisherResponse.getResponseMessage(), is("Message successfully posted")); - } - - @Test - public void testForcePublishSuccessful() throws Exception { - DMaaPMRPublisherConfig dmaapMRPublisherConfig = new - DMaaPMRPublisherConfig.Builder(HOST_NAME, TOPIC_NAME) - .setPortNumber(PORT_NUMBER) - .setProtocol(HTTP_PROTOCOL) - .setContentType(CONTENT_TYPE) - .setMaxRecoveryQueueSize(PUBLISHER_MAX_RECOVERY_QUEUE_SIZE) - .setMaxBatchSize(PUBLISHER_MAX_BATCH_QUEUE_SIZE).build(); - - HttpPost httpPost = Mockito.mock(HttpPost.class); - Mockito.when(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - dmaapMRPublisherConfig, dmaapMRPublisherQueueFactory, closeableHttpClient); - DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); - assertThat(response.getResponseCode(), is(200)); - } - - @Test - public void testForcePublishFailure() throws Exception { - Mockito.when(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(503, "Message successfully posted")); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); - assertThat(response.getResponseCode(), is(503)); - } - - @Rule - public ExpectedException httpIOException = ExpectedException.none(); - - @Test - public void testForcePublishHttpFailure() throws Exception { - - httpIOException.expect(DCAEAnalyticsRuntimeException.class); - httpIOException.expectCause(isA(IOException.class)); - - given(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); - } - - @Test - public void testFlushSuccessful() throws Exception { - Mockito.when(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); - - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush(); - assertThat(response.getResponseCode(), is(200)); - } - - @Test - public void testFlushEmptyList() throws Exception { - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush(); - assertThat(response.getResponseCode(), is(204)); - } - - @Test - public void testClose() throws Exception { - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); - Mockito.when(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - dmaapMRPublisherImpl.close(); - verify(closeableHttpClient).execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)); - } - - @Test - public void testCloseUnsuccessful() throws Exception { - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); - Mockito.when(closeableHttpClient.execute( - Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) - .thenReturn(new ImmutablePair<>(400, "Message successfully posted")); - Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); - - DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( - getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); - dmaapMRPublisherImpl.close(); - verify(closeableHttpClient, times(6)).execute(Mockito.any(HttpUriRequest.class), - Mockito.any(ResponseHandler.class)); - } -} +/* + * ===============================LICENSE_START====================================== + * dcae-analytics + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================LICENSE_END=========================================== + */ + +package org.openecomp.dcae.apod.analytics.dmaap.service.publisher; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.impl.client.CloseableHttpClient; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException; +import org.openecomp.dcae.apod.analytics.dmaap.BaseAnalyticsDMaaPUnitTest; +import org.openecomp.dcae.apod.analytics.dmaap.domain.config.DMaaPMRPublisherConfig; +import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse; + +import java.io.IOException; +import java.util.ArrayList; + +import static org.hamcrest.CoreMatchers.isA; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author Rajiv Singla . Creation Date: 10/21/2016. + */ +@RunWith(MockitoJUnitRunner.class) +public class DMaaPMRPublisherImplTest extends BaseAnalyticsDMaaPUnitTest { + + @Mock + private DMaaPMRPublisherQueueFactory dmaapMRPublisherQueueFactory; + @Mock + private CloseableHttpClient closeableHttpClient; + @Mock + private DMaaPMRPublisherQueue dmaapMRPublisherQueue; + + @Before + public void setUp() throws Exception { + given(dmaapMRPublisherQueueFactory.create(Mockito.anyInt(), Mockito.anyInt())) + .willReturn(dmaapMRPublisherQueue); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPublishSmallMessageList() throws Exception { + given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(10); + given(dmaapMRPublisherQueue.addBatchMessages(Mockito.anyList())).willReturn(2); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + + DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages()); + + assertThat(dmaapMRPublisherResponse.getResponseCode(), is(202)); + assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(2)); + assertThat(dmaapMRPublisherResponse.getResponseMessage(), + is("Accepted - Messages queued for batch publishing to MR Topic")); + } + + @Test + public void testPublishBigMessageList() throws Exception { + + given(dmaapMRPublisherQueue.getBatchQueueRemainingSize()).willReturn(0); + given(dmaapMRPublisherQueue.getMessageForPublishing()).willReturn(getTwoSampleMessages()); + Mockito.when( + closeableHttpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + + DMaaPMRPublisherResponse dmaapMRPublisherResponse = dmaapMRPublisherImpl.publish(getTwoSampleMessages()); + + assertThat(dmaapMRPublisherResponse.getResponseCode(), is(200)); + assertThat(dmaapMRPublisherResponse.getPendingMessagesCount(), is(200)); + assertThat(dmaapMRPublisherResponse.getResponseMessage(), is("Message successfully posted")); + } + + @Test + public void testForcePublishSuccessful() throws Exception { + DMaaPMRPublisherConfig dmaapMRPublisherConfig = new + DMaaPMRPublisherConfig.Builder(HOST_NAME, TOPIC_NAME) + .setPortNumber(PORT_NUMBER) + .setProtocol(HTTP_PROTOCOL) + .setContentType(CONTENT_TYPE) + .setMaxRecoveryQueueSize(PUBLISHER_MAX_RECOVERY_QUEUE_SIZE) + .setMaxBatchSize(PUBLISHER_MAX_BATCH_QUEUE_SIZE).build(); + + Mockito.when(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + dmaapMRPublisherConfig, dmaapMRPublisherQueueFactory, closeableHttpClient); + DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); + assertThat(response.getResponseCode(), is(200)); + } + + @Test + public void testForcePublishFailure() throws Exception { + Mockito.when(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(503, "Message successfully posted")); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); + assertThat(response.getResponseCode(), is(503)); + } + + @Rule + public ExpectedException httpIOException = ExpectedException.none(); + + @Test + public void testForcePublishHttpFailure() throws Exception { + + httpIOException.expect(DCAEAnalyticsRuntimeException.class); + httpIOException.expectCause(isA(IOException.class)); + + given(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).willThrow(IOException.class); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + dmaapMRPublisherImpl.forcePublish(getTwoSampleMessages()); + } + + @Test + public void testFlushSuccessful() throws Exception { + Mockito.when(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); + + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush(); + assertThat(response.getResponseCode(), is(200)); + } + + @Test + public void testFlushEmptyList() throws Exception { + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + DMaaPMRPublisherResponse response = dmaapMRPublisherImpl.flush(); + assertThat(response.getResponseCode(), is(204)); + } + + @Test + public void testClose() throws Exception { + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); + Mockito.when(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(200, "Message successfully posted")); + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + dmaapMRPublisherImpl.close(); + verify(closeableHttpClient).execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class)); + } + + @Test + public void testCloseUnsuccessful() throws Exception { + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(new ArrayList()); + Mockito.when(closeableHttpClient.execute( + Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))) + .thenReturn(new ImmutablePair<>(400, "Message successfully posted")); + Mockito.when(dmaapMRPublisherQueue.getMessageForPublishing()).thenReturn(getTwoSampleMessages()); + + DMaaPMRPublisherImpl dmaapMRPublisherImpl = new DMaaPMRPublisherImpl( + getPublisherConfig(), dmaapMRPublisherQueueFactory, closeableHttpClient); + dmaapMRPublisherImpl.close(); + verify(closeableHttpClient, times(6)).execute(Mockito.any(HttpUriRequest.class), + Mockito.any(ResponseHandler.class)); + } +}