diff options
author | Michal Kabaj <michal.kabaj@nokia.com> | 2018-03-07 15:08:52 +0100 |
---|---|---|
committer | Takamune Cho <tc012c@att.com> | 2018-03-09 20:40:43 +0000 |
commit | c6f9a1aab9e6ba545d8148d4f7456235be1e67d8 (patch) | |
tree | 777e0cba8d87df922d047caaa51dfbabd0718dd3 /appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main | |
parent | 072d9eb28f13b50058cf9a50207dce435ab88369 (diff) |
ChefAdapterImpl JUnits
-Added unit tests for chefXXX methods - extracted new test class
-Moved privateKeyCheck method to new class PrivateKeyChecker + JUnits
-New Factory class ChefAdapterFactory for ChefAdapterImpl construction
-Fixed potential bug with wrong String being assigned in iniSvcLogic method
Change-Id: I27badfd01bfaa807b8ecb9b4a4c13e7f026e34af
Issue-ID: APPC-437
Signed-off-by: Michal Kabaj <michal.kabaj@nokia.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main')
5 files changed, 87 insertions, 29 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/ChefActivator.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/ChefActivator.java index a2eadfa63..d96716e9b 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/ChefActivator.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/ChefActivator.java @@ -27,7 +27,7 @@ package org.onap.appc.adapter.chef; import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import org.onap.appc.Constants; -import org.onap.appc.adapter.chef.impl.ChefAdapterImpl; +import org.onap.appc.adapter.chef.impl.ChefAdapterFactory; import org.onap.appc.configuration.Configuration; import org.onap.appc.configuration.ConfigurationFactory; import org.onap.appc.i18n.Msg; @@ -74,6 +74,7 @@ public class ChefActivator implements BundleActivator { */ private Configuration configuration = ConfigurationFactory.getConfiguration(); + /** * Called when this bundle is started so the Framework can perform the bundle-specific activities necessary to start * this bundle. This method can be used to register services or to allocate any resources that this bundle needs. @@ -95,7 +96,7 @@ public class ChefActivator implements BundleActivator { String appName = readApplicationNameFromProperties(); logger.info(Msg.COMPONENT_INITIALIZING, appName, ADAPTER_NAME); - adapter = new ChefAdapterImpl(); + adapter = new ChefAdapterFactory().create(); if (registration == null) { logger.info(Msg.REGISTERING_SERVICE, appName, ADAPTER_NAME, ChefAdapter.class.getSimpleName()); 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 a69ccf1b2..818d51694 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 @@ -25,7 +25,7 @@ import org.onap.appc.adapter.chef.chefclient.api.ChefApiClient; import org.onap.appc.adapter.chef.chefclient.impl.ChefApiClientImpl; import org.onap.appc.adapter.chef.chefclient.impl.ChefApiHeaderFactory; -public final class ChefApiClientFactory { +public class ChefApiClientFactory { private HttpClient httpClient = HttpClients.createDefault(); private ChefApiHeaderFactory chefApiHeaderFactory = new ChefApiHeaderFactory(); diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterFactory.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterFactory.java new file mode 100644 index 000000000..60c06bad4 --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterFactory.java @@ -0,0 +1,30 @@ +/* + * ============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.impl; + +import org.onap.appc.adapter.chef.ChefAdapter; +import org.onap.appc.adapter.chef.chefclient.ChefApiClientFactory; + +public class ChefAdapterFactory { + + public ChefAdapter create() { + return new ChefAdapterImpl(new ChefApiClientFactory(), new PrivateKeyChecker()); + } +} diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java index 4a4081e5c..0f4b8c8b3 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/ChefAdapterImpl.java @@ -23,11 +23,9 @@ */ package org.onap.appc.adapter.chef.impl; -import java.io.File; import java.util.Arrays; import java.util.List; import java.util.Map; -import java.util.Properties; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -109,12 +107,12 @@ public class ChefAdapterImpl implements ChefAdapter { private static final String CHEF_SERVER_RESULT_MSG_STR = "chefServerResult.message"; private static final String CHEF_ACTION_STR = "chefAction"; private static final String NODE_LIST_STR = "NodeList"; - private ChefApiClientFactory chefApiClientFactory = new ChefApiClientFactory(); + private final ChefApiClientFactory chefApiClientFactory; + private final PrivateKeyChecker privateKeyChecker; - /** - * This default constructor is used as a work around because the activator wasnt getting called - */ - public ChefAdapterImpl() { + ChefAdapterImpl(ChefApiClientFactory chefApiClientFactory, PrivateKeyChecker privateKeyChecker) { + this.chefApiClientFactory = chefApiClientFactory; + this.privateKeyChecker = privateKeyChecker; logger.info("Initialize Chef Adapter"); } @@ -136,7 +134,7 @@ public class ChefAdapterImpl implements ChefAdapter { JSONObject envJ = new JSONObject(env); String envName = envJ.getString("name"); String message; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { // update the details of an environment on the Chef server. ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); ChefResponse chefResponse = chefApiClient.put("/environments/" + envName, env); @@ -187,7 +185,7 @@ public class ChefAdapterImpl implements ChefAdapter { rc.isAlive(); code = 200; String message = null; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefApiClient cac = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); for (String nodeName: nodes) { @@ -295,7 +293,7 @@ public class ChefAdapterImpl implements ChefAdapter { for (String node : nodes) { String chefAction = "/nodes/" + node; String message; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefResponse chefResponse = getApiMethod(chefAction); code = chefResponse.getStatusCode(); message = chefResponse.getBody(); @@ -396,17 +394,6 @@ public class ChefAdapterImpl implements ChefAdapter { } } - private Boolean privateKeyCheck() { - File f = new File(clientPrivatekey); - if (f.exists()) { - logger.info("Key exists"); - return true; - } else { - logger.info("Key doesn't exists"); - return false; - } - } - @SuppressWarnings("nls") @Override public void retrieveData(Map<String, String> params, SvcLogicContext ctx) { @@ -458,7 +445,7 @@ public class ChefAdapterImpl implements ChefAdapter { int code; String message; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefResponse chefResponse = getApiMethod(chefAction); code = chefResponse.getStatusCode(); message = chefResponse.getBody(); @@ -483,7 +470,7 @@ public class ChefAdapterImpl implements ChefAdapter { rc.isAlive(); int code; String message; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); ChefResponse chefResponse = chefApiClient.put(chefAction, chefNodeStr); @@ -513,7 +500,7 @@ public class ChefAdapterImpl implements ChefAdapter { int code; String message; // should load pem from somewhere else - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); // need pass path into it @@ -541,7 +528,7 @@ public class ChefAdapterImpl implements ChefAdapter { rc.isAlive(); int code; String message; - if (privateKeyCheck()) { + if (privateKeyChecker.doesExist(clientPrivatekey)) { ChefApiClient chefApiClient = chefApiClientFactory.create(chefserver, organizations, username, clientPrivatekey); ChefResponse chefResponse = chefApiClient.delete(chefAction); code = chefResponse.getStatusCode(); @@ -718,7 +705,7 @@ public class ChefAdapterImpl implements ChefAdapter { SvcLogicContext svcLogic = rc.getSvcLogicContext(); String codeStr = "server".equals(target) ? CHEF_SERVER_RESULT_CODE_STR : CHEF_CLIENT_RESULT_CODE_STR; - String messageStr = "client".equals(target) ? CHEF_SERVER_RESULT_MSG_STR : CHEF_CLIENT_RESULT_MSG_STR; + String messageStr = "client".equals(target) ? CHEF_CLIENT_RESULT_MSG_STR : CHEF_SERVER_RESULT_MSG_STR; svcLogic.setStatus(OUTCOME_SUCCESS); svcLogic.setAttribute(codeStr, Integer.toString(code)); diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/PrivateKeyChecker.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/PrivateKeyChecker.java new file mode 100644 index 000000000..e6c35cb29 --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/onap/appc/adapter/chef/impl/PrivateKeyChecker.java @@ -0,0 +1,40 @@ +/* + * ============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.impl; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import java.io.File; + +class PrivateKeyChecker { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PrivateKeyChecker.class); + + Boolean doesExist(String clientPrivateKeyPath) { + File f = new File(clientPrivateKeyPath); + if (f.exists()) { + logger.info("Key exists"); + return true; + } else { + logger.info("Key doesn't exists"); + return false; + } + } +}
\ No newline at end of file |