Second part of onap rename
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / listener / SdcTestUtils.java
1 package org.onap.appc.sdc.listener;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.openecomp.sdc.api.notification.IArtifactInfo;
6 import org.openecomp.sdc.api.notification.INotificationData;
7 import org.openecomp.sdc.api.notification.IResourceInstance;
8
9 import java.io.File;
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;
19
20 public class SdcTestUtils {
21
22     @Test
23     public void testToSdcStoreDocumentInput() throws Exception{
24         Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput
25                 (getNotificationData(),getResources(),getServiceArtifact(),"Mock data"));
26     }
27
28     @Test
29     public void testProviderResponse(){
30         ProviderResponse response=new ProviderResponse(200,"Success");
31         Assert.assertEquals(200,response.getStatus());
32         Assert.assertEquals("Success",response.getBody());
33     }
34
35     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
36             InstantiationException, InvocationTargetException {
37
38         INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
39
40         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
41
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");
47
48         return notificationData;
49     }
50
51     private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException,
52             InstantiationException, IllegalAccessException {
53         IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
54
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");
60
61         return resource;
62     }
63
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);
70             }
71         }
72     }
73     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
74         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
75                 .stream()
76                 .filter(constructor1 -> constructor1.getParameterCount()==0)
77                 .collect(Collectors.toList())
78                 .get(0);
79         constructor.setAccessible(true);
80         return constructor.newInstance();
81     }
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;
90     }
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");
96         return artifactInfo;
97     }
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){
104             e.printStackTrace();
105         }
106         return new String(bFile);
107     }
108 }