From c2f55b2e5771f32b84bb07770d15b0d42b8e814b Mon Sep 17 00:00:00 2001 From: Marcus G K Williams Date: Thu, 7 Sep 2017 17:16:09 -0700 Subject: Add Unit Tests for appc-chef-adapter Issue-Id: APPC-181 Change-Id: Iee824ca929ea46944ac1673e709074687ad812f9 Signed-off-by: Marcus G K Williams --- .../adapter/chef/chefclient/TestChefApiClient.java | 107 +++++++++++++++++++ .../adapter/chef/impl/TestChefAdapterImpl.java | 114 ++++++++++----------- 2 files changed, 160 insertions(+), 61 deletions(-) create mode 100644 appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java/org/openecomp/appc/adapter/chef/chefclient/TestChefApiClient.java (limited to 'appc-adapters/appc-chef-adapter/appc-chef-adapter-bundle/src/test/java') 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 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 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 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 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 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); + } } -- cgit 1.2.3-korg