bf718ab2f96f8f483e7f96a86b51d9e43ad5fb54
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / chefclient / TestChefApiClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.adapter.chef.chefclient;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import java.io.InputStream;
32 import java.text.SimpleDateFormat;
33 import java.util.Date;
34 import java.util.Properties;
35 import java.util.TimeZone;
36 import java.util.regex.Pattern;
37
38
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.appc.adapter.chef.chefapi.ApiMethod;
42 import org.onap.appc.adapter.chef.chefapi.Delete;
43 import org.onap.appc.adapter.chef.chefapi.Get;
44 import org.onap.appc.adapter.chef.chefapi.Post;
45 import org.onap.appc.adapter.chef.chefapi.Put;
46
47 public class TestChefApiClient {
48
49     private ChefApiClient client;
50     private Properties props;
51
52     @Before
53     public void setup() throws IllegalArgumentException, IllegalAccessException {
54         props = new Properties();
55         InputStream propStr = getClass().getResourceAsStream("/test.properties");
56         if (propStr == null) {
57             fail("src/test/resources/test.properties missing");
58         }
59
60         try {
61             props.load(propStr);
62             propStr.close();
63         } catch (Exception e) {
64             e.printStackTrace();
65             fail("Could not initialize properties");
66         }
67         client = new ChefApiClient(
68                 props.getProperty("org.onap.appc.adapter.chef.chefclient.userId"),
69                 System.getProperty("user.dir") +
70                         props.getProperty("org.onap.appc.adapter.chef.chefclient.pemPath"),
71                 props.getProperty("org.onap.appc.adapter.chef.chefclient.endPoint"),
72                 props.getProperty("org.onap.appc.adapter.chef.chefclient.organizations"));
73     }
74
75     @Test
76     public void testGet(){
77         Get get = client.get(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
78         ApiMethod method = get.execute();
79         String[] response = method.test.split("\n");
80
81         thenStringShouldMatch("GET", response);
82     }
83
84     @Test
85     public void testPut(){
86         Put put = client.put(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
87         ApiMethod method = put.execute();
88         String[] response = method.test.split("\n");
89
90         thenStringShouldMatch("PUT", response);
91     }
92
93     @Test
94     public void testPost() {
95         Post post = client.post(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
96         ApiMethod method = post.execute();
97         String[] response = method.test.split("\n");
98
99         thenStringShouldMatch("POST", response);
100     }
101
102     @Test
103     public void testDelete(){
104         Delete delete = client.delete(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
105         ApiMethod method = delete.execute();
106         String[] response = method.test.split("\n");
107
108         thenStringShouldMatch("DELETE", response);
109     }
110
111     private String timestamp(){
112         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
113         sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
114         String timeStamp = sdf.format(new Date());
115         timeStamp = timeStamp.replace(" ", "T");
116         timeStamp = timeStamp + "Z";
117         return timeStamp;
118     }
119
120     private void thenStringShouldMatch(String method, String[] response){
121         assertEquals("sb Method:" + method, response[0]);
122         assertEquals("Hashed Path:+JEk1y2gXwqZRweNjXYtx4ojxW8=", response[1]);
123         assertEquals("X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=", response[2]);
124         String timestamp = timestamp().substring(0, timestamp().length() - 3);
125         String regEx = "X-Ops-Timestamp:" +
126                  timestamp +
127                 "...";
128         assertTrue(Pattern.matches(regEx, response[3]));
129         assertEquals("X-Ops-UserId:test", response[4]);
130     }
131 }