Add/update license text part 3
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / test / java / org / onap / appc / sdc / listener / SdcTestUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24 package org.onap.appc.sdc.listener;
25
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;
31
32 import java.io.File;
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;
42
43 public class SdcTestUtils {
44
45     @Test
46     public void testToSdcStoreDocumentInput() throws Exception{
47         Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput
48                 (getNotificationData(),getResources(),getServiceArtifact(),"Mock data"));
49     }
50
51     @Test
52     public void testProviderResponse(){
53         ProviderResponse response=new ProviderResponse(200,"Success");
54         Assert.assertEquals(200,response.getStatus());
55         Assert.assertEquals("Success",response.getBody());
56     }
57
58     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
59             InstantiationException, InvocationTargetException {
60
61         INotificationData notificationData = (INotificationData)getObject("org.openecomp.sdc.impl.NotificationDataImpl");
62
63         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
64
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");
70
71         return notificationData;
72     }
73
74     private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException,
75             InstantiationException, IllegalAccessException {
76         IResourceInstance resource = (IResourceInstance)getObject("org.openecomp.sdc.impl.JsonContainerResourceInstance");
77
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");
83
84         return resource;
85     }
86
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);
93             }
94         }
95     }
96     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
97         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
98                 .stream()
99                 .filter(constructor1 -> constructor1.getParameterCount()==0)
100                 .collect(Collectors.toList())
101                 .get(0);
102         constructor.setAccessible(true);
103         return constructor.newInstance();
104     }
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;
113     }
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");
119         return artifactInfo;
120     }
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){
127             e.printStackTrace();
128         }
129         return new String(bFile);
130     }
131 }