738febef6fbfe0e159063e84379a7478ec5afeea
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / artifacts / impl / TestToscaCsarArtifactProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  * file except in compliance with the License. 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 distributed under the License
11  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12  * or implied. See the License for the specific language governing permissions and limitations under
13  * the License.
14  *
15  * SPDX-License-Identifier: Apache-2.0
16  * ============LICENSE_END=========================================================
17  */
18 package org.onap.appc.sdc.artifacts.impl;
19
20 import static org.mockito.Matchers.anyString;
21 import static org.mockito.Mockito.times;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.onap.appc.adapter.message.EventSender;
28 import org.onap.appc.exceptions.APPCException;
29 import org.onap.appc.sdc.artifacts.helper.ArtifactStorageService;
30 import org.onap.appc.sdc.artifacts.helper.DependencyModelGenerator;
31 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
32 import org.onap.sdc.api.IDistributionClient;
33 import org.onap.sdc.api.notification.IArtifactInfo;
34 import org.onap.sdc.api.notification.INotificationData;
35 import org.onap.sdc.api.notification.IResourceInstance;
36 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
37 import org.powermock.reflect.Whitebox;
38
39 /**
40  * This class contains test methods for ToscaCsarArtifact Processor
41  *
42  */
43
44 public class TestToscaCsarArtifactProcessor {
45
46   private ToscaCsarArtifactProcessor toscaCsarArtifactProcessor;
47
48   private DependencyModelGenerator dependencyModelGenerator;
49
50   private ArtifactStorageService storageService;
51
52   private SDCArtifact sdcArtifact;
53
54   private IDistributionClientDownloadResult distributionClientDownloadResult;
55
56   private IDistributionClient client;
57
58   private EventSender eventSender;
59
60   private INotificationData notificationData;
61
62   private IResourceInstance resourceInstance;
63
64   private IArtifactInfo artifactInfo;
65
66   @Before
67   public void setup() throws Exception {
68     client = Mockito.mock(IDistributionClient.class);
69     eventSender = Mockito.mock(EventSender.class);
70     storageService = Mockito.mock(ArtifactStorageService.class);
71     distributionClientDownloadResult = Mockito.mock(IDistributionClientDownloadResult.class);
72     dependencyModelGenerator = Mockito.mock(DependencyModelGenerator.class);
73     toscaCsarArtifactProcessor = new ToscaCsarArtifactProcessor(client, eventSender,
74         notificationData, resourceInstance, artifactInfo, null);
75     Whitebox.setInternalState(toscaCsarArtifactProcessor, "artifactStorageService", storageService);
76     Whitebox.setInternalState(toscaCsarArtifactProcessor, "dependencyModelGenerator",
77         dependencyModelGenerator);
78   }
79
80   /**
81    * This method tests the process Artifacts method success scenario.
82    * 
83    * @throws APPCException if the there are any exception in sdc artifacts
84    */
85   @Test
86   public void testProcessArtifact() throws APPCException {
87     ToscaCsarArtifactProcessor toscaCsarArtifactProcessorSpy =
88         Mockito.spy(toscaCsarArtifactProcessor);
89     sdcArtifact = new SDCArtifact();
90     sdcArtifact.setResourceVersion("RESOURCE VERSION");
91     sdcArtifact.setArtifactUUID("123-456-789");
92     sdcArtifact.setResourceName("Resource Name");
93     when(dependencyModelGenerator.getDependencyModel(anyString(), anyString()))
94         .thenReturn("dependencyModel");
95     toscaCsarArtifactProcessorSpy.processArtifact(sdcArtifact);
96     verify(toscaCsarArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact);
97   }
98
99   /**
100    * This method tests the process Artifacts method failure scenario.
101    * 
102    * @throws APPCException if the there are any exception in sdc artifacts
103    */
104   @Test(expected = APPCException.class)
105   public void testProcessArtifactFailWithNullValues() throws APPCException {
106     sdcArtifact = new SDCArtifact();
107     toscaCsarArtifactProcessor.processArtifact(sdcArtifact);
108   }
109
110   /**
111    * This method tests the process Artifacts method failure scenario
112    * 
113    * @throws Exception if the there are any exception in sdc artifacts or in creating notification
114    *         data, resource data & service artifacts
115    */
116   @Test(expected = APPCException.class)
117   public void testProcessArtifactFail() throws Exception {
118     sdcArtifact = new SDCArtifact();
119     sdcArtifact.setResourceVersion("RESOURCE VERSION");
120     sdcArtifact.setArtifactUUID("123-456-789");
121     sdcArtifact.setResourceName("Resource Name");
122     when(dependencyModelGenerator.getDependencyModel(anyString(), anyString()))
123         .thenThrow(new APPCException());
124     toscaCsarArtifactProcessor.processArtifact(sdcArtifact);
125   }
126
127   /**
128    * This method tests the process Artifacts method success scenario.
129    * 
130    * @throws APPCException if the there are any exception in sdc artifacts
131    */
132   // @Test
133   public void testProcessArtifactDistributionClient() throws APPCException {
134     when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]);
135     ToscaCsarArtifactProcessor toscaCsarArtifactProcessorSpy =
136         Mockito.spy(toscaCsarArtifactProcessor);
137     toscaCsarArtifactProcessorSpy.processArtifact(distributionClientDownloadResult);
138     verify(toscaCsarArtifactProcessorSpy, times(1))
139         .processArtifact(distributionClientDownloadResult);
140   }
141 }