1 package org.onap.appc.adapter.chef.chefclient;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
7 import java.io.InputStream;
8 import java.text.SimpleDateFormat;
10 import java.util.Properties;
11 import java.util.TimeZone;
12 import java.util.regex.Pattern;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.onap.appc.adapter.chef.chefapi.ApiMethod;
18 import org.onap.appc.adapter.chef.chefapi.Delete;
19 import org.onap.appc.adapter.chef.chefapi.Get;
20 import org.onap.appc.adapter.chef.chefapi.Post;
21 import org.onap.appc.adapter.chef.chefapi.Put;
23 public class TestChefApiClient {
25 private ChefApiClient client;
26 private Properties props;
29 public void setup() throws IllegalArgumentException, IllegalAccessException {
30 props = new Properties();
31 InputStream propStr = getClass().getResourceAsStream("/test.properties");
32 if (propStr == null) {
33 fail("src/test/resources/test.properties missing");
39 } catch (Exception e) {
41 fail("Could not initialize properties");
43 client = new ChefApiClient(
44 props.getProperty("org.onap.appc.adapter.chef.chefclient.userId"),
45 System.getProperty("user.dir") +
46 props.getProperty("org.onap.appc.adapter.chef.chefclient.pemPath"),
47 props.getProperty("org.onap.appc.adapter.chef.chefclient.endPoint"),
48 props.getProperty("org.onap.appc.adapter.chef.chefclient.organizations"));
52 public void testGet(){
53 Get get = client.get(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
54 ApiMethod method = get.createRequest();
55 String[] response = method.test.split("\n");
57 thenStringShouldMatch("GET", response);
61 public void testPut(){
62 Put put = client.put(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
63 ApiMethod method = put.createRequest();
64 String[] response = method.test.split("\n");
66 thenStringShouldMatch("PUT", response);
70 public void testPost() {
71 Post post = client.post(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
72 ApiMethod method = post.createRequest();
73 String[] response = method.test.split("\n");
75 thenStringShouldMatch("POST", response);
79 public void testDelete(){
80 Delete delete = client.delete(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
81 ApiMethod method = delete.createRequest();
82 String[] response = method.test.split("\n");
84 thenStringShouldMatch("DELETE", response);
87 private String timestamp(){
88 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
89 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
90 String timeStamp = sdf.format(new Date());
91 timeStamp = timeStamp.replace(" ", "T");
92 timeStamp = timeStamp + "Z";
96 private void thenStringShouldMatch(String method, String[] response){
97 assertEquals("sb Method:" + method, response[0]);
98 assertEquals("Hashed Path:+JEk1y2gXwqZRweNjXYtx4ojxW8=", response[1]);
99 assertEquals("X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=", response[2]);
100 String timestamp = timestamp().substring(0, timestamp().length() - 3);
101 String regEx = "X-Ops-Timestamp:" +
104 assertTrue(Pattern.matches(regEx, response[3]));
105 assertEquals("X-Ops-UserId:test", response[4]);