cf041afd6c4ac4a7ac10ec012c2cd7761228b98e
[so.git] / packages / arquillian-unit-tests / src / test / java / org / openecomp / mso / global_tests / asdc / notif_emulator / JsonNotificationData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.global_tests.asdc.notif_emulator;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.codehaus.jackson.annotate.JsonAnySetter;
30 import org.codehaus.jackson.annotate.JsonIgnore;
31 import org.codehaus.jackson.annotate.JsonProperty;
32 import org.codehaus.jackson.map.ObjectMapper;
33 import org.codehaus.jackson.map.annotate.JsonDeserialize;
34 import org.jboss.shrinkwrap.api.exporter.FileExistsException;
35
36 import org.openecomp.sdc.api.notification.IArtifactInfo;
37 import org.openecomp.sdc.api.notification.INotificationData;
38 import org.openecomp.sdc.api.notification.IResourceInstance;
39
40
41 public class JsonNotificationData implements INotificationData {
42
43         @JsonIgnore
44         private Map<String,Object> attributesMap = new HashMap<>();
45         
46         @JsonProperty("serviceArtifacts")
47         @JsonDeserialize(using=JsonArtifactInfoDeserializer.class)
48         private List<IArtifactInfo> serviceArtifacts;
49         
50         @JsonProperty("resources")
51         @JsonDeserialize(using=JsonResourceInfoDeserializer.class)
52         private List<IResourceInstance> resourcesList;
53         
54         public JsonNotificationData() {
55                 
56         }
57                 
58         /**
59          * Method instantiate a INotificationData implementation from a JSON file.
60          * 
61          * @param notifFilePath The file path in String
62          * @return A JsonNotificationData instance
63          * @throws IOException in case of the file is not readable or not accessible 
64          */
65         public static JsonNotificationData instantiateNotifFromJsonFile(String notifFilePath) throws IOException {
66                 
67                 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(notifFilePath+"/notif-structure.json");
68                 
69                 if (is == null) {
70                         throw new FileExistsException("Resource Path does not exist: "+notifFilePath);
71                 }
72                 ObjectMapper mapper = new ObjectMapper();
73                 return mapper.readValue(is, JsonNotificationData.class);
74         }
75         
76         @SuppressWarnings("unused")
77         @JsonAnySetter
78         public final void setAttribute(String attrName, Object attrValue) {
79                 if ((null != attrName) && (!attrName.isEmpty()) && (null != attrValue) && (null != attrValue.toString())) {
80                         this.attributesMap.put(attrName,attrValue);
81                 }
82         }
83
84         @Override
85         public IArtifactInfo getArtifactMetadataByUUID(String arg0) {
86                 return null;
87         }
88
89         @Override
90         public String getDistributionID() {
91                 return (String)this.attributesMap.get("distributionID");
92         }
93
94         @Override
95         public List<IResourceInstance> getResources() {
96                 return resourcesList;
97         }
98
99         @Override
100         public List<IArtifactInfo> getServiceArtifacts() {
101                 return this.serviceArtifacts;
102         }
103
104         @Override
105         public String getServiceDescription() {
106                 return (String)this.attributesMap.get("serviceDescription");
107         }
108
109         @Override
110         public String getServiceInvariantUUID() {
111                 return (String)this.attributesMap.get("serviceInvariantUUID");
112         }
113
114         @Override
115         public String getServiceName() {
116                 return (String)this.attributesMap.get("serviceName");
117         }
118
119         @Override
120         public String getServiceUUID() {
121                 return (String)this.attributesMap.get("serviceUUID");
122         }
123
124         @Override
125         public String getServiceVersion() {
126                 return (String)this.attributesMap.get("serviceVersion");
127         }
128 }