Feature for micro service communication
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / artifacts / impl / TestConfigArtifcatProcessor.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.Matchers.anyString;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import java.net.URI;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mockito;
31 import org.onap.appc.adapter.message.EventSender;
32 import org.onap.appc.exceptions.APPCException;
33 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
34 import org.onap.appc.sdc.listener.ProviderOperations;
35 import org.onap.appc.sdc.listener.ProviderResponse;
36 import org.onap.appc.sdc.listener.Util;
37 import org.onap.sdc.api.IDistributionClient;
38 import org.onap.sdc.api.notification.IArtifactInfo;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.onap.sdc.api.notification.IResourceInstance;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44
45 /**
46  * This class contains test methods for ConfigArtifact Processor.
47  *
48  */
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({ProviderOperations.class, Util.class})
52 public class TestConfigArtifcatProcessor {
53
54   private ConfigArtifactProcessor configArtifactProcessor;
55
56   private SDCArtifact sdcArtifact;
57
58   private URI uri = URI.create("http://localhost:8080");
59
60   private IDistributionClient client;
61
62   private EventSender eventSender;
63
64   private ProviderResponse response;
65
66   private INotificationData notificationData;
67
68   private IResourceInstance resourceInstance;
69   
70   private IArtifactInfo artifactInfo;
71
72   @Before
73   public void setup() throws APPCException {
74     client = Mockito.mock(IDistributionClient.class);
75     notificationData = Mockito.mock(INotificationData.class);
76     resourceInstance = Mockito.mock(IResourceInstance.class);
77     artifactInfo = Mockito.mock(IArtifactInfo.class);
78     sdcArtifact = Mockito.mock(SDCArtifact.class);
79     eventSender = Mockito.mock(EventSender.class);
80     configArtifactProcessor = new ConfigArtifactProcessor(client, eventSender, notificationData,
81         resourceInstance, artifactInfo, uri);
82     response = new ProviderResponse(200, "{\"key\":\"value\"}");
83   }
84
85   /**
86    * This method tests the process Artifacts method success scenario.
87    * 
88    * @throws APPCException if the there are any exception in sdc artifacts
89    */
90   @Test
91   public void testProcessArtifact() throws APPCException {
92     PowerMockito.mockStatic(ProviderOperations.class);
93     PowerMockito.mockStatic(Util.class);
94     when(ProviderOperations.post(anyObject(), anyString(), anyObject())).thenReturn(response);
95     when(Util.parseResponse(anyObject())).thenReturn(true);
96     ConfigArtifactProcessor configArtifactProcessorSpy = Mockito.spy(configArtifactProcessor);
97     configArtifactProcessorSpy.processArtifact(sdcArtifact);
98     verify(configArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact);
99   }
100
101   /**
102    * This method tests the process Artifacts method failure scenario.
103    * 
104    * @throws APPCException if the there are any exception in sdc artifacts
105    */
106   @Test(expected = APPCException.class)
107   public void testProcessArtifactFail() throws APPCException {
108     PowerMockito.mockStatic(ProviderOperations.class);
109     when(ProviderOperations.post(anyObject(), anyString(), anyObject())).thenReturn(response);
110     configArtifactProcessor.processArtifact(sdcArtifact);
111   }
112
113   /**
114    * This method tests the process Artifacts method failure scenario when the uri is null.
115    * 
116    * @throws APPCException if the there are any exception in sdc artifacts or in creating
117    *         notification data, resource data & service artifacts
118    */
119   @Test
120   public void testProcessArtifactWithNoURI() throws APPCException {
121     configArtifactProcessor = new ConfigArtifactProcessor(client, eventSender, notificationData,
122         resourceInstance, artifactInfo, null);
123     ConfigArtifactProcessor configArtifactProcessorSpy = Mockito.spy(configArtifactProcessor);
124     configArtifactProcessorSpy.processArtifact(sdcArtifact);
125     verify(configArtifactProcessorSpy, times(1)).processArtifact(sdcArtifact);
126   }
127 }