2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
24 package org.onap.appc.sdc.listener;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.openecomp.sdc.api.notification.IArtifactInfo;
29 import org.openecomp.sdc.api.notification.INotificationData;
30 import org.openecomp.sdc.api.notification.IResourceInstance;
33 import java.io.FileInputStream;
34 import java.lang.reflect.Constructor;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.net.URISyntaxException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.stream.Collectors;
43 public class SdcTestUtils {
46 public void testToSdcStoreDocumentInput() throws Exception{
47 Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput
48 (getNotificationData(),getResources(),getServiceArtifact(),"Mock data"));
52 public void testProviderResponse(){
53 ProviderResponse response=new ProviderResponse(200,"Success");
54 Assert.assertEquals(200,response.getStatus());
55 Assert.assertEquals("Success",response.getBody());
58 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
59 InstantiationException, InvocationTargetException {
61 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
63 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
65 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
66 invokeMethod(notificationData,"setServiceUUID","4564-4567-7897");
67 invokeMethod(notificationData,"setDistributionID","Distribution ID Mock");
68 invokeMethod(notificationData,"setServiceDescription","Service Description Mock");
69 invokeMethod(notificationData,"setServiceName","Service Name Mock");
71 return notificationData;
74 private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException,
75 InstantiationException, IllegalAccessException {
76 IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
78 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
79 invokeMethod(resource,"setArtifacts",serviceArtifacts);
80 invokeMethod(resource,"setResourceName","Vnf");
81 invokeMethod(resource,"setResourceVersion","1.0");
82 invokeMethod(resource,"setResourceUUID","Resource UUID");
87 private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
88 Method[] methods = object.getClass().getDeclaredMethods();
89 for(Method method:methods){
90 if(methodName.equalsIgnoreCase(method.getName())){
91 method.setAccessible(true);
92 method.invoke(object,arguments);
96 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
97 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
99 .filter(constructor1 -> constructor1.getParameterCount()==0)
100 .collect(Collectors.toList())
102 constructor.setAccessible(true);
103 return constructor.newInstance();
105 private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
106 InstantiationException, IllegalAccessException {
107 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
108 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
109 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
110 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
111 serviceArtifacts.add(artifactInfo);
112 return serviceArtifacts;
114 private IArtifactInfo getServiceArtifact() throws ClassNotFoundException, InvocationTargetException,
115 InstantiationException, IllegalAccessException {
116 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
117 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
118 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
121 private String readInput(String inputFile) throws URISyntaxException {
122 File file = new File(this.getClass().getResource(inputFile).toURI());
123 byte[] bFile = new byte[(int) file.length()];
124 try(FileInputStream fileInputStream = new FileInputStream(file)){
125 fileInputStream.read(bFile);
126 } catch (Exception e){
129 return new String(bFile);