Replaced all tabs with spaces in java and pom.xml
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / oof / OofClientTestIT.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Intel Corp. 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.onap.so.client.oof;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
26 import static org.junit.Assert.assertEquals;
27 import org.junit.Test;
28 import org.onap.so.BaseIntegrationTest;
29 import org.onap.so.client.exception.BadResponseException;
30 import org.onap.so.client.oof.beans.ModelInfo;
31 import org.onap.so.client.oof.beans.OofRequest;
32 import org.onap.so.client.oof.beans.OofRequestParameters;
33 import org.onap.so.client.oof.beans.PlacementDemand;
34 import org.onap.so.client.oof.beans.PlacementInfo;
35 import org.onap.so.client.oof.beans.RequestInfo;
36 import org.onap.so.client.oof.beans.ResourceModelInfo;
37 import org.onap.so.client.oof.beans.ServiceInfo;
38 import org.onap.so.client.oof.beans.SubscriberInfo;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44
45 public class OofClientTestIT extends BaseIntegrationTest {
46
47     @Autowired
48     private OofClient client;
49
50     @Test
51     public void testPostDemands_success() throws BadResponseException, JsonProcessingException {
52         String mockResponse =
53                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"status\", \"requestStatus\": \"accepted\"}";
54
55         ModelInfo modelInfo = new ModelInfo();
56         modelInfo.setModelCustomizationName("modelCustomizationName-Service");
57         modelInfo.setModelInvariantId("modelInvariantId-Service");
58         modelInfo.setModelName("modelName-Service");
59         modelInfo.setModelType("modelType-Service");
60         modelInfo.setModelVersion("modelVersion-Service");
61         modelInfo.setModelVersionId("modelVersionId-Service");
62
63         ServiceInfo serviceInfo = new ServiceInfo();
64         serviceInfo.setModelInfo(modelInfo);
65         serviceInfo.setServiceInstanceId("serviceInstanceId");
66         serviceInfo.setServiceName("serviceName");
67
68         SubscriberInfo subscriberInfo = new SubscriberInfo();
69         subscriberInfo.setGlobalSubscriberId("globalSubscriberId");
70         subscriberInfo.setSubscriberCommonSiteId("subscriberCommonSiteId");
71         subscriberInfo.setSubscriberName("subscriberName");
72
73         ResourceModelInfo resourceModelInfo = new ResourceModelInfo();
74         resourceModelInfo.setModelType("modelType");
75         resourceModelInfo.setModelCustomizationName("modelCustomizationName");
76         resourceModelInfo.setModelInvariantId("invarianteId");
77         resourceModelInfo.setModelName("modelName");
78         resourceModelInfo.setModelVersion("version");
79         resourceModelInfo.setModelVersionId("versionId");
80
81         PlacementDemand placementDemand = new PlacementDemand();
82         placementDemand.setResourceModelInfo(resourceModelInfo);
83         placementDemand.setResourceModuleName("resourceModuleName");
84         placementDemand.setServiceResourceId("serviceResourceId");
85         placementDemand.setTenantId("tenantId");
86
87         OofRequestParameters oofRequestParameters = new OofRequestParameters();
88         oofRequestParameters.setCustomerLatitude("customerLatitude");
89         oofRequestParameters.setCustomerLongitude("customerLongitude");
90         oofRequestParameters.setCustomerName("customerName");
91
92         ArrayList<PlacementDemand> placementDemands = new ArrayList<>();
93         placementDemands.add(placementDemand);
94
95         PlacementInfo placementInfo = new PlacementInfo();
96         placementInfo.setPlacementDemands(placementDemands);
97         placementInfo.setRequestParameters(oofRequestParameters);
98         placementInfo.setSubscriberInfo(subscriberInfo);
99
100         RequestInfo requestInfo = new RequestInfo();
101         requestInfo.setTransactionId("transactionId");
102         List<String> optimizer = new ArrayList<>();
103         optimizer.add("optimizer1");
104         optimizer.add("optimizer2");
105         requestInfo.setOptimizers(optimizer);
106         requestInfo.setCallbackUrl("callBackUrl");
107         requestInfo.setNumSolutions(1);
108         requestInfo.setRequestId("requestId");
109         requestInfo.setSourceId("sourceId");
110         requestInfo.setTimeout(30L);
111         requestInfo.setRequestType("requestType");
112
113         OofRequest oofRequest = new OofRequest();
114         oofRequest.setRequestInformation(requestInfo);
115         oofRequest.setPlacementInformation(placementInfo);
116         oofRequest.setServiceInformation(serviceInfo);
117         oofRequest.setLicenseInformation("");
118
119         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
120                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
121
122         client.postDemands(oofRequest);
123
124         String oofRequestOutput = oofRequest.toJsonString();
125         assertEquals("{\n" + "  \"requestInfo\" : {\n" + "    \"transactionId\" : \"transactionId\",\n"
126                 + "    \"requestId\" : \"requestId\",\n" + "    \"callbackUrl\" : \"callBackUrl\",\n"
127                 + "    \"sourceId\" : \"sourceId\",\n" + "    \"requestType\" : \"requestType\",\n"
128                 + "    \"numSolutions\" : 1,\n" + "    \"optimizers\" : [ \"optimizer1\", \"optimizer2\" ],\n"
129                 + "    \"timeout\" : 30\n" + "  },\n" + "  \"serviceInfo\" : {\n"
130                 + "    \"serviceInstanceId\" : \"serviceInstanceId\",\n" + "    \"serviceName\" : \"serviceName\",\n"
131                 + "    \"modelInfo\" : {\n" + "      \"modelType\" : \"modelType-Service\",\n"
132                 + "      \"modelInvariantId\" : \"modelInvariantId-Service\",\n"
133                 + "      \"modelVersionId\" : \"modelVersionId-Service\",\n"
134                 + "      \"modelName\" : \"modelName-Service\",\n"
135                 + "      \"modelVersion\" : \"modelVersion-Service\",\n"
136                 + "      \"modelCustomizationName\" : \"modelCustomizationName-Service\"\n" + "    }\n" + "  },\n"
137                 + "  \"placementInfo\" : {\n" + "    \"requestParameters\" : {\n"
138                 + "      \"customerLatitude\" : \"customerLatitude\",\n"
139                 + "      \"customerLongitude\" : \"customerLongitude\",\n"
140                 + "      \"customerName\" : \"customerName\"\n" + "    },\n" + "    \"subscriberInfo\" : {\n"
141                 + "      \"globalSubscriberId\" : \"globalSubscriberId\",\n"
142                 + "      \"subscriberName\" : \"subscriberName\",\n"
143                 + "      \"subscriberCommonSiteId\" : \"subscriberCommonSiteId\"\n" + "    },\n"
144                 + "    \"placementDemands\" : [ {\n" + "      \"resourceModuleName\" : \"resourceModuleName\",\n"
145                 + "      \"serviceResourceId\" : \"serviceResourceId\",\n" + "      \"tenantId\" : \"tenantId\",\n"
146                 + "      \"resourceModelInfo\" : {\n" + "        \"modelType\" : \"modelType\",\n"
147                 + "        \"modelInvariantId\" : \"invarianteId\",\n" + "        \"modelVersionId\" : \"versionId\",\n"
148                 + "        \"modelName\" : \"modelName\",\n" + "        \"modelVersion\" : \"version\",\n"
149                 + "        \"modelCustomizationName\" : \"modelCustomizationName\"\n" + "      }\n" + "    } ]\n"
150                 + "  },\n" + "  \"licenseInfo\" : \"\"\n" + "}", oofRequestOutput);
151     }
152
153     @Test
154     public void testAsyncResponse_success() throws BadResponseException, JsonProcessingException {
155         String mockResponse =
156                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"status\", \"requestStatus\": \"accepted\"}";
157
158         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
159                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
160
161         client.postDemands(new OofRequest());
162     }
163
164     @Test(expected = BadResponseException.class)
165     public void testPostDemands_error_failed() throws JsonProcessingException, BadResponseException {
166         String mockResponse =
167                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"missing data\", \"requestStatus\": \"failed\"}";
168
169         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
170                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
171
172
173         client.postDemands(new OofRequest());
174
175         // TODO assertEquals("missing data", );
176
177     }
178
179     @Test(expected = BadResponseException.class)
180     public void testPostDemands_error_noMessage() throws JsonProcessingException, BadResponseException {
181         String mockResponse =
182                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"\", \"requestStatus\": \"failed\"}";
183
184         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
185                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
186
187
188         client.postDemands(new OofRequest());
189
190     }
191
192     @Test(expected = BadResponseException.class)
193     public void testPostDemands_error_noStatus() throws JsonProcessingException, BadResponseException {
194         String mockResponse =
195                 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"missing data\", \"requestStatus\": null}";
196
197         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
198                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
199
200
201         client.postDemands(new OofRequest());
202
203     }
204
205     @Test(expected = BadResponseException.class)
206     public void testPostDemands_error_empty() throws JsonProcessingException, BadResponseException {
207         String mockResponse = "{ }";
208
209         wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
210                 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
211
212
213         client.postDemands(new OofRequest());
214     }
215
216 }