Ansible ConnectionBuilder cover update
[ccsdk/sli/adaptors.git] / ansible-adapter / ansible-adapter-bundle / src / test / java / org / onap / ccsdk / adapter / ansible / impl / TestConnectionBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * onap
4  * ================================================================================
5  * Copyright (C) 2018 Samsung
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
21 package org.onap.ccsdk.adapter.ansible.impl;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.onap.ccsdk.sli.adaptors.ansible.impl.ConnectionBuilder;
26 import org.onap.ccsdk.sli.adaptors.ansible.model.AnsibleResult;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
28
29 import javax.net.ssl.SSLException;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.security.KeyManagementException;
33 import java.security.KeyStoreException;
34 import java.security.NoSuchAlgorithmException;
35 import java.security.cert.CertificateException;
36
37 import static org.junit.Assert.assertEquals;
38
39 public class TestConnectionBuilder {
40     ConnectionBuilder builder;
41     @Before
42     public void setup()
43             throws SSLException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
44         builder = new ConnectionBuilder(1);
45     }
46
47
48     @Test
49     public void testSetHttpContext() throws IllegalStateException, IllegalArgumentException {
50         String user = "testUser";
51         String pass = "testPassword";
52
53         builder.setHttpContext(user, pass);
54     }
55
56     @Test
57     public void testPost() throws IllegalStateException, IllegalArgumentException {
58         String user = "testUser";
59         String pass = "testPassword";
60         String agentUrl = "test/server.com";
61         String payload = "testPayload";
62
63         builder.setHttpContext(user, pass);
64         AnsibleResult result = builder.post(agentUrl, payload);
65
66         assertEquals(611, result.getStatusCode());
67         assertEquals(null, result.getStatusMessage());
68         assertEquals("UNKNOWN", result.getResults());
69     }
70
71     @Test
72     public void testGet() throws IllegalStateException, IllegalArgumentException {
73         String user = "testUser";
74         String pass = "testPassword";
75         String agentUrl = "test/server.com";
76
77         builder.setHttpContext(user, pass);
78         AnsibleResult result = builder.get(agentUrl);
79
80         assertEquals(611, result.getStatusCode());
81         assertEquals(null, result.getStatusMessage());
82         assertEquals("UNKNOWN", result.getResults());
83     }
84
85     @Test
86     public void testGetMode()
87             throws SSLException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
88         String user = "testUser";
89         String pass = "testPassword";
90         String agentUrl = "test/server.com";
91
92         builder = new ConnectionBuilder(2);
93         builder.setHttpContext(user, pass);
94         AnsibleResult result = builder.get(agentUrl);
95
96         assertEquals(611, result.getStatusCode());
97         assertEquals(null, result.getStatusMessage());
98         assertEquals("UNKNOWN", result.getResults());
99     }
100
101     @Test (expected = FileNotFoundException.class)
102     public void testGetModeCert()
103             throws KeyStoreException, CertificateException, IOException,
104             KeyManagementException, NoSuchAlgorithmException, SvcLogicException {
105         String user = "testUser";
106         String pass = "testPassword";
107         String agentUrl = "test/server.com";
108         String certFile = "testCert";
109
110         builder = new ConnectionBuilder(certFile);
111         builder.setHttpContext(user, pass);
112         AnsibleResult result = builder.get(agentUrl);
113
114         assertEquals(611, result.getStatusCode());
115         assertEquals(null, result.getStatusMessage());
116         assertEquals("UNKNOWN", result.getResults());
117     }
118
119 }