AT&T 1712 and 1802 release code
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / openecomp / mso / apihandlerinfra / tenantisolation / helpers / AsdcClientHelperTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.apihandlerinfra.tenantisolation.helpers;\r
22 \r
23 import static org.junit.Assert.assertEquals;\r
24 import static org.junit.Assert.fail;\r
25 \r
26 import java.util.LinkedHashMap;\r
27 import java.util.List;\r
28 \r
29 import org.json.JSONObject;\r
30 import org.junit.After;\r
31 import org.junit.Assert;\r
32 import org.junit.BeforeClass;\r
33 import org.junit.Rule;\r
34 import org.junit.Test;\r
35 import org.openecomp.mso.apihandlerinfra.Constants;\r
36 import org.openecomp.mso.apihandlerinfra.MsoPropertiesUtils;\r
37 import org.openecomp.mso.properties.MsoJavaProperties;\r
38 import org.openecomp.mso.properties.MsoPropertiesFactory;\r
39 import org.openecomp.mso.rest.RESTClient;\r
40 import org.openecomp.mso.rest.RESTConfig;\r
41 \r
42 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;\r
43 import com.github.tomakehurst.wiremock.junit.WireMockRule;\r
44 \r
45 public class AsdcClientHelperTest {\r
46 \r
47         MsoJavaProperties properties = MsoPropertiesUtils.loadMsoProperties();\r
48         AsdcClientHelper asdcClientUtils = new AsdcClientHelper(properties);\r
49         \r
50         String serviceModelVersionId = "TEST_uuid1";\r
51         String operationalEnvironmentId = "TEST_operationalEnvironmentId";\r
52         String workloadContext = "TEST_workloadContext";\r
53 \r
54         @Rule\r
55         public final WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().port(28090)); //.extensions(transformerArray));\r
56         \r
57         @BeforeClass\r
58         public static void setUp() throws Exception {\r
59                 MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();\r
60                 msoPropertiesFactory.removeAllMsoProperties();\r
61                 msoPropertiesFactory.initializeMsoProperties(Constants.MSO_PROP_APIHANDLER_INFRA, "src/test/resources/mso.apihandler-infra.properties");\r
62         }       \r
63         \r
64         @After\r
65         public void tearDown() throws Exception {\r
66                 \r
67         }       \r
68         \r
69         @Test\r
70         public void getPropertiesTest() {\r
71                 \r
72                 String asdcInstanceId = asdcClientUtils.getAsdcInstanceId(); \r
73                 Assert.assertEquals("Asdc InstanceId - " , "test", asdcInstanceId);\r
74                 \r
75                 String asdcEndpoint = asdcClientUtils.getAsdcEndpoint(); \r
76                 Assert.assertEquals("Asdc Endpoint - " , "http://localhost:28090", asdcEndpoint);\r
77 \r
78                 String userid = asdcClientUtils.getAsdcUserId();\r
79                 Assert.assertEquals("userid - " , "cs0008", userid);\r
80                 \r
81         }       \r
82         \r
83         @Test\r
84         public void buildUriBuilderTest() {\r
85                 \r
86                 try {  \r
87                 String url = asdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);\r
88                         assertEquals("http://localhost:28090/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate", url);\r
89                         \r
90                 } catch (Exception e) {\r
91                         fail("Exception caught: " + e.getMessage());\r
92 \r
93                 }       \r
94         }       \r
95 \r
96         @Test\r
97         public void buildJsonWorkloadContextTest() {\r
98                 \r
99                 try {  \r
100                         String jsonPayload = asdcClientUtils.buildJsonWorkloadContext(workloadContext);\r
101                         assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);\r
102                         \r
103                 } catch (Exception e) {\r
104                         fail("Exception caught: " + e.getMessage());\r
105 \r
106                 }       \r
107         }               \r
108         \r
109         @Test\r
110         public void setRestClientTest() {\r
111                 \r
112                 try {\r
113                         String url = asdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);\r
114                         RESTConfig config = new RESTConfig(url);\r
115                         RESTClient client = asdcClientUtils.setRestClient(config);\r
116                         LinkedHashMap<String, List<String>> headers = client.getHeaders();\r
117                         assertEquals("[cs0008]", headers.get("USER_ID").toString());\r
118                         \r
119                 } catch (Exception e) {\r
120                         fail("Exception caught: " + e.getMessage());\r
121 \r
122                 }       \r
123         }\r
124         \r
125         @Test\r
126         public void enhanceJsonResponseTest_Success() {\r
127                 \r
128                 try {\r
129                         // build success response data\r
130                         JSONObject asdcResponseJsonObj = new JSONObject();\r
131                         asdcResponseJsonObj.put("distributionId", "TEST_distributionId");\r
132 \r
133                         int statusCode = 202;\r
134                         asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);\r
135                         \r
136                         assertEquals("202", asdcResponseJsonObj.getString("statusCode"));\r
137                         assertEquals("", asdcResponseJsonObj.getString("messageId"));\r
138                         assertEquals("Success", asdcResponseJsonObj.getString("message"));\r
139                         assertEquals("TEST_distributionId", asdcResponseJsonObj.getString("distributionId"));\r
140                         \r
141                 } catch (Exception e) {\r
142                         fail("Exception caught: " + e.getMessage());\r
143 \r
144                 }       \r
145         }       \r
146         \r
147         @Test\r
148         public void enhanceJsonResponseTest_Error() {\r
149                 \r
150                 try {\r
151                         \r
152                         // build error response data\r
153                         JSONObject jsonMessages = new JSONObject();\r
154                         jsonMessages.put("messageId", "SVC4675");\r
155                         jsonMessages.put("text", "Error: Service state is invalid for this action.");\r
156                         JSONObject jsonServException = new JSONObject();\r
157                         jsonServException.put("serviceException", jsonMessages);\r
158                         JSONObject jsonErrorRequest = new JSONObject();\r
159                         jsonErrorRequest.put("requestError", jsonServException);\r
160 \r
161                         String responseData =  jsonErrorRequest.toString();\r
162                         \r
163                         JSONObject asdcResponseJsonObj = new JSONObject(responseData);\r
164                         int statusCode = 409;\r
165                         asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);\r
166                         \r
167                         assertEquals("409", asdcResponseJsonObj.getString("statusCode"));\r
168                         assertEquals("SVC4675", asdcResponseJsonObj.getString("messageId"));\r
169                         assertEquals("Error: Service state is invalid for this action.", asdcResponseJsonObj.getString("message"));\r
170 \r
171                         \r
172                 } catch (Exception e) {\r
173                         fail("Exception caught: " + e.getMessage());\r
174 \r
175                 }       \r
176         }               \r
177 \r
178         @Test\r
179         public void enhanceJsonResponseTest_Error_policyException() {\r
180                 \r
181                 try {\r
182                         \r
183                         // build error response data\r
184                         JSONObject jsonMessages = new JSONObject();\r
185                         jsonMessages.put("messageId", "POL5003");\r
186                         jsonMessages.put("text", "Error: Not authorized to use the API.");\r
187                         JSONObject jsonServException = new JSONObject();\r
188                         jsonServException.put("policyException", jsonMessages);\r
189                         JSONObject jsonErrorRequest = new JSONObject();\r
190                         jsonErrorRequest.put("requestError", jsonServException);\r
191 \r
192                         String responseData =  jsonErrorRequest.toString();\r
193                         \r
194                         JSONObject asdcResponseJsonObj = new JSONObject(responseData);\r
195                         int statusCode = 403;\r
196                         asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);\r
197                         \r
198                         assertEquals("403", asdcResponseJsonObj.getString("statusCode"));\r
199                         assertEquals("POL5003", asdcResponseJsonObj.getString("messageId"));\r
200                         assertEquals("Error: Not authorized to use the API.", asdcResponseJsonObj.getString("message"));\r
201 \r
202                         \r
203                 } catch (Exception e) {\r
204                         fail("Exception caught: " + e.getMessage());\r
205 \r
206                 }       \r
207         }                       \r
208         \r
209 }\r