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;
30 import java.io.InputStream;
31 import java.time.LocalDateTime;
32 import java.time.ZoneId;
33 import java.time.format.DateTimeFormatter;
34 import java.time.temporal.ChronoUnit;
35 import java.util.Properties;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.appc.adapter.chef.chefapi.ApiMethod;
39 import org.onap.appc.adapter.chef.chefapi.Delete;
40 import org.onap.appc.adapter.chef.chefapi.Get;
41 import org.onap.appc.adapter.chef.chefapi.Post;
42 import org.onap.appc.adapter.chef.chefapi.Put;
44 public class TestChefApiClient {
46 private ChefApiClient client;
47 private Properties props;
48 private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
51 public void setup() throws IllegalArgumentException, IllegalAccessException {
52 props = new Properties();
53 InputStream propStr = getClass().getResourceAsStream("/test.properties");
54 if (propStr == null) {
55 fail("src/test/resources/test.properties missing");
61 } catch (Exception e) {
63 fail("Could not initialize properties");
65 client = new ChefApiClient(props.getProperty("org.onap.appc.adapter.chef.chefclient.userId"),
66 System.getProperty("user.dir") + props.getProperty("org.onap.appc.adapter.chef.chefclient.pemPath"),
67 props.getProperty("org.onap.appc.adapter.chef.chefclient.endPoint"),
68 props.getProperty("org.onap.appc.adapter.chef.chefclient.organizations"));
72 public void testGet() {
73 Get get = client.get(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
74 ApiMethod method = get.execute();
75 String[] response = method.test.split("\n");
76 thenStringShouldMatch("GET", response);
80 public void testPut() {
81 Put put = client.put(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
82 ApiMethod method = put.execute();
83 String[] response = method.test.split("\n");
85 thenStringShouldMatch("PUT", response);
89 public void testPost() {
90 Post post = client.post(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
91 ApiMethod method = post.execute();
92 String[] response = method.test.split("\n");
94 thenStringShouldMatch("POST", response);
98 public void testDelete() {
99 Delete delete = client.delete(props.getProperty("org.onap.appc.adapter.chef.chefclient.path"));
100 ApiMethod method = delete.execute();
101 String[] response = method.test.split("\n");
103 thenStringShouldMatch("DELETE", response);
106 private void thenStringShouldMatch(String method, String[] response) {
107 assertEquals("sb Method:" + method, response[0]);
108 assertEquals("Hashed Path:+JEk1y2gXwqZRweNjXYtx4ojxW8=", response[1]);
109 assertEquals("X-Ops-Content-Hash:2jmj7l5rSw0yVb/vlWAYkK/YBwk=", response[2]);
110 checkTimestamp(response[3], 5000);
111 assertEquals("X-Ops-UserId:test", response[4]);
114 private void checkTimestamp(String timeStampHeader, long maxDeltaMs) {
115 assertTrue(timeStampHeader.startsWith("X-Ops-Timestamp:"));
116 LocalDateTime ld1 = LocalDateTime.parse(timeStampHeader.replace("X-Ops-Timestamp:", ""), dtf);
117 assertTrue(ChronoUnit.MILLIS.between(ld1, LocalDateTime.now(ZoneId.of("UTC"))) <= maxDeltaMs);