Changed to unmaintained
[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-2018 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  * ============LICENSE_END=========================================================
21  */
22 package org.onap.appc.sdc.listener;
23
24 import org.junit.Assert;
25 import org.junit.Test;
26 import org.onap.sdc.api.notification.IArtifactInfo;
27 import org.onap.sdc.api.notification.INotificationData;
28 import org.onap.sdc.api.notification.IResourceInstance;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.lang.reflect.Constructor;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.net.URISyntaxException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.stream.Collectors;
40
41 public class SdcTestUtils {
42
43     @Test
44     public void testToSdcStoreDocumentInput() throws Exception{
45         Assert.assertEquals(readInput("/output/TestUtilResponse.json"), Util.toSdcStoreDocumentInput
46                 (getNotificationData(),getResources(),getServiceArtifact(),"Mock data"));
47     }
48
49     @Test
50     public void testProviderResponse(){
51         ProviderResponse response=new ProviderResponse(200,"Success");
52         Assert.assertEquals(200,response.getStatus());
53         Assert.assertEquals("Success",response.getBody());
54     }
55
56     private INotificationData getNotificationData() throws ClassNotFoundException, IllegalAccessException,
57             InstantiationException, InvocationTargetException {
58
59         INotificationData notificationData = (INotificationData)getObject("org.onap.sdc.impl.NotificationDataImpl");
60
61         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
62
63         invokeMethod(notificationData, "setServiceArtifacts", serviceArtifacts);
64         invokeMethod(notificationData,"setServiceUUID","4564-4567-7897");
65         invokeMethod(notificationData,"setDistributionID","Distribution ID Mock");
66         invokeMethod(notificationData,"setServiceDescription","Service Description Mock");
67         invokeMethod(notificationData,"setServiceName","Service Name Mock");
68
69         return notificationData;
70     }
71
72     private IResourceInstance getResources() throws ClassNotFoundException, InvocationTargetException,
73             InstantiationException, IllegalAccessException {
74         IResourceInstance resource = (IResourceInstance)getObject("org.onap.sdc.impl.JsonContainerResourceInstance");
75
76         List<IArtifactInfo> serviceArtifacts = getServiceArtifacts();
77         invokeMethod(resource,"setArtifacts",serviceArtifacts);
78         invokeMethod(resource,"setResourceName","Vnf");
79         invokeMethod(resource,"setResourceVersion","1.0");
80         invokeMethod(resource,"setResourceUUID","Resource UUID");
81
82         return resource;
83     }
84
85     private void invokeMethod(Object object, String methodName,Object... arguments) throws IllegalAccessException, InvocationTargetException {
86         Method[] methods = object.getClass().getDeclaredMethods();
87         for(Method method:methods){
88             if(methodName.equalsIgnoreCase(method.getName())){
89                 method.setAccessible(true);
90                 method.invoke(object,arguments);
91             }
92         }
93     }
94     private Object getObject(String fqcn) throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
95         Constructor constructor = Arrays.asList(Class.forName(fqcn).getDeclaredConstructors())
96                 .stream()
97                 .filter(constructor1 -> constructor1.getParameterCount()==0)
98                 .collect(Collectors.toList())
99                 .get(0);
100         constructor.setAccessible(true);
101         return constructor.newInstance();
102     }
103     private List<IArtifactInfo> getServiceArtifacts() throws ClassNotFoundException, InvocationTargetException,
104             InstantiationException, IllegalAccessException {
105         List<IArtifactInfo> serviceArtifacts = new ArrayList<>();
106         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.onap.sdc.impl.ArtifactInfoImpl");
107         invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
108         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
109         serviceArtifacts.add(artifactInfo);
110         return serviceArtifacts;
111     }
112     private IArtifactInfo getServiceArtifact() throws ClassNotFoundException, InvocationTargetException,
113             InstantiationException, IllegalAccessException {
114         IArtifactInfo artifactInfo = (IArtifactInfo)getObject("org.onap.sdc.impl.ArtifactInfoImpl");
115         invokeMethod(artifactInfo,"setArtifactType","TOSCA_CSAR");
116         invokeMethod(artifactInfo,"setArtifactUUID","abcd-efgh-ijkl");
117         return artifactInfo;
118     }
119     private String readInput(String inputFile) throws URISyntaxException {
120         File file = new File(this.getClass().getResource(inputFile).toURI());
121         byte[] bFile = new byte[(int) file.length()];
122         try(FileInputStream fileInputStream = new FileInputStream(file)){
123             fileInputStream.read(bFile);
124         } catch (Exception e){
125             e.printStackTrace();
126         }
127         return new String(bFile);
128     }
129 }