From f8f6d212382f458e1cb31081dad744e78f92e521 Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Mon, 12 Mar 2018 15:04:50 +0100 Subject: Decouple ChefApiClientImpl from header creation Introduce new FunctionalInterface HttpHeaderFactory to allow customization of http header providers when creating ChefApiClientImpl using ChefApiClientFactory This allows for better encapsulation of header specific values such as: userId,organizations,pemPath. Change-Id: Iec2c7cff8e664a18e15d7e973920152fdb158c98 Issue-ID: APPC-437 Signed-off-by: Michal Kabaj --- .../chef/chefclient/ChefApiClientFactory.java | 9 ++++--- .../chef/chefclient/impl/ChefApiClientImpl.java | 24 +++++++------------ .../chef/chefclient/impl/HttpHeaderFactory.java | 28 ++++++++++++++++++++++ .../chefclient/impl/ChefApiClientImplTest.java | 2 +- 4 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/HttpHeaderFactory.java (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle') diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/ChefApiClientFactory.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/ChefApiClientFactory.java index 818d51694..1815266f3 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/ChefApiClientFactory.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/ChefApiClientFactory.java @@ -31,11 +31,10 @@ public class ChefApiClientFactory { private ChefApiHeaderFactory chefApiHeaderFactory = new ChefApiHeaderFactory(); public ChefApiClient create(String endPoint, String organizations, String userId, String pemPath) { - return new ChefApiClientImpl(httpClient, - chefApiHeaderFactory, + return new ChefApiClientImpl( + httpClient, endPoint, - organizations, - userId, - pemPath); + (methodName, requestPath, body) -> chefApiHeaderFactory + .create(methodName, requestPath, body, userId, organizations, pemPath)); } } diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImpl.java index b26f69a9e..8edc2b566 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImpl.java @@ -38,20 +38,13 @@ import org.onap.appc.adapter.chef.chefclient.impl.ChefRequestBuilder.OngoingRequ public class ChefApiClientImpl implements ChefApiClient { private final HttpClient httpClient; - private final ChefApiHeaderFactory chefApiHeaderFactory; - private String endpoint; - private String userId; - private String pemPath; - private String organizations; + private final String endpoint; + private final HttpHeaderFactory httpHeaderFactory; - public ChefApiClientImpl(HttpClient httpClient, ChefApiHeaderFactory chefApiHeaderFactory, - String endpoint, String organizations, String userId, String pemPath) { + public ChefApiClientImpl(HttpClient httpClient, String endpoint, HttpHeaderFactory httpHeaderFactory) { this.httpClient = httpClient; - this.chefApiHeaderFactory = chefApiHeaderFactory; this.endpoint = endpoint; - this.organizations = organizations; - this.userId = userId; - this.pemPath = pemPath; + this.httpHeaderFactory = httpHeaderFactory; } @Override @@ -59,7 +52,7 @@ public class ChefApiClientImpl implements ChefApiClient { OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint) .httpGet() .withPath(path) - .withHeaders(chefApiHeaderFactory.create("GET", path, "", userId, organizations, pemPath)); + .withHeaders(httpHeaderFactory.create("GET", path, "")); return execute(requestBuilder); } @@ -68,7 +61,7 @@ public class ChefApiClientImpl implements ChefApiClient { OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint) .httpDelete() .withPath(path) - .withHeaders(chefApiHeaderFactory.create("DELETE", path, "", userId, organizations, pemPath)); + .withHeaders(httpHeaderFactory.create("DELETE", path, "")); return execute(requestBuilder); } @@ -77,7 +70,7 @@ public class ChefApiClientImpl implements ChefApiClient { OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint) .httpPost(body) .withPath(path) - .withHeaders(chefApiHeaderFactory.create("POST", path, body, userId, organizations, pemPath)); + .withHeaders(httpHeaderFactory.create("POST", path, body)); return execute(requestBuilder); } @@ -86,7 +79,7 @@ public class ChefApiClientImpl implements ChefApiClient { OngoingRequestBuilder requestBuilder = ChefRequestBuilder.newRequestTo(endpoint) .httpPut(body) .withPath(path) - .withHeaders(chefApiHeaderFactory.create("PUT", path, body, userId, organizations, pemPath)); + .withHeaders(httpHeaderFactory.create("PUT", path, body)); return execute(requestBuilder); } @@ -104,3 +97,4 @@ public class ChefApiClientImpl implements ChefApiClient { } } } + diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/HttpHeaderFactory.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/HttpHeaderFactory.java new file mode 100644 index 000000000..4a9d6c81e --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/HttpHeaderFactory.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2018 Nokia. 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.appc.adapter.chef.chefclient.impl; + +import com.google.common.collect.ImmutableMap; + +@FunctionalInterface +public interface HttpHeaderFactory { + + ImmutableMap create(String methodName, String requestPath, String body); +} diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImplTest.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImplTest.java index 21e607de7..ed39efb1b 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImplTest.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImplTest.java @@ -55,6 +55,7 @@ public class ChefApiClientImplTest { private static final String USER_ID = "testUser"; private static final String REQUEST_PATH = "/test/path"; private static final String BODY = "SOME BODY STRING"; + private static final String PEM_FILEPATH = "path/to/pemFile"; private static final ImmutableMap HEADERS = ImmutableMap.builder() .put("Content-type", "application/json") .put("Accept", "application/json") @@ -71,7 +72,6 @@ public class ChefApiClientImplTest { @InjectMocks private ChefApiClientFactory chefApiClientFactory; - private static final String PEM_FILEPATH = "path/to/pemFile"; private ChefApiClient chefApiClient; @Before -- cgit 1.2.3-korg