ChefApiClient Package Reorganization and cleanup
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / chefclient / impl / ChefApiHeaderFactoryTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.appc.adapter.chef.chefclient.impl;
21
22 import static junit.framework.TestCase.assertEquals;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.Matchers.any;
25
26 import com.google.common.collect.ImmutableMap;
27 import java.util.Date;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.runners.MockitoJUnitRunner;
33
34 @RunWith(MockitoJUnitRunner.class)
35 public class ChefApiHeaderFactoryTest {
36
37     private static final String ORGANIZATIONS_PATH = "onap";
38     private static final String USER_ID = "testUser";
39     private static final String REQUEST_PATH = "/test/path";
40     private static final String EXPECTED_TIMESTAMP = "1970-01-15T06:56:07Z";
41     private static final String EMPTY_BODY = "";
42
43     @Mock
44     private FormattedTimestamp formattedTimestamp;
45
46     @InjectMocks
47     private ChefApiHeaderFactory chefApiHeaderFactory;
48
49     @Test
50     public void create_shouldCreateProperChefHeaders_withHashedAuthorizationString() {
51         // GIVEN
52         given(formattedTimestamp.format(any(Date.class))).willReturn(EXPECTED_TIMESTAMP);
53         String pemFilePath = getClass().getResource("/testclient.pem").getPath();
54
55         // WHEN
56         ImmutableMap<String, String> headers = chefApiHeaderFactory
57             .create("GET", REQUEST_PATH, "", USER_ID, ORGANIZATIONS_PATH, pemFilePath);
58
59         // THEN
60         assertEquals(headers, createExpectedHeaders());
61     }
62
63     private ImmutableMap<String, String> createExpectedHeaders() {
64         String hashedBody = Utils.sha1AndBase64(EMPTY_BODY);
65         ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
66         builder
67             .put("Content-type", "application/json")
68             .put("Accept", "application/json")
69             .put("X-Ops-Timestamp", EXPECTED_TIMESTAMP)
70             .put("X-Ops-UserId", USER_ID)
71             .put("X-Chef-Version", "12.4.1")
72             .put("X-Ops-Content-Hash", hashedBody)
73             .put("X-Ops-Sign", "version=1.0")
74             .put("X-Ops-Authorization-1", "i+HGCso703727yd2ZQWMZIIpGKgTzm41fA31LIExNxEf9mOUMcpesIHjH/Wr")
75             .put("X-Ops-Authorization-2", "QEvsX/Gy1ay9KsUtqhy9GA6PB8UfDeMNoVUisqR4HQW+S6IOfvqBjW+2afzE")
76             .put("X-Ops-Authorization-3", "RdRReB/TJIF3s6ZC8vNpbEdY9kHmwiDglhxmS8X2FS+ArSh/DK/i7MqBbjux")
77             .put("X-Ops-Authorization-4", "49iiOlRVG7aTr/FA115hlBYP9CYCIQWKIBUOK3JyV9fXNdVqc9R0r1XdjxUl")
78             .put("X-Ops-Authorization-5", "EDGw6tuE8YW8mH5wkgHCjKpXG3WjmWt2X6kUrdIu44qCBK2N3sZziSub2fJA")
79             .put("X-Ops-Authorization-6", "hPBuOhjiYDZuFUqC99lCryM0Hf5RMw1uTlkYsBEZmA==");
80
81         return builder.build();
82     }
83 }