2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.chef.chefclient;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
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;
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;
47 public class TestChefApiClient {
49 private ChefApiClient client;
50 private Properties props;
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");
63 } catch (Exception e) {
65 fail("Could not initialize properties");
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"));
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");
81 thenStringShouldMatch("GET", response);
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");
90 thenStringShouldMatch("PUT", response);
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");
99 thenStringShouldMatch("POST", response);
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");
108 thenStringShouldMatch("DELETE", response);
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";
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:" +
128 assertTrue(Pattern.matches(regEx, response[3]));
129 assertEquals("X-Ops-UserId:test", response[4]);