2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.oof;
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;
45 public class OofClientTestIT extends BaseIntegrationTest {
48 private OofClient client;
51 public void testPostDemands_success() throws BadResponseException, JsonProcessingException {
53 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"status\", \"requestStatus\": \"accepted\"}";
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");
63 ServiceInfo serviceInfo = new ServiceInfo();
64 serviceInfo.setModelInfo(modelInfo);
65 serviceInfo.setServiceInstanceId("serviceInstanceId");
66 serviceInfo.setServiceName("serviceName");
68 SubscriberInfo subscriberInfo = new SubscriberInfo();
69 subscriberInfo.setGlobalSubscriberId("globalSubscriberId");
70 subscriberInfo.setSubscriberCommonSiteId("subscriberCommonSiteId");
71 subscriberInfo.setSubscriberName("subscriberName");
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");
81 PlacementDemand placementDemand = new PlacementDemand();
82 placementDemand.setResourceModelInfo(resourceModelInfo);
83 placementDemand.setResourceModuleName("resourceModuleName");
84 placementDemand.setServiceResourceId("serviceResourceId");
85 placementDemand.setTenantId("tenantId");
87 OofRequestParameters oofRequestParameters = new OofRequestParameters();
88 oofRequestParameters.setCustomerLatitude("customerLatitude");
89 oofRequestParameters.setCustomerLongitude("customerLongitude");
90 oofRequestParameters.setCustomerName("customerName");
92 ArrayList<PlacementDemand> placementDemands = new ArrayList<>();
93 placementDemands.add(placementDemand);
95 PlacementInfo placementInfo = new PlacementInfo();
96 placementInfo.setPlacementDemands(placementDemands);
97 placementInfo.setRequestParameters(oofRequestParameters);
98 placementInfo.setSubscriberInfo(subscriberInfo);
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");
113 OofRequest oofRequest = new OofRequest();
114 oofRequest.setRequestInformation(requestInfo);
115 oofRequest.setPlacementInformation(placementInfo);
116 oofRequest.setServiceInformation(serviceInfo);
117 oofRequest.setLicenseInformation("");
119 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
120 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
122 client.postDemands(oofRequest);
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);
154 public void testAsyncResponse_success() throws BadResponseException, JsonProcessingException {
155 String mockResponse =
156 "{\"transactionId\": \"123456789\", \"requestId\": \"1234\", \"statusMessage\": \"status\", \"requestStatus\": \"accepted\"}";
158 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
159 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
161 client.postDemands(new OofRequest());
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\"}";
169 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
170 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
173 client.postDemands(new OofRequest());
175 // TODO assertEquals("missing data", );
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\"}";
184 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
185 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
188 client.postDemands(new OofRequest());
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}";
197 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
198 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
201 client.postDemands(new OofRequest());
205 @Test(expected = BadResponseException.class)
206 public void testPostDemands_error_empty() throws JsonProcessingException, BadResponseException {
207 String mockResponse = "{ }";
209 wireMockServer.stubFor(post(urlEqualTo("/api/oof/v1/placement")).willReturn(
210 aResponse().withStatus(200).withHeader("Content-Type", "application/json").withBody(mockResponse)));
213 client.postDemands(new OofRequest());