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