8276d90b0a226960d6d1d9077e4e372d52794cee
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / TestEventCallback.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.modelloader.notification;
22
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.util.List;
30 import java.util.Properties;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.aai.modelloader.config.ModelLoaderConfig;
36 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
37 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
38 import org.onap.aai.modelloader.service.ArtifactDeploymentManager;
39 import org.onap.sdc.api.IDistributionClient;
40 import org.onap.sdc.api.notification.INotificationData;
41 import org.springframework.test.util.ReflectionTestUtils;
42
43 /**
44  * Tests {@link EventCallback}.
45  */
46 public class TestEventCallback {
47
48     private static final String CONFIG_FILE = "model-loader.properties";
49
50     private ModelLoaderConfig config;
51     private Properties configProperties;
52     private EventCallback eventCallback;
53
54     private ArtifactDeploymentManager mockArtifactDeploymentManager;
55     private ArtifactDownloadManager mockArtifactDownloadManager;
56     private IDistributionClient mockDistributionClient;
57     private NotificationPublisher mockNotificationPublisher;
58
59     @Before
60     public void setup() throws IOException {
61         configProperties = new Properties();
62         configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
63         config = new ModelLoaderConfig(configProperties, null);
64
65         mockArtifactDeploymentManager = mock(ArtifactDeploymentManager.class);
66         mockArtifactDownloadManager = mock(ArtifactDownloadManager.class);
67         mockDistributionClient = mock(IDistributionClient.class);
68         mockNotificationPublisher = mock(NotificationPublisher.class);
69
70         eventCallback = new EventCallback(mockDistributionClient, config, null);
71
72         ReflectionTestUtils.setField(eventCallback, "artifactDeploymentManager", mockArtifactDeploymentManager);
73         ReflectionTestUtils.setField(eventCallback, "artifactDownloadManager", mockArtifactDownloadManager);
74         ReflectionTestUtils.setField(eventCallback, "notificationPublisher", mockNotificationPublisher);
75     }
76
77     @After
78     public void tearDown() {
79         config = null;
80         configProperties = null;
81         eventCallback = null;
82         mockArtifactDeploymentManager = null;
83         mockArtifactDownloadManager = null;
84         mockDistributionClient = null;
85     }
86
87     @Test
88     @SuppressWarnings("unchecked")
89     public void activateCallback_downloadFails() {
90         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
91
92         when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
93                 any(List.class), any(List.class))).thenReturn(false);
94
95         eventCallback.activateCallback(data);
96
97         verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class),
98                 any(List.class), any(List.class));
99         Mockito.verifyZeroInteractions(mockArtifactDeploymentManager);
100     }
101
102     @SuppressWarnings("unchecked")
103     @Test
104     public void activateCallback() throws BabelArtifactParsingException {
105         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
106
107         when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
108                 any(List.class), any(List.class))).thenReturn(true);
109
110         when(mockArtifactDeploymentManager.deploy(any(INotificationData.class), any(List.class), any(List.class)))
111                 .thenReturn(true);
112
113         eventCallback.activateCallback(data);
114
115         verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class),
116                 any(List.class), any(List.class));
117         verify(mockArtifactDeploymentManager).deploy(any(INotificationData.class), any(List.class), any(List.class));
118     }
119 }