40fadcc749c54eed678c1ab8701c143192c8e411
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-plugins / src / test / java / org / openecomp / dcae / apod / analytics / cdap / plugins / streaming / dmaap / DMaaPMRReceiverTest.java
1 /*\r
2  * ===============================LICENSE_START======================================\r
3  *  dcae-analytics\r
4  * ================================================================================\r
5  *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  *  Licensed under the Apache License, Version 2.0 (the "License");\r
8  *  you may not use this file except in compliance with the License.\r
9  *   You may obtain a copy of the License at\r
10  *\r
11  *          http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  *  Unless required by applicable law or agreed to in writing, software\r
14  *  distributed under the License is distributed on an "AS IS" BASIS,\r
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  *  See the License for the specific language governing permissions and\r
17  *  limitations under the License.\r
18  *  ============================LICENSE_END===========================================\r
19  */\r
20 \r
21 package org.openecomp.dcae.apod.analytics.cdap.plugins.streaming.dmaap;\r
22 \r
23 import com.google.common.collect.ImmutableList;\r
24 import org.apache.spark.storage.StorageLevel;\r
25 import org.junit.Test;\r
26 import org.mockito.Mockito;\r
27 import org.openecomp.dcae.apod.analytics.cdap.plugins.BaseAnalyticsCDAPPluginsUnitTest;\r
28 import org.openecomp.dcae.apod.analytics.cdap.plugins.domain.config.dmaap.TestDMaaPMRSourcePluginConfig;\r
29 import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;\r
30 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;\r
31 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;\r
32 \r
33 import static org.mockito.Mockito.times;\r
34 import static org.mockito.Mockito.verify;\r
35 import static org.mockito.Mockito.when;\r
36 \r
37 /**\r
38  * @author Rajiv Singla . Creation Date: 1/24/2017.\r
39  */\r
40 public class DMaaPMRReceiverTest extends BaseAnalyticsCDAPPluginsUnitTest {\r
41 \r
42 \r
43     @Test\r
44     public void testStoreStructuredRecords() throws Exception {\r
45 \r
46         final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();\r
47         final TestDMaaPMRReceiver dMaaPMRReceiver =\r
48                 new TestDMaaPMRReceiver(StorageLevel.MEMORY_ONLY(), testDMaaPMRSourcePluginConfig);\r
49 \r
50         final DMaaPMRSubscriber dMaaPMRSubscriber = Mockito.mock(DMaaPMRSubscriber.class);\r
51         final DMaaPMRSubscriberResponse subscriberResponse = Mockito.mock(DMaaPMRSubscriberResponse.class);\r
52         when(dMaaPMRSubscriber.fetchMessages()).thenReturn(subscriberResponse);\r
53         when(subscriberResponse.getFetchedMessages()).thenReturn(ImmutableList.of("Test Message"));\r
54         when(subscriberResponse.getResponseCode()).thenReturn(200);\r
55         when(subscriberResponse.getResponseMessage()).thenReturn("OK");\r
56         dMaaPMRReceiver.storeStructuredRecords(dMaaPMRSubscriber);\r
57         verify(dMaaPMRSubscriber, times(1)).fetchMessages();\r
58         verify(subscriberResponse, times(1)).getFetchedMessages();\r
59     }\r
60 \r
61     @Test\r
62     public void testStoreStructuredRecordsWhenSubscriberThrowsException() throws Exception {\r
63 \r
64         final TestDMaaPMRSourcePluginConfig testDMaaPMRSourcePluginConfig = getTestDMaaPMRSourcePluginConfig();\r
65         final TestDMaaPMRReceiver dMaaPMRReceiver =\r
66                 new TestDMaaPMRReceiver(StorageLevel.MEMORY_ONLY(), testDMaaPMRSourcePluginConfig);\r
67 \r
68         final DMaaPMRSubscriber dMaaPMRSubscriber = Mockito.mock(DMaaPMRSubscriber.class);\r
69         final DMaaPMRSubscriberResponse subscriberResponse = Mockito.mock(DMaaPMRSubscriberResponse.class);\r
70         when(dMaaPMRSubscriber.fetchMessages()).thenThrow(DCAEAnalyticsRuntimeException.class);\r
71         dMaaPMRReceiver.storeStructuredRecords(dMaaPMRSubscriber);\r
72         verify(dMaaPMRSubscriber, times(1)).fetchMessages();\r
73         verify(subscriberResponse, times(0)).getFetchedMessages();\r
74     }\r
75 }\r