First part of onap rename
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / chefclient / TestChefApiClient.java
1 package org.onap.appc.adapter.chef.chefclient;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.junit.Assert.fail;
6
7 import java.io.InputStream;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import java.util.Properties;
11 import java.util.TimeZone;
12 import java.util.regex.Pattern;
13
14
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;
22
23 public class TestChefApiClient {
24
25     private ChefApiClient client;
26     private Properties props;
27
28     @Before
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");
34         }
35
36         try {
37             props.load(propStr);
38             propStr.close();
39         } catch (Exception e) {
40             e.printStackTrace();
41             fail("Could not initialize properties");
42         }
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"));
49     }
50
51     @Test
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");
56
57         thenStringShouldMatch("GET", response);
58     }
59
60     @Test
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");
65
66         thenStringShouldMatch("PUT", response);
67     }
68
69     @Test
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");
74
75         thenStringShouldMatch("POST", response);
76     }
77
78     @Test
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");
83
84         thenStringShouldMatch("DELETE", response);
85     }
86
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";
93         return timeStamp;
94     }
95
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:" +
102                  timestamp +
103                 "...";
104         assertTrue(Pattern.matches(regEx, response[3]));
105         assertEquals("X-Ops-UserId:test", response[4]);
106     }
107 }