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