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;
19 import static org.hamcrest.CoreMatchers.startsWith;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
23 import java.io.FileNotFoundException;
25 import java.util.Collections;
27 import java.util.UUID;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.ExpectedException;
32 import org.openecomp.sdcrests.item.types.ItemAction;
38 public class NotifierFactoryTest {
40 private static final String CONFIG_LOCATION_PROPERTY = "configuration.yaml";
43 public ExpectedException exception = ExpectedException.none();
46 public void clearConfigLocation() {
47 System.clearProperty(CONFIG_LOCATION_PROPERTY);
51 public void notifierFactoryReturnsAnInstance() {
52 assertNotNull(NotifierFactory.getInstance());
56 public void unsupportedConfigurationNotifierWhenConfigurationLocationNotGiven() {
57 assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier);
61 public void asyncNotifierReturnedWhenConfigurationCorrect() throws FileNotFoundException {
62 String configPath = getConfigPath("catalog-notification-config-correct.yaml");
63 System.setProperty(CONFIG_LOCATION_PROPERTY, configPath);
64 assertTrue("Configuration file must be present and correct",
65 NotifierFactory.createInstance() instanceof AsyncNotifier);
68 private String getConfigPath(String classpathFile) throws FileNotFoundException {
70 URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
71 if (resource == null) {
72 throw new FileNotFoundException("Cannot find resource: " + classpathFile);
75 return resource.getPath();
79 public void unsupportedConfigurationNotifierReturnedWhenConfigurationEmpty() throws FileNotFoundException {
80 String configPath = getConfigPath("catalog-notification-config-empty.yaml");
81 System.setProperty(CONFIG_LOCATION_PROPERTY, configPath);
82 assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier);
86 public void unsupportedConfigurationNotifierReturnedWhenConfigurationDoesNotHaveNotificationSection()
87 throws FileNotFoundException {
88 String configPath = getConfigPath("catalog-notification-config-without-notification-section.yaml");
89 System.setProperty(CONFIG_LOCATION_PROPERTY, configPath);
90 assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier);
94 public void unsupportedConfigurationNotifierThrowsException() {
95 exception.expect(IllegalStateException.class);
96 exception.expectMessage(startsWith("Cannot send notifications"));
97 Set<String> itemIds = Collections.singleton(UUID.randomUUID().toString());
98 new NotifierFactory.UnsupportedConfigurationNotifier().execute(itemIds, ItemAction.ARCHIVE);