update the package name
[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.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
52 public class AdminServiceImplemTest {
53
54         @InjectMocks
55         AdminServiceImpl adminServiceImpl;
56
57         @Mock
58         DMaaPContext dmaapContext;
59         @Mock
60         ConsumerFactory factory;
61
62         @Mock
63         ConfigurationReader configReader;
64         @Mock
65         Blacklist Blacklist;
66
67         @Before
68         public void setUp() throws Exception {
69
70                 MockitoAnnotations.initMocks(this);
71                 PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
72                 NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
73
74                 PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
75                 PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
76                 PowerMockito.when(configReader.getfIpBlackList()).thenReturn(Blacklist);
77
78                 PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
79                 PowerMockito.mockStatic(DMaaPResponseBuilder.class);
80         }
81
82         @After
83         public void tearDown() throws Exception {
84         }
85
86         // ISSUES WITH AUTHENTICATION
87         @Test
88         public void testShowConsumerCache() {
89
90                 try {
91                         adminServiceImpl.showConsumerCache(dmaapContext);
92                 } catch (IOException | AccessDeniedException e) {
93                         // TODO Auto-generated catch block
94                         e.printStackTrace();
95                 } catch (NullPointerException e) {
96                         // TODO Auto-generated catch block
97                         // e.printStackTrace();
98                         assertTrue(true);
99                 }
100
101                 String trueValue = "True";
102                 assertTrue(trueValue.equalsIgnoreCase("True"));
103
104         }
105
106         @Test
107         public void testDropConsumerCache() {
108
109                 try {
110                         adminServiceImpl.dropConsumerCache(dmaapContext);
111                 } catch (IOException | AccessDeniedException e) {
112                         // TODO Auto-generated catch block
113                         e.printStackTrace();
114                 } catch (NullPointerException e) {
115                         // TODO Auto-generated catch block
116                         // e.printStackTrace();
117                         assertTrue(true);
118                 }
119
120                 String trueValue = "True";
121                 assertTrue(trueValue.equalsIgnoreCase("True"));
122
123         }
124
125         @Test
126         public void testGetBlacklist() {
127
128                 try {
129                         adminServiceImpl.getBlacklist(dmaapContext);
130                 } catch (IOException | AccessDeniedException e) {
131                         // TODO Auto-generated catch block
132                         e.printStackTrace();
133                 } catch (NullPointerException e) {
134                         // TODO Auto-generated catch block
135                         // e.printStackTrace();
136                         assertTrue(true);
137                 }
138
139                 String trueValue = "True";
140                 assertTrue(trueValue.equalsIgnoreCase("True"));
141
142         }
143
144         @Test
145         public void testAddToBlacklist() {
146
147                 try {
148                         adminServiceImpl.addToBlacklist(dmaapContext, "120.120.120.120");
149                 } catch (IOException | AccessDeniedException | ConfigDbException e) {
150                         // TODO Auto-generated catch block
151                         e.printStackTrace();
152                 } catch (NullPointerException e) {
153                         // TODO Auto-generated catch block
154                         // e.printStackTrace();
155                         assertTrue(true);
156                 }
157
158                 String trueValue = "True";
159                 assertTrue(trueValue.equalsIgnoreCase("True"));
160
161         }
162
163         @Test
164         public void testRemoveFromBlacklist() {
165
166                 try {
167                         adminServiceImpl.removeFromBlacklist(dmaapContext, "120.120.120.120");
168                 } catch (IOException | AccessDeniedException | ConfigDbException e) {
169                         // TODO Auto-generated catch block
170                         e.printStackTrace();
171                 } catch (NullPointerException e) {
172                         // TODO Auto-generated catch block
173                         // e.printStackTrace();
174                         assertTrue(true);
175                 }
176
177                 String trueValue = "True";
178                 assertTrue(trueValue.equalsIgnoreCase("True"));
179
180         }
181
182 }