57a4c094a7d16f0f8aa6d002e43541c41ecc1d76
[aai/model-loader.git] / src / test / java / org / onap / aai / modelloader / notification / EventCallbackTest.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.Matchers.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.mockito.internal.util.reflection.Whitebox;
36 import org.onap.aai.modelloader.config.ModelLoaderConfig;
37 import org.onap.aai.modelloader.entity.model.BabelArtifactParsingException;
38 import org.onap.aai.modelloader.fixture.NotificationDataFixtureBuilder;
39 import org.onap.sdc.api.IDistributionClient;
40 import org.onap.sdc.api.notification.INotificationData;
41
42 /**
43  * Tests {@link EventCallback}
44  */
45 public class EventCallbackTest {
46
47     private static final String CONFIG_FILE = "model-loader.properties";
48
49     private ModelLoaderConfig config;
50     private Properties configProperties;
51     private EventCallback eventCallback;
52
53     private ArtifactDeploymentManager mockArtifactDeploymentManager;
54     private ArtifactDownloadManager mockArtifactDownloadManager;
55     private IDistributionClient mockDistributionClient;
56
57     @Before
58     public void setup() throws IOException {
59         configProperties = new Properties();
60         configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE));
61         config = new ModelLoaderConfig(configProperties, null);
62
63         mockArtifactDeploymentManager = mock(ArtifactDeploymentManager.class);
64         mockArtifactDownloadManager = mock(ArtifactDownloadManager.class);
65         mockDistributionClient = mock(IDistributionClient.class);
66
67         eventCallback = new EventCallback(mockDistributionClient, config);
68
69         Whitebox.setInternalState(eventCallback, "artifactDeploymentManager", mockArtifactDeploymentManager);
70         Whitebox.setInternalState(eventCallback, "artifactDownloadManager", mockArtifactDownloadManager);
71     }
72
73     @After
74     public void tearDown() {
75         config = null;
76         configProperties = null;
77         eventCallback = null;
78         mockArtifactDeploymentManager = null;
79         mockArtifactDownloadManager = null;
80         mockDistributionClient = null;
81     }
82
83     @Test
84     @SuppressWarnings("unchecked")
85     public void activateCallback_downloadFails() {
86         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
87
88         when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
89                 any(List.class), any(List.class))).thenReturn(false);
90
91         eventCallback.activateCallback(data);
92
93         verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class),
94                 any(List.class), any(List.class));
95         Mockito.verifyZeroInteractions(mockArtifactDeploymentManager);
96     }
97
98     @SuppressWarnings("unchecked")
99     @Test
100     public void activateCallback() throws BabelArtifactParsingException {
101         INotificationData data = NotificationDataFixtureBuilder.getNotificationDataWithToscaCsarFile();
102
103         when(mockArtifactDownloadManager.downloadArtifacts(any(INotificationData.class), any(List.class),
104                 any(List.class), any(List.class))).thenReturn(true);
105
106         when(mockArtifactDeploymentManager.deploy(any(INotificationData.class), any(List.class), any(List.class),
107                 any(List.class))).thenReturn(true);
108
109         eventCallback.activateCallback(data);
110
111         verify(mockArtifactDownloadManager).downloadArtifacts(any(INotificationData.class), any(List.class),
112                 any(List.class), any(List.class));
113         verify(mockArtifactDeploymentManager).deploy(any(INotificationData.class), any(List.class), any(List.class),
114                 any(List.class));
115     }
116 }