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 / NotifierFactoryTest.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;
18
19 import static org.hamcrest.CoreMatchers.startsWith;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.FileNotFoundException;
24 import java.net.URL;
25 import java.util.Collections;
26 import java.util.Set;
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;
33
34 /**
35  * @author evitaliy
36  * @since 26 Nov 2018
37  */
38 public class NotifierFactoryTest {
39
40     private static final String CONFIG_LOCATION_PROPERTY = "configuration.yaml";
41
42     @Rule
43     public ExpectedException exception = ExpectedException.none();
44
45     @Before
46     public void clearConfigLocation() {
47         System.clearProperty(CONFIG_LOCATION_PROPERTY);
48     }
49
50     @Test
51     public void notifierFactoryReturnsAnInstance() {
52         assertNotNull(NotifierFactory.getInstance());
53     }
54
55     @Test
56     public void unsupportedConfigurationNotifierWhenConfigurationLocationNotGiven() {
57         assertTrue(NotifierFactory.createInstance() instanceof NotifierFactory.UnsupportedConfigurationNotifier);
58     }
59
60     @Test
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);
66     }
67
68     private String getConfigPath(String classpathFile) throws FileNotFoundException {
69
70         URL resource = Thread.currentThread().getContextClassLoader().getResource(classpathFile);
71         if (resource == null) {
72             throw new FileNotFoundException("Cannot find resource: " + classpathFile);
73         }
74
75         return resource.getPath();
76     }
77
78     @Test
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);
83     }
84
85     @Test
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);
91     }
92
93     @Test
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);
99     }
100
101 }