Added new tests for SdcRestClientUtils
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / utils / SdcRestClientUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (c) 2019 Samsung
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.sdc.dcae.utils;
23
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26
27 import org.onap.sdc.dcae.composition.restmodels.sdc.Artifact;
28 import org.onap.sdc.dcae.enums.ArtifactGroupType;
29 import org.onap.sdc.dcae.enums.SdcConsumerInfo;
30 import org.springframework.util.Base64Utils;
31 import org.springframework.util.StringUtils;
32
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.util.ArrayList;
36 import java.util.EnumMap;
37 import java.util.List;
38
39 public class SdcRestClientUtils {
40
41     private static final String SDC_CATALOG_PATH = "/sdc/v1/catalog/";
42
43     private SdcRestClientUtils() {
44     }
45
46     // TODO consider moving params elsewhere (user/password/instanceId can be constant)
47     public static EnumMap<SdcConsumerInfo, String> extractConsumerInfoFromUri(URI configUri) {
48         EnumMap<SdcConsumerInfo, String> userInfoMap = new EnumMap<>(SdcConsumerInfo.class);
49         String userInfo = configUri.getUserInfo();
50         if (userInfo != null) {
51             userInfoMap.put(SdcConsumerInfo.AUTH, "Basic "+ Base64Utils.encodeToString(userInfo.getBytes()));
52         }
53         String fragment = configUri.getFragment();
54         if (fragment == null)
55             throw new IllegalArgumentException("The URI must contain a fragment specification, to be used as SDC instance id");
56         userInfoMap.put(SdcConsumerInfo.INSTANCE_ID, fragment);
57         try {
58             userInfoMap.put(SdcConsumerInfo.CATALOG_URL, new URI(configUri.getScheme(), null, configUri.getHost(), configUri.getPort(), configUri.getPath()+SDC_CATALOG_PATH, null, null).toString());
59         }
60         catch (URISyntaxException se) {
61             throw new IllegalArgumentException("Invalid uri", se);
62         }
63         return userInfoMap;
64     }
65
66     public static String buildResourceFilterQuery(String resourceType, String category, String subcategory) {
67         List<String> filters = new ArrayList<>();
68         if(!StringUtils.isEmpty(resourceType))
69             filters.add("resourceType="+resourceType);
70         if(!StringUtils.isEmpty(category))
71             filters.add("category="+category);
72         if(!StringUtils.isEmpty(subcategory))
73             filters.add("subCategory="+subcategory);
74         return "?"+ String.join("&", filters);
75     }
76
77     public static UserRemarks buildUserRemarksObject(String userRemarks) {
78         return new UserRemarks(userRemarks);
79     }
80
81     private static class UserRemarks {
82         private String userRemarks;
83
84         private UserRemarks(String userRemarks) {
85             this.userRemarks = userRemarks;
86         }
87
88         public String getUserRemarks() {
89             return userRemarks;
90         }
91     }
92
93     public static String artifactToString(Artifact artifact) throws JsonProcessingException {
94         ObjectMapper mapper = new ObjectMapper();
95         return mapper.writeValueAsString(artifact);
96     }
97
98     public static Artifact generateDeploymentArtifact(String description, String name, String type, String label, byte[] payload) {
99         Artifact artifact = new Artifact();
100         artifact.setDescription(description);
101         artifact.setArtifactName(name);
102         artifact.setArtifactGroupType(ArtifactGroupType.DEPLOYMENT.name());
103         artifact.setArtifactType(type);
104         artifact.setArtifactLabel(label);
105         artifact.setPayloadData(Base64Utils.encodeToString(payload));
106         return artifact;
107     }
108
109         public static Artifact generateCatalogDcaeToscaArtifact(String name, String path, byte[] payload) {
110                 Artifact artifact = new Artifact();
111                 artifact.setArtifactName(name);
112                 artifact.setArtifactURL(path);
113                 artifact.setPayloadData(new String(payload));
114                 return artifact;
115         }
116 }