Initial TCA commit into DCAEGEN2
[dcaegen2/analytics/tca.git] / dcae-analytics-it / src / test / java / org / openecomp / dcae / apod / analytics / it / cucumber / steps / DMaaPMRSteps.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
10  *
11  *          http://www.apache.org/licenses/LICENSE-2.0
12  *
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===========================================
19  */
20
21 package org.openecomp.dcae.apod.analytics.it.cucumber.steps;
22
23 import com.google.inject.Inject;
24 import com.google.inject.name.Named;
25 import cucumber.api.java.en.And;
26 import cucumber.api.java.en.Given;
27 import cucumber.api.java.en.Then;
28 import cucumber.api.java.en.When;
29 import org.openecomp.dcae.apod.analytics.common.utils.HTTPUtils;
30 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRPublisherResponse;
31 import org.openecomp.dcae.apod.analytics.dmaap.domain.response.DMaaPMRSubscriberResponse;
32 import org.openecomp.dcae.apod.analytics.dmaap.service.publisher.DMaaPMRPublisher;
33 import org.openecomp.dcae.apod.analytics.dmaap.service.subscriber.DMaaPMRSubscriber;
34 import org.openecomp.dcae.apod.analytics.it.dmaap.DMaaPMRCreator;
35 import org.openecomp.dcae.apod.analytics.it.util.StepUtils;
36 import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsIT;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import java.util.Arrays;
41 import java.util.concurrent.TimeUnit;
42
43 import static org.junit.Assert.assertNotNull;
44 import static org.junit.Assert.assertTrue;
45
46 /**
47  * @author Rajiv Singla . Creation Date: 2/1/2017.
48  */
49 public class DMaaPMRSteps extends BaseDCAEAnalyticsIT {
50
51     private static final Logger LOG = LoggerFactory.getLogger(DMaaPMRSteps.class);
52
53     private final DMaaPMRCreator dMaaPMRCreator;
54     private final String defaultPublisherTopicName;
55
56     private static DMaaPMRSubscriberResponse subscriberResponse;
57     private static String messageToPublish;
58     private static String fetchedMessage;
59
60     @Inject
61     public DMaaPMRSteps(DMaaPMRCreator dMaaPMRCreator,
62                         @Named("dmaap.mr.publisher.topicName") String defaultPublisherTopicName) {
63         this.dMaaPMRCreator = dMaaPMRCreator;
64         this.defaultPublisherTopicName = defaultPublisherTopicName;
65     }
66
67
68     @Given("^DMaaP MR Service is up$")
69     public void dmaapMRServiceIsUp() throws Throwable {
70         final DMaaPMRSubscriber subscriber =
71                 dMaaPMRCreator.getDMaaPMRSubscriberWithTopicName(defaultPublisherTopicName);
72         final DMaaPMRSubscriberResponse subscriberResponse = subscriber.fetchMessages();
73         assertNotNull(subscriberResponse.getResponseCode());
74         assertTrue(HTTPUtils.isSuccessfulResponseCode(subscriberResponse.getResponseCode()));
75         LOG.info("Subscriber is able to fetch messages successfully - Verified DMaaP MR Service is UP");
76     }
77
78     @When("^I publish json message to publisher topic name \"([^\"]*)\" in file \"([^\"]*)\"$")
79     public void iPublishJsonMessageToPublisherTopicNameInFile(String publisherTopicName, String fileLocation)
80             throws Throwable {
81         String publisherTopic;
82         if (StepUtils.isDefaultPublisherTopic(publisherTopicName)) {
83             publisherTopic = defaultPublisherTopicName;
84         } else {
85             publisherTopic = publisherTopicName;
86         }
87         final DMaaPMRPublisher publisher = dMaaPMRCreator.getDMaaPMRPublisherWithTopicName(publisherTopic);
88         messageToPublish = fromStream(fileLocation);
89         final DMaaPMRPublisherResponse publisherResponse = publisher.publish(Arrays.asList(messageToPublish));
90         LOG.info("Publisher published messages to DMaaP MR Topic - Response: {}", publisherResponse);
91         assertTrue(HTTPUtils.isSuccessfulResponseCode(publisherResponse.getResponseCode()));
92     }
93
94     @And("^wait for \"([^\"]*)\" seconds$")
95     public void waitForSeconds(Integer waitInSeconds) throws Throwable {
96         TimeUnit.SECONDS.sleep(waitInSeconds);
97         LOG.info("Waking up after sleep: {} seconds", waitInSeconds);
98     }
99
100     @And("^subscriber fetch message from publisher topic name \"([^\"]*)\"$")
101     public void fetchMessageFrom(String publisherTopicName) throws Throwable {
102         String publisherTopic;
103         if (StepUtils.isDefaultPublisherTopic(publisherTopicName)) {
104             publisherTopic = defaultPublisherTopicName;
105         } else {
106             publisherTopic = publisherTopicName;
107         }
108         final DMaaPMRSubscriber subscriber = dMaaPMRCreator.getDMaaPMRSubscriberWithTopicName(publisherTopic);
109         subscriberResponse = subscriber.fetchMessages();
110         LOG.info("Subscriber fetched messages to DMaaP MR Topic - Response: {}", subscriberResponse);
111         assertTrue(HTTPUtils.isSuccessfulResponseCode(subscriberResponse.getResponseCode()));
112     }
113
114     @And("^compare fetched json message with published message$")
115     public void compareFetchedJsonMessageWithPublishedMessage() throws Throwable {
116
117         fetchedMessage = subscriberResponse.getFetchedMessages().get(0);
118         LOG.info("Fetched Json Message: {}", fetchedMessage);
119         LOG.info("Published Json Message: {}", messageToPublish);
120     }
121
122     @Then("^fetched message must be same as published message$")
123     public void fetchedMessageMustBeSameAsPublishedMessage() throws Throwable {
124         assertJson(messageToPublish, fetchedMessage);
125     }
126
127
128
129 }