36ef1ad37bc6023a25a5a556468926f075d10785
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / LCM / operation / ProviderOperationsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2018 Nokia Solutions and Networks
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  * ============LICENSE_END=========================================================
22  */
23 package org.onap.appc.listener.LCM.operation;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Matchers.anyString;
31
32 import com.att.aft.dme2.internal.jersey.core.util.Base64;
33 import java.io.IOException;
34 import java.net.Socket;
35 import java.net.SocketException;
36 import java.security.KeyManagementException;
37 import java.security.KeyStore;
38 import java.security.KeyStoreException;
39 import java.security.NoSuchAlgorithmException;
40 import java.security.UnrecoverableKeyException;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mock;
44 import org.onap.appc.exceptions.APPCException;
45 import org.onap.appc.listener.LCM.operation.ProviderOperations.MySSLSocketFactory;
46
47 public class ProviderOperationsTest {
48
49     private ProviderOperations providerOperations;
50     private MySSLSocketFactory socketFactory;
51
52     @Mock
53     private KeyStore mockKeyStore;
54
55
56     @Before
57     public void setup()
58         throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
59
60         providerOperations =
61             new ProviderOperations("http://127.0.0.1", "test_user", "test_password");
62         socketFactory = new MySSLSocketFactory(mockKeyStore);
63     }
64
65     @Test
66     public void setAuthentication_should_return_null_given_null_arguments() {
67         String newAuthentication = providerOperations.setAuthentication(null, null);
68         assertNull(newAuthentication);
69     }
70
71     @Test
72     public void should_set_properties() {
73         providerOperations.setUrl("hp://123.1.2.3");
74         assertEquals("http://127.0.0.1", providerOperations.getUrl());
75         providerOperations.setUrl("http://123.1.2.3");
76         assertEquals("http://123.1.2.3", providerOperations.getUrl());
77
78         String newAuthentication = providerOperations.setAuthentication("new_user", "new_password");
79         assertEquals("new_user:new_password", Base64.base64Decode(newAuthentication));
80     }
81
82     @Test
83     public void isSucceeded_should_resolve_status_codes() {
84
85         assertFalse(ProviderOperations.isSucceeded(null));
86         assertFalse(ProviderOperations.isSucceeded(200));
87         assertTrue(ProviderOperations.isSucceeded(100));
88         assertTrue(ProviderOperations.isSucceeded(400));
89     }
90
91     @Test(expected = APPCException.class)
92     public void topologyDG_should_throw_given_null_message() throws APPCException {
93
94         providerOperations.topologyDG("test-rpc-name", null);
95     }
96
97     @Test(expected = SocketException.class)
98     public void sslSocketFactory_should_throw_when_socket_not_connected() throws IOException {
99         Socket socket = socketFactory.createSocket();
100         assertNotNull(socket);
101
102         socketFactory.createSocket(socket, "127.0.0.1", 123, true);
103     }
104
105     //TODO write some test cases for topologyDG method
106 }