Feature for micro service communication
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / artifacts / impl / TestAbstractArtifactProcessor.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
19 package org.onap.appc.sdc.artifacts.impl;
20
21 import static org.mockito.Matchers.anyObject;
22 import static org.mockito.Mockito.CALLS_REAL_METHODS;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.mockito.internal.util.reflection.Whitebox;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.onap.appc.adapter.message.EventSender;
33 import org.onap.sdc.api.IDistributionClient;
34 import org.onap.sdc.api.notification.IArtifactInfo;
35 import org.onap.sdc.api.notification.INotificationData;
36 import org.onap.sdc.api.notification.IResourceInstance;
37 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
38 import org.onap.sdc.utils.DistributionActionResultEnum;
39 import com.att.eelf.configuration.EELFLogger;
40
41 /**
42  * This class tests the Abstract Artifact Processor
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class TestAbstractArtifactProcessor {
46
47   private AbstractArtifactProcessor abstractArtifactProcessor;
48
49   private EELFLogger logger;
50
51   private IDistributionClient client;
52
53   private IDistributionClientDownloadResult distributionClientDownloadResult;
54
55   private IArtifactInfo artifactInfo;
56
57   private INotificationData notificationData;
58
59   private EventSender eventSender;
60
61   private IResourceInstance resource;
62
63   /**
64    * Setup the test environment by loading a new Abstract artifact Processor
65    */
66   @Before
67   public void setup() {
68     abstractArtifactProcessor = Mockito.mock(AbstractArtifactProcessor.class, CALLS_REAL_METHODS);
69     logger = Mockito.mock(EELFLogger.class);
70     Mockito.doCallRealMethod().when(abstractArtifactProcessor).run();
71     Whitebox.setInternalState(abstractArtifactProcessor, "logger", logger);
72     client = Mockito.mock(IDistributionClient.class);
73     distributionClientDownloadResult = Mockito.mock(IDistributionClientDownloadResult.class);
74     artifactInfo = Mockito.mock(IArtifactInfo.class);
75     notificationData = Mockito.mock(INotificationData.class);
76     eventSender = Mockito.mock(EventSender.class);
77     resource = Mockito.mock(IResourceInstance.class);
78     Whitebox.setInternalState(abstractArtifactProcessor, "client", client);
79     Whitebox.setInternalState(abstractArtifactProcessor, "artifact", artifactInfo);
80     Whitebox.setInternalState(abstractArtifactProcessor, "notification", notificationData);
81     Whitebox.setInternalState(abstractArtifactProcessor, "eventSender", eventSender);
82     Whitebox.setInternalState(abstractArtifactProcessor, "resource", resource);
83   }
84
85   /**
86    * This method tests the abstract Artifacts method success scenario
87    */
88   @Test
89   public void testRun() {
90     when(client.download(anyObject())).thenReturn(distributionClientDownloadResult);
91     when(distributionClientDownloadResult.getDistributionActionResult())
92         .thenReturn(DistributionActionResultEnum.SUCCESS);
93     when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult);
94     when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]);
95     abstractArtifactProcessor.run();
96     verify(abstractArtifactProcessor, times(1)).run();
97   }
98
99   /**
100    * This method tests the abstract Artifacts method failure scenario
101    */
102   @Test
103   public void testRunWithDownloadFailed() {
104     when(client.download(anyObject())).thenReturn(distributionClientDownloadResult);
105     when(distributionClientDownloadResult.getDistributionActionResult())
106         .thenReturn(DistributionActionResultEnum.FAIL);
107     when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult);
108     when(distributionClientDownloadResult.getArtifactPayload()).thenReturn(new byte[1]);
109     abstractArtifactProcessor.run();
110     verify(abstractArtifactProcessor, times(1)).run();
111   }
112
113   /**
114    * This method tests the abstract Artifacts method failure scenario
115    */
116   @Test
117   public void testRunWithException() {
118     when(client.download(anyObject())).thenReturn(distributionClientDownloadResult);
119     when(distributionClientDownloadResult.getDistributionActionResult())
120         .thenReturn(DistributionActionResultEnum.SUCCESS);
121     when(client.sendDownloadStatus(anyObject())).thenReturn(distributionClientDownloadResult);
122     when(distributionClientDownloadResult.getArtifactPayload()).thenThrow(new RuntimeException());
123     abstractArtifactProcessor.run();
124     verify(abstractArtifactProcessor, times(1)).run();
125   }
126 }