1 package org.onap.appc.sdc.listener;
3 import org.junit.Assert;
5 import org.openecomp.sdc.api.notification.IArtifactInfo;
6 import org.openecomp.sdc.api.notification.INotificationData;
7 import org.openecomp.sdc.api.notification.IResourceInstance;
10 import java.io.FileInputStream;
11 import java.lang.reflect.Constructor;
12 import java.lang.reflect.InvocationTargetException;
13 import java.lang.reflect.Method;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18 import java.util.stream.Collectors;
20 public class SdcTestUtils {
23 public void testToSdcStoreDocumentInput() throws Exception{
24 Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput
25 (getNotificationData(),getResources(),getServiceArtifact(),"Mock data"));
29 public void testProviderResponse(){
30 ProviderResponse response=new ProviderResponse(200,"Success");
31 Assert.assertEquals(200,response.getStatus());
32 Assert.assertEquals("Success",response.getBody());
35 private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
36 InstantiationException, InvocationTargetException {
38 INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
40 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
42 invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
43 invokeMethod(notificationData,"setServiceUUID","4564-4567-7897");
44 invokeMethod(notificationData,"setDistributionID","Distribution ID Mock");
45 invokeMethod(notificationData,"setServiceDescription","Service Description Mock");
46 invokeMethod(notificationData,"setServiceName","Service Name Mock");
48 return notificationData;
51 private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException,
52 InstantiationException, IllegalAccessException {
53 IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
55 List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
56 invokeMethod(resource,"setArtifacts",serviceArtifacts);
57 invokeMethod(resource,"setResourceName","Vnf");
58 invokeMethod(resource,"setResourceVersion","1.0");
59 invokeMethod(resource,"setResourceUUID","Resource UUID");
64 private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
65 Method[] methods = object.getClass().getDeclaredMethods();
66 for(Method method:methods){
67 if(methodName.equalsIgnoreCase(method.getName())){
68 method.setAccessible(true);
69 method.invoke(object,arguments);
73 private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
74 Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
76 .filter(constructor1 -> constructor1.getParameterCount()==0)
77 .collect(Collectors.toList())
79 constructor.setAccessible(true);
80 return constructor.newInstance();
82 private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
83 InstantiationException, IllegalAccessException {
84 List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
85 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
86 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
87 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
88 serviceArtifacts.add(artifactInfo);
89 return serviceArtifacts;
91 private IArtifactInfo getServiceArtifact() throws ClassNotFoundException, InvocationTargetException,
92 InstantiationException, IllegalAccessException {
93 IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.openecomp.sdc.impl.ArtifactInfoImpl");
94 invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
95 invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
98 private String readInput(String inputFile) throws URISyntaxException {
99 File file = new File(this.getClass().getResource(inputFile).toURI());
100 byte[] bFile = new byte[(int) file.length()];
101 try(FileInputStream fileInputStream = new FileInputStream(file)){
102 fileInputStream.read(bFile);
103 } catch (Exception e){
106 return new String(bFile);