private final Config prhAppConfig;
private final HttpRequestDetails requestDetails;
private AAIProducerClient producerClient;
- public Optional<String> response;
+ Optional<String> response;
@Autowired
public AAIConsumerTaskImpl(AppConfig prhAppConfig, HttpRequestDetails requestDetails) {
private final Config prhAppConfig;
private AAIProducerClient producerClient;
private HttpRequestDetails requestDetails;
- private String jsonBody = "{\"ipaddress-v4-oam\":\"11.22.33.155\"}";
- private String pnfName = "example-pnf-name-val-40510"; // pnf name received from dmaap required for URI
- public Optional<String> response;
+ Optional<String> response;
@Autowired
public AAIProducerTaskImpl(AppConfig prhAppConfig, HttpRequestDetails requestDetails) {
*/
package org.onap.dcaegen2.services.prh.tasks;
-import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
-import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration;
import org.onap.dcaegen2.services.prh.exceptions.PrhTaskException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
/**
* @author <a href="mailto:przemyslaw.wasala@nokia.com">Przemysław Wąsala</a> on 3/23/18
*/
+++ /dev/null
-/*-
- * ============LICENSE_START=======================================================
- * PNF-REGISTRATION-HANDLER
- * ================================================================================
- * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.onap.dcaegen2.services.service;
-
-import org.apache.http.HttpEntity;
-import org.apache.http.client.ResponseHandler;
-import org.apache.http.util.EntityUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Optional;
-
-public class CommonMethods {
-
- private static Logger logger = LoggerFactory.getLogger(CommonMethods.class);
-
- private CommonMethods() {}
-
- public static ResponseHandler<Optional<String>> dmaapResponseHandler() {
- return httpResponse -> {
- final int responseCode = httpResponse.getStatusLine().getStatusCode();
- final HttpEntity responseEntity = httpResponse.getEntity();
-
- if (HttpUtils.isSuccessfulResponseCode(responseCode) && responseEntity != null) {
- logger.info("HTTP response successful.");
- final String response = EntityUtils.toString(responseEntity);
- return Optional.of(response);
- } else {
- String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
- logger.error("HTTP response not successful : {}", response);
- return Optional.empty();
- }
- };
- }
-}
package org.onap.dcaegen2.services.service.consumer;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
import org.onap.dcaegen2.services.config.DmaapConsumerConfiguration;
-import org.onap.dcaegen2.services.service.CommonMethods;
import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
+import org.onap.dcaegen2.services.service.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Optional<HttpRequestBase> request = createRequest();
try {
- extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler());
+ extendedDetails = closeableHttpClient.execute(request.get(), dmaapConsumerResponseHandler());
} catch (IOException | NullPointerException e) {
logger.error("Exception while executing HTTP request: {}", e);
}
return extendedURI;
}
+
+ private ResponseHandler<Optional<String>> dmaapConsumerResponseHandler() {
+ return httpResponse -> {
+ final int responseCode = httpResponse.getStatusLine().getStatusCode();
+ logger.info("Status code of operation: {}", responseCode);
+ final HttpEntity responseEntity = httpResponse.getEntity();
+
+ if (HttpUtils.isSuccessfulResponseCode(responseCode) ) {
+ logger.info("HTTP response successful.");
+ final String dmaapResponse = EntityUtils.toString(responseEntity);
+ return Optional.of(dmaapResponse);
+ } else {
+ String dmaapResponse = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
+ logger.error("HTTP response not successful : {}", dmaapResponse);
+ return Optional.of("" + responseCode);
+ }
+ };
+ }
}
package org.onap.dcaegen2.services.service.producer;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
import org.onap.dcaegen2.services.config.DmaapPublisherConfiguration;
-import org.onap.dcaegen2.services.service.CommonMethods;
import org.onap.dcaegen2.services.service.DmaapHttpClientImpl;
+import org.onap.dcaegen2.services.service.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Optional<HttpRequestBase> request = createRequest(requestDetails);
try {
- extendedDetails = closeableHttpClient.execute(request.get(), CommonMethods.dmaapResponseHandler());
+ extendedDetails = closeableHttpClient.execute(request.get(), dmaapProducerResponseHandler());
} catch (IOException | NullPointerException e) {
logger.error("Exception while executing HTTP request: {}", e);
}
post.setEntity(stringEntity.get());
return post;
}
+
+ private ResponseHandler<Optional<String>> dmaapProducerResponseHandler() {
+ return httpResponse -> {
+ final int responseCode = httpResponse.getStatusLine().getStatusCode();
+ final HttpEntity responseEntity = httpResponse.getEntity();
+
+ if (HttpUtils.isSuccessfulResponseCode(responseCode)) {
+ logger.info("HTTP response successful.");
+ return Optional.of("" + responseCode);
+ } else {
+ String response = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
+ logger.error("HTTP response not successful : {}", response);
+ return Optional.of("" + responseCode);
+ }
+ };
+ }
}
private static Optional<String> expectedResult = Optional.empty();
private static final String JSON_MESSAGE = "{ \"ipaddress-v4-oam\": \"11.22.33.44\" }";
+ private static final String RESPONSE_SUCCESS = "200";
+ private static final String RESPONSE_FAILURE = "404";
@BeforeAll
public static void init() throws NoSuchFieldException, IllegalAccessException {
@Test
public void getHttpResponsePost_success() throws IOException {
- expectedResult = Optional.of(JSON_MESSAGE);
+ expectedResult = Optional.of(RESPONSE_SUCCESS);
when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class)))
.thenReturn(expectedResult);
}
@Test
- public void getExtendedDetails_returnsNull() throws IOException {
- expectedResult = Optional.of(JSON_MESSAGE);
+ public void getExtendedDetails_returnsFailure() throws IOException {
+ expectedResult = Optional.of(RESPONSE_FAILURE);
when(closeableHttpClientMock.execute(any(HttpPost.class), any(ResponseHandler.class))).
thenReturn(Optional.empty());
Optional<String> actualResult = objectUnderTest.getHttpProducerResponse(requestDetailsMock);