2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdcrests.item.rest.services.catalog.notification.http;
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;
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;
44 public class HttpNotificationTaskTest {
47 public static final WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
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";
54 private static String endpoint;
57 public static void initConfiguration() {
58 endpoint = "http://localhost:" + wireMockRule.port() + NOTIFICATION_PATH;
62 public void doneWhenResponseOk() {
63 assertDone(200, arrayToJson(UUID.randomUUID().toString()));
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)));
75 private String arrayToJson(String... ids) {
76 return ids.length == 0 ? "[]" : "[ \"" + String.join("\", \"", ids) + "\" ]";
80 public void doneWhenResponse400() {
81 assertDone(400, arrayToJson(UUID.randomUUID().toString()));
85 public void doneWhenResponse522() {
86 assertDone(522, arrayToJson(UUID.randomUUID().toString()));
90 public void doneWhenResponse500ButFailedIdsNotReturned() {
91 assertDone(500, "{}");
95 public void doneWhenResponse500ButFailedIdsEmpty() {
96 assertDone(500, toFailedIdsResponse());
99 private String toFailedIdsResponse(String... ids) {
100 return "{ \"failedIds\": " + arrayToJson(ids) + " }";
104 public void retryWithSameItemIdsWhenResponse500AndFailedToParseResponse() {
106 final String[] expectedItemIds = {UUID.randomUUID().toString(), UUID.randomUUID().toString()};
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());
113 EqualToJsonPattern expectedRequestBody = new EqualToJsonPattern(arrayToJson(expectedItemIds), true, false);
114 verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody));
116 stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200).withBody("{}"))
117 .inScenario(MALFORMED_RESPONSE_SCENARIO));
118 assertEquals(DONE, task.call());
120 verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody));
124 public void retryWithFailedItemsWhenResponse500() {
126 final String failedId = UUID.randomUUID().toString();
127 final String successId = UUID.randomUUID().toString();
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)));
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)));
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)));