diff options
author | Marcus G K Williams <marcus.williams@intel.com> | 2017-09-07 17:16:09 -0700 |
---|---|---|
committer | Skip Wonnell <skip@att.com> | 2017-09-18 03:27:31 +0000 |
commit | c2f55b2e5771f32b84bb07770d15b0d42b8e814b (patch) | |
tree | 1cfbb6c6b04886af91f7d908a68d146477720ed2 /appc-adapters/appc-chef-adapter | |
parent | 6475b40bcbb943aa52beec12fc8b54d671d9e429 (diff) |
Add Unit Tests for appc-chef-adapter
Issue-Id: APPC-181
Change-Id: Iee824ca929ea46944ac1673e709074687ad812f9
Signed-off-by: Marcus G K Williams <marcus.williams@intel.com>
Diffstat (limited to 'appc-adapters/appc-chef-adapter')
7 files changed, 218 insertions, 79 deletions
diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/pom.xml b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/pom.xml index 28930ba2d..bb29f912f 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/pom.xml +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/pom.xml @@ -58,8 +58,14 @@ <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> - <version>4.4.4</version> + <version>4.4.6</version> </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.5.3</version> + </dependency> + <dependency> <groupId>org.bouncycastle</groupId> diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java index 19e381ec5..de4f31a04 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/chefapi/ApiMethod.java @@ -24,25 +24,19 @@ package org.openecomp.appc.adapter.chef.chefapi; -import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; -import org.apache.http.*; -import org.apache.http.client.*; -import org.apache.http.client.methods.*; -import org.apache.http.impl.client.*; +import org.apache.http.HttpResponse; +import org.apache.http.Header; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.util.EntityUtils; import org.openecomp.appc.adapter.chef.chefclient.Utils; -import javax.net.ssl.SSLContext; -import java.io.File; import org.apache.http.HttpEntity; -import org.apache.http.conn.ssl.SSLConnectionSocketFactory; -import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.impl.client.HttpClients; -import org.apache.http.ssl.SSLContexts; public class ApiMethod { private HttpClient client = null; @@ -64,7 +58,7 @@ public class ApiMethod { this.methodName = methodName; } - public ApiMethod execute() { + public ApiMethod createRequest(){ String hashedPath = Utils.sha1AndBase64("/organizations/"+organizations+chefPath); String hashedBody = Utils.sha1AndBase64(reqBody); @@ -101,11 +95,15 @@ public class ApiMethod { * RHS=this.method.getHeaders(); for (int i = 0; i < RHS.length; i++) { * test=test+RHS[i]+"\n"; } test=test+this.reqBody+"\n"; */ + return this; + } + + public ApiMethod execute() { try{ - response = client.execute(method); - resCode = response.getStatusLine().getStatusCode(); - HttpEntity entity1 = response.getEntity(); - responseBody = EntityUtils.toString(entity1);} + response = client.execute(method); + resCode = response.getStatusLine().getStatusCode(); + HttpEntity entity1 = response.getEntity(); + responseBody = EntityUtils.toString(entity1);} catch(Exception ex){ resCode=500; responseBody=ex.getMessage(); diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/impl/ChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/impl/ChefAdapterImpl.java index b81c10bc8..ab853dcc4 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/impl/ChefAdapterImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/main/java/org/openecomp/appc/adapter/chef/impl/ChefAdapterImpl.java @@ -145,7 +145,11 @@ public class ChefAdapterImpl implements ChefAdapter { serverAddress = params.get("org.openecomp.appc.instance.serverAddress"); organizations = params.get("org.openecomp.appc.instance.organizations"); chefserver = "https://" + serverAddress + ORGANIZATIONS_STR + organizations; - clientPrivatekey = "/opt/app/bvc/chef/" + serverAddress + "/" + organizations + "/" + clientName + ".pem"; + if (params.containsKey("org.openecomp.appc.instance.pemPath")) { + clientPrivatekey = params.get("org.openecomp.appc.instance.pemPath"); + } else { + clientPrivatekey = "/opt/app/bvc/chef/" + serverAddress + "/" + organizations + "/" + clientName + ".pem"; + } } public Boolean privateKeyCheck() { @@ -444,7 +448,7 @@ public class ChefAdapterImpl implements ChefAdapter { configuration = ConfigurationFactory.getConfiguration(); // need to fetch data from appc configurator or form some file in the appc vms clientName = "testnode"; - clientPrivatekey = "/etc/chef/client.pem"; + clientPrivatekey = System.getProperty("user.dir") + "/src/test/resources/testclient.pem"; serverAddress = "http://example.com"; organizations = "test"; chefserver = serverAddress + ORGANIZATIONS_STR + organizations; diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/chefclient/TestChefApiClient.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/chefclient/TestChefApiClient.java new file mode 100644 index 000000000..3dc9ff20d --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/chefclient/TestChefApiClient.java @@ -0,0 +1,107 @@ +package org.openecomp.appc.adapter.chef.chefclient; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; +import java.util.TimeZone; +import java.util.regex.Pattern; + + +import org.junit.Before; +import org.junit.Test; +import org.openecomp.appc.adapter.chef.chefapi.ApiMethod; +import org.openecomp.appc.adapter.chef.chefapi.Delete; +import org.openecomp.appc.adapter.chef.chefapi.Get; +import org.openecomp.appc.adapter.chef.chefapi.Post; +import org.openecomp.appc.adapter.chef.chefapi.Put; + +public class TestChefApiClient { + + private ChefApiClient client; + private Properties props; + + @Before + public void setup() throws IllegalArgumentException, IllegalAccessException { + props = new Properties(); + InputStream propStr = getClass().getResourceAsStream("/test.properties"); + if (propStr == null) { + fail("src/test/resources/test.properties missing"); + } + + try { + props.load(propStr); + propStr.close(); + } catch (Exception e) { + e.printStackTrace(); + fail("Could not initialize properties"); + } + client = new ChefApiClient( + props.getProperty("org.openecomp.appc.adapter.chef.chefclient.userId"), + System.getProperty("user.dir") + + props.getProperty("org.openecomp.appc.adapter.chef.chefclient.pemPath"), + props.getProperty("org.openecomp.appc.adapter.chef.chefclient.endPoint"), + props.getProperty("org.openecomp.appc.adapter.chef.chefclient.organizations")); + } + + @Test + public void testGet(){ + Get get = client.get(props.getProperty("org.openecomp.appc.adapter.chef.chefclient.path")); + ApiMethod method = get.createRequest(); + String[] response = method.test.split("\n"); + + thenStringShouldMatch("GET", response); + } + + @Test + public void testPut(){ + Put put = client.put(props.getProperty("org.openecomp.appc.adapter.chef.chefclient.path")); + ApiMethod method = put.createRequest(); + String[] response = method.test.split("\n"); + + thenStringShouldMatch("PUT", response); + } + + @Test + public void testPost() { + Post post = client.post(props.getProperty("org.openecomp.appc.adapter.chef.chefclient.path")); + ApiMethod method = post.createRequest(); + String[] response = method.test.split("\n"); + + thenStringShouldMatch("POST", response); + } + + @Test + public void testDelete(){ + Delete delete = client.delete(props.getProperty("org.openecomp.appc.adapter.chef.chefclient.path")); + ApiMethod method = delete.createRequest(); + String[] response = method.test.split("\n"); + + thenStringShouldMatch("DELETE", response); + } + + private String timestamp(){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + String timeStamp = sdf.format(new Date()); + timeStamp = timeStamp.replace(" ", "T"); + timeStamp = timeStamp + "Z"; + return timeStamp; + } + + private void thenStringShouldMatch(String method, String[] response){ + assertEquals("sb Method:" + method, response[0]); + assertEquals("Hashed Path:+JEk1y2gXwqZRweNjXYtx4ojxW8=", response[1]); + assertEquals("X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=", response[2]); + String timestamp = timestamp().substring(0, timestamp().length() - 3); + String regEx = "X-Ops-Timestamp:" + + timestamp + + "..."; + assertTrue(Pattern.matches(regEx, response[3])); + assertEquals("X-Ops-UserId:test", response[4]); + } +} diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/impl/TestChefAdapterImpl.java b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/impl/TestChefAdapterImpl.java index d0eb060a7..bb63d342c 100644 --- a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/impl/TestChefAdapterImpl.java +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/impl/TestChefAdapterImpl.java @@ -25,97 +25,89 @@ package org.openecomp.appc.adapter.chef.impl; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Properties; -import java.util.Set; +import org.junit.After; import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; -import org.openecomp.appc.Constants; -import org.openecomp.appc.adapter.chef.ChefAdapter; -import org.openecomp.appc.adapter.chef.impl.ChefAdapterImpl; -import org.openecomp.appc.configuration.ConfigurationFactory; import org.openecomp.appc.exceptions.APPCException; -import org.openecomp.appc.exceptions.UnknownProviderException; import com.att.cdp.exceptions.ZoneException; -import com.att.cdp.zones.ComputeService; -import com.att.cdp.zones.Context; -import com.att.cdp.zones.ContextFactory; -import com.att.cdp.zones.model.Server; -import com.att.cdp.zones.model.Server.Status; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; -import org.slf4j.MDC; -@Ignore public class TestChefAdapterImpl { - + private SvcLogicContext svcContext; private ChefAdapterImpl adapter; - @SuppressWarnings("nls") - @BeforeClass - public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException { - - } + private Map<String, String> params; + private String getAttribute; @Before - public void setup() throws IllegalArgumentException, IllegalAccessException { + public void setup() { + adapter = new ChefAdapterImpl(Boolean.TRUE); + params = new HashMap<>(); + params.put("org.openecomp.appc.instance.pemPath", + "/src/test/resources/testclient.pem"); + } - adapter = new ChefAdapterImpl(System.getProperty("user.dir")+"/src/main/resources/client.pem"); + @After + public void tearDown() { + params = null; + svcContext = null; + getAttribute = null; } @Test - public void testChefGet() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - Map<String, String> params = new HashMap<>(); - params.put("org.openecomp.appc.instance.chefAction", "/nodes"); - - SvcLogicContext svcContext = new SvcLogicContext(); - adapter.chefGet(params, svcContext); - String status=svcContext.getAttribute("org.openecomp.appc.chefServerResult.code"); - assertEquals("200",status); + public void testChefGetFail() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { + params.put("org.openecomp.appc.instance.chefAction", "/nodes"); + givenParams(params, "chefGet"); + thenResponseShouldFail(); } @Test - public void testChefPut() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - Map<String, String> params = new HashMap<>(); - params.put("org.openecomp.appc.instance.chefAction", "/nodes/testnode"); - params.put("org.openecomp.appc.instance.runList", "recipe[commandtest]"); - params.put("org.openecomp.appc.instance.attributes", ""); - SvcLogicContext svcContext = new SvcLogicContext(); - adapter.chefPut(params, svcContext); - String status=svcContext.getAttribute("org.openecomp.appc.chefServerResult.code"); - assertEquals("200",status); - + public void testChefPutFail() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { + params.put("org.openecomp.appc.instance.chefAction", "/nodes/testnode"); + params.put("org.openecomp.appc.instance.runList", "recipe[commandtest]"); + params.put("org.openecomp.appc.instance.attributes", ""); + params.put("org.openecomp.appc.instance.chefRequestBody", "Test Body"); + + givenParams(params, "chefPut"); + thenResponseShouldFail(); } @Test - public void testTrigger() throws IOException, IllegalStateException, IllegalArgumentException, - ZoneException, APPCException { - - Map<String, String> params = new HashMap<>(); - params.put("org.openecomp.appc.instance.ip", "http://example.com/test"); - SvcLogicContext svcContext = new SvcLogicContext(); - adapter.trigger(params, svcContext); - String status=svcContext.getAttribute("org.openecomp.appc.chefAgent.code"); - assertEquals("200",status); + public void testTriggerFail() throws IOException, IllegalStateException, IllegalArgumentException, + ZoneException, APPCException { + params.put("org.openecomp.appc.instance.ip", ""); + givenParams(params, "trigger"); + thenResponseShouldFail(); } + private void givenParams(Map<String, String> adapterParams, String method) { + svcContext = new SvcLogicContext(); + if (method == "chefGet"){ + adapter.chefGet(adapterParams, svcContext); + getAttribute = "org.openecomp.appc.chefServerResult.code"; + } + if (method == "chefPut"){ + adapter.chefPut(adapterParams, svcContext); + getAttribute = "org.openecomp.appc.chefServerResult.code"; + } + if (method == "trigger"){ + adapter.trigger(adapterParams, svcContext); + getAttribute = "org.openecomp.appc.chefAgent.code"; + } + } + private void thenResponseShouldFail(){ + String status = svcContext.getAttribute(this.getAttribute); + assertEquals("500", status); + } } diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/test.properties b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/test.properties new file mode 100644 index 000000000..4805e4316 --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/test.properties @@ -0,0 +1,5 @@ +org.openecomp.appc.adapter.chef.chefclient.userId=test +org.openecomp.appc.adapter.chef.chefclient.pemPath=/src/test/resources/testclient.pem +org.openecomp.appc.adapter.chef.chefclient.endPoint=http://test.com +org.openecomp.appc.adapter.chef.chefclient.organizations=onap +org.openecomp.appc.adapter.chef.chefclient.path=/test/path diff --git a/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/testclient.pem b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/testclient.pem new file mode 100644 index 000000000..a12f38118 --- /dev/null +++ b/appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/resources/testclient.pem @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAqDFyi0Tp4CQN2Q1kp2NXLPl5P4CVqjdXTUgxugupRC9aqJa5 +l7NOOkQWadSaU/CCtdsikp6ymyxEqR0Y3TtxmY3ongFBtLDaU/UnWXZY0pcaJBN/ +ZZKV9++lZF8+USKlZTQo+EqD7XlEjiDKjfwylaMTSwQPKGa0bDZZ6U44K63uuClw +mkvhtlaeS+t4Ah/YMU2nSxrWH3CpPxO4qD2KducrNRs01+hRxoVEaEX7HGXi8zdz +BHSp8mcA8mGl9uJFXN8Gtgphrlknq7DPjVb0iZ9Lw2u6WNhZizFHghEwHRSJYn26 +DNzmOjyD0IaQf5lf1L1sO5DcPkWC2I21MNaE4QIDAQABAoIBAEa/Xe4lE7d7kvOd +BZy/VZkOaykB/nJ2CtvwJTKb2xxaSuklVXXxL1Ok9kSX8D6kqWazgYxpArnw2gTE +v4O3kGZF4fYskyXdSkkMkvu3o08Zzh4ksW7ZRQngnRJmWcEpMKcsVJt0RKAsZWDf +fDRTRDfbO69PSsz0vqnSBunzQ/9i4LbFDOKYnWFDOiGKzM8SXMvVUVpGAzvc773M +lthBAo9KpbNrbO0b3OGUM8pU2o9GRbBAEzIq8j/i2h/vHtswgM9g3IsHNqYbvJPD +uAY8hsYEPZh/RasIBLN1J9HD+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