b8be22560355e8d82943a927f9d88b0fa31b6dff
[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 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;
43
44 public class TestChefApiClient {
45
46     private ChefApiClient client;
47     private Properties props;
48     private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
49
50     @Before
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");
56         }
57
58         try {
59             props.load(propStr);
60             propStr.close();
61         } catch (Exception e) {
62             e.printStackTrace();
63             fail("Could not initialize properties");
64         }
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"));
69     }
70
71     @Test
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);
77     }
78
79     @Test
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");
84
85         thenStringShouldMatch("PUT", response);
86     }
87
88     @Test
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");
93
94         thenStringShouldMatch("POST", response);
95     }
96
97     @Test
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");
102
103         thenStringShouldMatch("DELETE", response);
104     }
105
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]);
112     }
113
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);
118     }
119 }