Decouple ChefApiClientImpl from header creation 37/35237/2
authorMichal Kabaj <michal.kabaj@nokia.com>
Mon, 12 Mar 2018 14:04:50 +0000 (15:04 +0100)
committerTakamune Cho <tc012c@att.com>
Mon, 12 Mar 2018 18:33:57 +0000 (18:33 +0000)
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 <michal.kabaj@nokia.com>
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/ChefApiClientFactory.java
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImpl.java
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/chefclient/impl/HttpHeaderFactory.java [new file with mode: 0644]
appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/onap/appc/adapter/chef/chefclient/impl/ChefApiClientImplTest.java

index 818d516..1815266 100644 (file)
@@ -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));
     }
 }
index b26f69a..8edc2b5 100644 (file)
@@ -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 (file)
index 0000000..4a9d6c8
--- /dev/null
@@ -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<String, String> create(String methodName, String requestPath, String body);
+}
index 21e607d..ed39efb 100644 (file)
@@ -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<String, String> HEADERS = ImmutableMap.<String, String>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