From d8906a0cc7fcc302020e983fdfade2758663ba4d Mon Sep 17 00:00:00 2001 From: avigaffa Date: Sun, 18 Nov 2018 16:01:07 +0200 Subject: error when trying to archive\restore fix bug: getting server error when trying to archive\restore VLM\VSP Change-Id: I7abefd2d8ac368d590329071a56f200c203cf966 Issue-ID: SDC-1667 Signed-off-by: avigaffa --- .../catalog/notification/AsyncNotifierTest.java | 104 ++++++++++++++ .../catalog/notification/NotifierFactoryTest.java | 101 ++++++++++++++ .../http/HttpNotificationTaskTest.java | 150 +++++++++++++++++++++ .../notification/http/HttpTaskProducerTest.java | 121 +++++++++++++++++ 4 files changed, 476 insertions(+) create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifierTest.java create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactoryTest.java create mode 100644 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 create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducerTest.java (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifierTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifierTest.java new file mode 100644 index 0000000000..900fc940ee --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/AsyncNotifierTest.java @@ -0,0 +1,104 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdcrests.item.rest.services.catalog.notification; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE; +import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY; + +import java.util.concurrent.Callable; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.apache.commons.lang.mutable.MutableInt; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.stubbing.Answer; + +/** + * @author evitaliy + * @since 22 Nov 2018 + */ +public class AsyncNotifierTest { + + @Test + public void taskRunsOnceWhenSuccessful() throws Exception { + + ScheduledExecutorService executorServiceMock = createMockScheduledExecutor(); + + MutableInt counter = new MutableInt(0); + Callable countingTask = () -> { + counter.increment(); + return DONE; + }; + + AsyncNotifier.RetryingTask retryingTask = + new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock); + retryingTask.call(); + assertEquals(1, counter.intValue()); + } + + @Test + public void taskRunsTwiceWhenFailedFirstTime() throws Exception { + + ScheduledExecutorService executorServiceMock = createMockScheduledExecutor(); + + MutableInt counter = new MutableInt(0); + Callable countingTask = () -> { + counter.increment(); + return counter.intValue() < 2 ? RETRY : DONE; + }; + + AsyncNotifier.RetryingTask retryingTask = + new AsyncNotifier.RetryingTask(countingTask, 10, 10, executorServiceMock); + retryingTask.call(); + assertEquals(2, counter.intValue()); + } + + @Test + public void exhaustedAttemptsWhenTaskAlwaysFails() throws Exception { + + ScheduledExecutorService executorServiceMock = createMockScheduledExecutor(); + + MutableInt counter = new MutableInt(0); + Callable countingTask = () -> { + counter.increment(); + return RETRY; + }; + + final int numOfRetries = 10; + AsyncNotifier.RetryingTask retryingTask = + new AsyncNotifier.RetryingTask(countingTask, numOfRetries, 10, executorServiceMock); + retryingTask.call(); + assertEquals(numOfRetries, counter.intValue()); + } + + private ScheduledExecutorService createMockScheduledExecutor() { + + ScheduledExecutorService executorServiceMock = Mockito.mock(ScheduledExecutorService.class); + Answer passThrough = invocation -> { + ((Callable) invocation.getArgument(0)).call(); + return null; + }; + + Mockito.doAnswer(passThrough).when(executorServiceMock).submit(any(Callable.class)); + Mockito.doAnswer(passThrough).when(executorServiceMock) + .schedule(any(Callable.class), anyLong(), any(TimeUnit.class)); + return executorServiceMock; + } +} \ No newline at end of file diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactoryTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactoryTest.java new file mode 100644 index 0000000000..2744682811 --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/NotifierFactoryTest.java @@ -0,0 +1,101 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdcrests.item.rest.services.catalog.notification; + +import static org.hamcrest.CoreMatchers.startsWith; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.FileNotFoundException; +import java.net.URL; +import java.util.Collections; +import java.util.Set; +import java.util.UUID; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.openecomp.sdcrests.item.types.ItemAction; + +/** + * @author evitaliy + * @since 26 Nov 2018 + */ +public class NotifierFactoryTest { + + private static final String CONFIG_LOCATION_PROPERTY = "configuration.yaml"; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void clearConfigLocation() { + System.clearProperty(CONFIG_LOCATION_PROPERTY); + } + + @Test + public void notifierFactoryReturnsAnInstance() { + assertNotNull(NotifierFactory.getInstance()); + } + + @Test + public void unsupportedConfigurationNotifierWhenConfigurationLocationNotGiven() { + assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier); + } + + @Test + public void asyncNotifierReturnedWhenConfigurationCorrect() throws FileNotFoundException { + String configPath = getConfigPath("catalog-notification-config-correct.yaml"); + System.setProperty(CONFIG_LOCATION_PROPERTY, configPath); + assertTrue("Configuration file must be present and correct", + NotifierFactory.createInstance() instanceof AsyncNotifier); + } + + private String getConfigPath(String classpathFile) throws FileNotFoundException { + + URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile); + if (resource == null) { + throw new FileNotFoundException("Cannot find resource: " + classpathFile); + } + + return resource.getPath(); + } + + @Test + public void unsupportedConfigurationNotifierReturnedWhenConfigurationEmpty() throws FileNotFoundException { + String configPath = getConfigPath("catalog-notification-config-empty.yaml"); + System.setProperty(CONFIG_LOCATION_PROPERTY, configPath); + assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier); + } + + @Test + public void unsupportedConfigurationNotifierReturnedWhenConfigurationDoesNotHaveNotificationSection() + throws FileNotFoundException { + String configPath = getConfigPath("catalog-notification-config-without-notification-section.yaml"); + System.setProperty(CONFIG_LOCATION_PROPERTY, configPath); + assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier); + } + + @Test + public void unsupportedConfigurationNotifierThrowsException() { + exception.expect(IllegalStateException.class); + exception.expectMessage(startsWith("Cannot send notifications")); + Set itemIds = Collections.singleton(UUID.randomUUID().toString()); + new NotifierFactory.UnsupportedConfigurationNotifier().execute(itemIds, ItemAction.ARCHIVE); + } + +} \ No newline at end of file diff --git a/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 b/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 new file mode 100644 index 0000000000..1511fc7547 --- /dev/null +++ b/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 @@ -0,0 +1,150 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdcrests.item.rest.services.catalog.notification.http; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static com.github.tomakehurst.wiremock.client.WireMock.verify; +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; +import static org.junit.Assert.assertEquals; +import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.DONE; +import static org.openecomp.sdcrests.item.rest.services.catalog.notification.AsyncNotifier.NextAction.RETRY; + +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.github.tomakehurst.wiremock.matching.EqualToJsonPattern; +import com.github.tomakehurst.wiremock.matching.EqualToPattern; +import java.util.Arrays; +import java.util.Collections; +import java.util.UUID; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +/** + * @author evitaliy + * @since 22 Nov 2018 + */ +public class HttpNotificationTaskTest { + + @ClassRule + public static final WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort()); + + private static final String NOTIFICATION_PATH = "/notification"; + private static final String USER_ID = "d75360e1-f393-480f-b39e-fbbdf38a22c1"; + private static final String RETRY_SCENARIO = "RETRY_SCENARIO"; + private static final String MALFORMED_RESPONSE_SCENARIO = "MALFORMED_RESPONSE_SCENARIO"; + + private static String endpoint; + + @BeforeClass + public static void initConfiguration() { + endpoint = "http://localhost:" + wireMockRule.port() + NOTIFICATION_PATH; + } + + @Test + public void doneWhenResponseOk() { + assertDone(200, arrayToJson(UUID.randomUUID().toString())); + } + + private void assertDone(int responseStatus, String body) { + final String itemId = UUID.randomUUID().toString(); + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(responseStatus).withBody(body))); + HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Collections.singleton(itemId)); + assertEquals(DONE, task.call()); + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)) + .withRequestBody(new EqualToJsonPattern(arrayToJson(itemId), true, false))); + } + + private String arrayToJson(String... ids) { + return ids.length == 0 ? "[]" : "[ \"" + String.join("\", \"", ids) + "\" ]"; + } + + @Test + public void doneWhenResponse400() { + assertDone(400, arrayToJson(UUID.randomUUID().toString())); + } + + @Test + public void doneWhenResponse522() { + assertDone(522, arrayToJson(UUID.randomUUID().toString())); + } + + @Test + public void doneWhenResponse500ButFailedIdsNotReturned() { + assertDone(500, "{}"); + } + + @Test + public void doneWhenResponse500ButFailedIdsEmpty() { + assertDone(500, toFailedIdsResponse()); + } + + private String toFailedIdsResponse(String... ids) { + return "{ \"failedIds\": " + arrayToJson(ids) + " }"; + } + + @Test + public void retryWithSameItemIdsWhenResponse500AndFailedToParseResponse() { + + final String[] expectedItemIds = {UUID.randomUUID().toString(), UUID.randomUUID().toString()}; + + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(500).withBody("d[g.0g,y/")) + .inScenario(MALFORMED_RESPONSE_SCENARIO)); + HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Arrays.asList(expectedItemIds)); + assertEquals(RETRY, task.call()); + + EqualToJsonPattern expectedRequestBody = new EqualToJsonPattern(arrayToJson(expectedItemIds), true, false); + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody)); + + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200).withBody("{}")) + .inScenario(MALFORMED_RESPONSE_SCENARIO)); + assertEquals(DONE, task.call()); + + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withRequestBody(expectedRequestBody)); + } + + @Test + public void retryWithFailedItemsWhenResponse500() { + + final String failedId = UUID.randomUUID().toString(); + final String successId = UUID.randomUUID().toString(); + + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(500).withBody(toFailedIdsResponse(failedId))) + .inScenario(RETRY_SCENARIO)); + HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Arrays.asList(failedId, successId)); + assertEquals(RETRY, task.call()); + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)) + .withRequestBody(new EqualToJsonPattern(arrayToJson(failedId, successId), true, false))); + + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200).withBody("{}")) + .inScenario(RETRY_SCENARIO)); + assertEquals(DONE, task.call()); + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)) + .withRequestBody(new EqualToJsonPattern(arrayToJson(failedId), true, false))); + } + + @Test + public void userIdSentToServer() { + stubFor(post(NOTIFICATION_PATH).willReturn(aResponse().withStatus(200))); + HttpNotificationTask task = new HttpNotificationTask(endpoint, USER_ID, Collections.emptyList()); + assertEquals(DONE, task.call()); + verify(postRequestedFor(urlEqualTo(NOTIFICATION_PATH)).withHeader("USER_ID", new EqualToPattern(USER_ID))); + } +} \ No newline at end of file diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducerTest.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducerTest.java new file mode 100644 index 0000000000..3c12b37e6d --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/test/java/org/openecomp/sdcrests/item/rest/services/catalog/notification/http/HttpTaskProducerTest.java @@ -0,0 +1,121 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdcrests.item.rest.services.catalog.notification.http; + +import static org.hamcrest.CoreMatchers.containsString; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.openecomp.sdcrests.item.rest.services.catalog.notification.EntryNotConfiguredException; + +/** + * @author evitaliy + * @since 26 Nov 2018 + */ +public class HttpTaskProducerTest { + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void errorWhenProtocolNotDefined() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol(null); + exception.expect(EntryNotConfiguredException.class); + exception.expectMessage(containsString("Protocol")); + new HttpTaskProducer(config); + } + + @Test + public void errorWhenFqdnNotDefined() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeFqdn(null); + exception.expect(EntryNotConfiguredException.class); + exception.expectMessage(containsString("Catalog host")); + new HttpTaskProducer(config); + } + + @Test + public void errorWhenNotificationUrlNotDefined() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogNotificationUrl(null); + exception.expect(EntryNotConfiguredException.class); + exception.expectMessage(containsString("Notification URL")); + new HttpTaskProducer(config); + } + + @Test + public void errorWhenUnknownProtocol() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("invented-protocol"); + exception.expect(IllegalArgumentException.class); + exception.expectMessage(containsString("Unsupported protocol")); + new HttpTaskProducer(config); + } + + @Test + public void errorWhenHttpUsedButHttpPortUndefined() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("http"); + config.setCatalogBeHttpPort(null); + exception.expect(EntryNotConfiguredException.class); + exception.expectMessage(containsString("HTTP port")); + new HttpTaskProducer(config); + } + + @Test + public void errorWhenSslUsedButHttpsPortUndefined() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("https"); + config.setCatalogBeSslPort(null); + exception.expect(EntryNotConfiguredException.class); + exception.expectMessage(containsString("SSL port")); + new HttpTaskProducer(config); + } + + @Test + public void okWhenProtocolHttps() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("https"); + new HttpTaskProducer(config); + } + + @Test + public void okWhenProtocolHttpsMixedCase() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("hTTpS"); + new HttpTaskProducer(config); + } + + @Test + public void okWhenProtocolHttpMixedCase() { + HttpConfiguration config = mockConfiguration(); + config.setCatalogBeProtocol("HTtp"); + new HttpTaskProducer(config); + } + + private HttpConfiguration mockConfiguration() { + HttpConfiguration config = new HttpConfiguration(); + config.setCatalogBeFqdn("fqdn"); + config.setCatalogBeHttpPort("http-port"); + config.setCatalogBeProtocol("http"); + config.setCatalogBeSslPort("ssl-port"); + config.setCatalogNotificationUrl("url"); + return config; + } +} \ No newline at end of file -- cgit 1.2.3-korg