MR Java 11 Uplift
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / service / impl / AdminServiceImplemTest.java
1 /*-
2 /*-
3  * ============LICENSE_START=======================================================
4  * ONAP Policy Engine
5  * ================================================================================
6  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22  package org.onap.dmaap.mr.cambria.service.impl;
23
24 import static org.junit.Assert.*;
25
26 import java.io.IOException;
27
28 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory;
29 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
30 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
31 import org.onap.dmaap.dmf.mr.service.impl.AdminServiceImpl;
32 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
33 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
34 import com.att.nsa.configs.ConfigDbException;
35 import com.att.nsa.limits.Blacklist;
36 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
37 import com.att.nsa.security.db.simple.NsaSimpleApiKey;
38
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.InjectMocks;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.powermock.api.mockito.PowerMockito;
47 import org.powermock.core.classloader.annotations.PowerMockIgnore;
48 import org.powermock.core.classloader.annotations.PrepareForTest;
49 import org.powermock.modules.junit4.PowerMockRunner;
50
51 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
52 @RunWith(PowerMockRunner.class)
53 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
54 public class AdminServiceImplemTest {
55
56         @InjectMocks
57         AdminServiceImpl adminServiceImpl;
58
59         @Mock
60         DMaaPContext dmaapContext;
61         @Mock
62         ConsumerFactory factory;
63
64         @Mock
65         ConfigurationReader configReader;
66         @Mock
67         Blacklist Blacklist;
68
69         @Before
70         public void setUp() throws Exception {
71
72                 MockitoAnnotations.initMocks(this);
73                 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
74                 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
75
76                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
77                 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
78                 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
79
80                 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
81                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
82         }
83
84         @After
85         public void tearDown() throws Exception {
86         }
87
88         // ISSUES WITH AUTHENTICATION
89         @Test
90         public void testShowConsumerCache() {
91
92                 try {
93                         adminServiceImpl.showConsumerCache(dmaapContext);
94                 } catch (IOException | AccessDeniedException e) {
95                         // TODO Auto-generated catch block
96                         e.printStackTrace();
97                 } catch (NullPointerException e) {
98                         // TODO Auto-generated catch block
99                         // e.printStackTrace();
100                         assertTrue(true);
101                 }
102
103                 String trueValue = "True";
104                 assertTrue(trueValue.equalsIgnoreCase("True"));
105
106         }
107
108         @Test
109         public void testDropConsumerCache() {
110
111                 try {
112                         adminServiceImpl.dropConsumerCache(dmaapContext);
113                 } catch (IOException | AccessDeniedException e) {
114                         // TODO Auto-generated catch block
115                         e.printStackTrace();
116                 } catch (NullPointerException e) {
117                         // TODO Auto-generated catch block
118                         // e.printStackTrace();
119                         assertTrue(true);
120                 }
121
122                 String trueValue = "True";
123                 assertTrue(trueValue.equalsIgnoreCase("True"));
124
125         }
126
127         @Test
128         public void testGetBlacklist() {
129
130                 try {
131                         adminServiceImpl.getBlacklist(dmaapContext);
132                 } catch (IOException | AccessDeniedException e) {
133                         // TODO Auto-generated catch block
134                         e.printStackTrace();
135                 } catch (NullPointerException e) {
136                         // TODO Auto-generated catch block
137                         // e.printStackTrace();
138                         assertTrue(true);
139                 }
140
141                 String trueValue = "True";
142                 assertTrue(trueValue.equalsIgnoreCase("True"));
143
144         }
145
146         @Test
147         public void testAddToBlacklist() {
148
149                 try {
150                         adminServiceImpl.addToBlacklist(dmaapContext, "120.120.120.120");
151                 } catch (IOException | AccessDeniedException | ConfigDbException e) {
152                         // TODO Auto-generated catch block
153                         e.printStackTrace();
154                 } catch (NullPointerException e) {
155                         // TODO Auto-generated catch block
156                         // e.printStackTrace();
157                         assertTrue(true);
158                 }
159
160                 String trueValue = "True";
161                 assertTrue(trueValue.equalsIgnoreCase("True"));
162
163         }
164
165         @Test
166         public void testRemoveFromBlacklist() {
167
168                 try {
169                         adminServiceImpl.removeFromBlacklist(dmaapContext, "120.120.120.120");
170                 } catch (IOException | AccessDeniedException | ConfigDbException e) {
171                         // TODO Auto-generated catch block
172                         e.printStackTrace();
173                 } catch (NullPointerException e) {
174                         // TODO Auto-generated catch block
175                         // e.printStackTrace();
176                         assertTrue(true);
177                 }
178
179                 String trueValue = "True";
180                 assertTrue(trueValue.equalsIgnoreCase("True"));
181
182         }
183
184 }