Added oparent to sdc main
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / item-rest / item-rest-services / src / test / java / org / openecomp / sdcrests / item / rest / services / catalog / notification / http / HttpNotificationTaskTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
18
19 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20 import static com.github.tomakehurst.wiremock.client.WireMock.post;
21 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
22 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
23 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
24 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
25 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
26 import static org.junit.Assert.assertEquals;
27 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE;
28 import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY;
29
30 import com.github.tomakehurst.wiremock.junit.WireMockRule;
31 import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern;
32 import com.github.tomakehurst.wiremock.matching.EqualToPattern;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.UUID;
36 import org.junit.BeforeClass;
37 import org.junit.ClassRule;
38 import org.junit.Test;
39
40 /**
41  * @author evitaliy
42  * @since 22 Nov 2018
43  */
44 public class HttpNotificationTaskTest {
45
46     @ClassRule
47     public static final WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
48
49     private static final String NOTIFICATION_PATH = "/notification";
50     private static final String USER_ID = "d75360e1-f393-480f-b39e-fbbdf38a22c1";
51     private static final String RETRY_SCENARIO = "RETRY_SCENARIO";
52     private static final String MALFORMED_RESPONSE_SCENARIO = "MALFORMED_RESPONSE_SCENARIO";
53
54     private static String endpoint;
55
56     @BeforeClass
57     public static void initConfiguration() {
58         endpoint = "http://localhost:" + wireMockRule.port() + NOTIFICATION_PATH;
59     }
60
61     @Test
62     public void doneWhenResponseOk() {
63         assertDone(200, arrayToJson(UUID.randomUUID().toString()));
64     }
65
66     private void assertDone(int responseStatus, String body) {
67         final String itemId = UUID.randomUUID().toString();
68         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(responseStatus).withBody(body)));
69         HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Collections.singleton(itemId));
70         assertEquals(DONE, task.call());
71         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH))
72                        .withRequestBody(new EqualToJsonPattern(arrayToJson(itemId), true, false)));
73     }
74
75     private String arrayToJson(String... ids) {
76         return ids.length == 0 ? "[]" : "[ \"" + String.join("\", \"", ids) + "\" ]";
77     }
78
79     @Test
80     public void doneWhenResponse400() {
81         assertDone(400, arrayToJson(UUID.randomUUID().toString()));
82     }
83
84     @Test
85     public void doneWhenResponse522() {
86         assertDone(522, arrayToJson(UUID.randomUUID().toString()));
87     }
88
89     @Test
90     public void doneWhenResponse500ButFailedIdsNotReturned() {
91         assertDone(500, "{}");
92     }
93
94     @Test
95     public void doneWhenResponse500ButFailedIdsEmpty() {
96         assertDone(500, toFailedIdsResponse());
97     }
98
99     private String toFailedIdsResponse(String... ids) {
100         return "{ \"failedIds\": " + arrayToJson(ids) + " }";
101     }
102
103     @Test
104     public void retryWithSameItemIdsWhenResponse500AndFailedToParseResponse() {
105
106         final String[] expectedItemIds = {UUID.randomUUID().toString(), UUID.randomUUID().toString()};
107
108         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(500).withBody("d[g.0g,y/"))
109                         .inScenario(MALFORMED_RESPONSE_SCENARIO));
110         HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Arrays.asList(expectedItemIds));
111         assertEquals(RETRY, task.call());
112
113         EqualToJsonPattern expectedRequestBody = new EqualToJsonPattern(arrayToJson(expectedItemIds), true, false);
114         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody));
115
116         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200).withBody("{}"))
117                         .inScenario(MALFORMED_RESPONSE_SCENARIO));
118         assertEquals(DONE, task.call());
119
120         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody));
121     }
122
123     @Test
124     public void retryWithFailedItemsWhenResponse500() {
125
126         final String failedId = UUID.randomUUID().toString();
127         final String successId = UUID.randomUUID().toString();
128
129         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(500).withBody(toFailedIdsResponse(failedId)))
130                         .inScenario(RETRY_SCENARIO));
131         HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Arrays.asList(failedId, successId));
132         assertEquals(RETRY, task.call());
133         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH))
134                        .withRequestBody(new EqualToJsonPattern(arrayToJson(failedId, successId), true, false)));
135
136         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200).withBody("{}"))
137                         .inScenario(RETRY_SCENARIO));
138         assertEquals(DONE, task.call());
139         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH))
140                        .withRequestBody(new EqualToJsonPattern(arrayToJson(failedId), true, false)));
141     }
142
143     @Test
144     public void userIdSentToServer() {
145         stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200)));
146         HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Collections.emptyList());
147         assertEquals(DONE, task.call());
148         verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withHeader("USER_ID", new EqualToPattern(USER_ID)));
149     }
150 }