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