code changes for platform hardening appc adapters
[appc.git] / appc-adapters / appc-chef-adapter / appc-chef-adapter-bundle / src / test / java / org / onap / appc / adapter / chef / impl / TestChefAdapterImpl.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.impl;
26
27 import static org.junit.Assert.assertEquals;
28
29 import java.io.IOException;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.junit.After;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.appc.exceptions.APPCException;
37 import com.att.cdp.exceptions.ZoneException;
38 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
40
41 public class TestChefAdapterImpl {
42     private SvcLogicContext svcContext;
43
44     private ChefAdapterImpl adapter;
45
46     private Map<String, String> params;
47     private String getAttribute;
48
49     @Before
50     public void setup() {
51         adapter = new ChefAdapterImpl(Boolean.TRUE);
52         params = new HashMap<>();
53         params.put("pemPath",
54                 "/src/test/resources/testclient.pem");
55     }
56
57     @After
58     public void tearDown() {
59         params = null;
60         svcContext = null;
61         getAttribute = null;
62     }
63     
64         @Test(expected=Exception.class)
65     public void testChefGetFail() throws IOException, IllegalStateException, IllegalArgumentException,
66             ZoneException, APPCException,SvcLogicException {
67         params.put("chefAction", "/nodes");
68
69         givenParams(params, "chefGet");
70         thenResponseShouldFail();
71     }
72
73       @Test(expected=Exception.class)
74     public void testChefPutFail() throws IOException, IllegalStateException, IllegalArgumentException,
75             ZoneException, APPCException,SvcLogicException {
76         params.put("chefAction", "/nodes/testnode");
77         params.put("runList", "recipe[commandtest]");
78         params.put("attributes", "");
79         params.put("chefRequestBody", "Test Body");
80
81         givenParams(params, "chefPut");
82         thenResponseShouldFail();
83     }
84
85     @Test
86     public void testTriggerFail() throws IOException, IllegalStateException, IllegalArgumentException,
87             ZoneException, APPCException,SvcLogicException {
88         params.put("ip", "");
89
90         givenParams(params, "trigger");
91         thenResponseShouldFail();
92     }
93
94     private void givenParams(Map<String, String> adapterParams, String method) throws SvcLogicException {
95         svcContext = new SvcLogicContext();
96         if (method == "chefGet"){
97             adapter.chefGet(adapterParams, svcContext);
98             getAttribute = "chefServerResult.code";
99         }
100         if (method == "chefPut"){
101             adapter.chefPut(adapterParams, svcContext);
102             getAttribute = "chefServerResult.code";
103         }
104         if (method == "trigger"){
105             adapter.trigger(adapterParams, svcContext);
106             getAttribute = "chefAgent.code";
107         }
108     }
109
110     private void thenResponseShouldFail(){
111         String status = svcContext.getAttribute(this.getAttribute);
112         assertEquals("500", status);
113     }
114 }