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/appc-chef-adapter-bundle/src/test | |
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/appc-chef-adapter-bundle/src/test')
4 files changed, 192 insertions, 61 deletions
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+Ex1q2OA+Yi3jBtT4s545MJIHpdmo57/B2SHQbug +KuWTsb0CgYEA0ZKIig/jeZEOokvWxDrN6Ok2Hb9TFZcNi/qBLcQ25A1C2fDkvsHc +S6UDxknT2cyQ77R9xK25vrFKKrv5FUwgg7h0ps19w8xSZRG9Wvg0nus69oavXIDi +MOCBfb6pn2+Glhdt84Ku0oSTPtfcTHerACOunGATpk1NW2dlmDbvmTcCgYEAzXQs +9lS31+2Oh64g4gH/6Jfx+Mh5JY+2bUhSMy53tulzFwfI1zLyyu9SLLfVbAnxhmS8 +rSg7Q2mN492ZhnoVScYN3RTpudBJIinqlFoarqu+6E83k+83wGv3m2pP6XPbpHCy +QE6IQm5U+ZSbMRD+8SjG3pbPRYcwww5xRsIj/qcCgYA2kEc6Yu6fzROZT9OH3aOU +u3tafWC9Y0mko0EU0FxWPdmk8qIrxD999mWoL7qXnzoxHrYMCgstSe18eNpeICbr +BJBiiWfwHXdqVxcM40iYA7ijTOfFVs0NWrZ8LbLuDtRkqY738pNfviK0HvF9ez6r +V57zmdQj3UaBwMbjvZHeOwKBgQCA0gsvGMd8+FKZ+DBeBWrz5/GsT+bGCmoT83i2 +5rfhVFb5ZcQkSqm5XH1l1I5ZA5MQ9TDoUYV3K0PwUA3nJ70ZWLlwmaBDBboVVbrj +8esxAjbdam4qr5+BYzEJnYslkaNyY8cgUx1UqeFV7DuydDml9C9deanUqoOEihW0 +jB4NmQKBgD4nwTUcQIbzeWnFoPxbDMQIIheZ3EK+rEbmXiU3blPhmCoye9q58Qet +YaeQKiJEF2mWnBE6VtSg84OiENSAMxUn2VlwopFoTbfLKD4qfGoGWm9tjUdBblHe +3U3ZdQLYKTiSNr+LXrAI0w4MukmL8vzxzo80tcB4+tePyWN/lqoq +-----END RSA PRIVATE KEY----- |