summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java')
-rw-r--r--src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java219
1 files changed, 185 insertions, 34 deletions
diff --git a/src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java b/src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java
index 55bed19..0523337 100644
--- a/src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java
+++ b/src/test/java/com/att/nsa/cambria/service/impl/UIServiceImplTest.java
@@ -21,21 +21,73 @@
package com.att.nsa.cambria.service.impl;
import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import com.att.nsa.cambria.backends.ConsumerFactory;
import com.att.nsa.cambria.beans.DMaaPContext;
+import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;
+import com.att.nsa.cambria.metabroker.Topic;
+import com.att.nsa.cambria.security.DMaaPAuthenticatorImpl;
+import com.att.nsa.cambria.utils.ConfigurationReader;
+import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
import com.att.nsa.configs.ConfigDbException;
-import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
+import com.att.nsa.security.db.NsaApiDb;
+import com.att.nsa.security.db.simple.NsaSimpleApiKey;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })
public class UIServiceImplTest {
+ @InjectMocks
+ UIServiceImpl service;
+
+ @Mock
+ DMaaPContext dmaapContext;
+ @Mock
+ ConsumerFactory factory;
+
+ @Mock
+ ConfigurationReader configReader;
+
+ @Mock
+ DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
+
+ @Mock
+ Topic metatopic;
+
@Before
public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);
+ NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");
+
+ PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);
+
+ PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);
+ PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
}
@After
@@ -44,29 +96,27 @@ public class UIServiceImplTest {
@Test
public void testHello() {
-
- UIServiceImpl service = new UIServiceImpl();
+
try {
- service.hello(new DMaaPContext());
+ service.hello(dmaapContext);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
- }
-
+ }
+
String trueValue = "True";
assertTrue(trueValue.equalsIgnoreCase("True"));
-
+
}
-
+
@Test
public void testGetApiKeysTable() {
-
- UIServiceImpl service = new UIServiceImpl();
+
try {
- service.getApiKeysTable(new DMaaPContext());
+ service.getApiKeysTable(dmaapContext);
} catch (NullPointerException e) {
// TODO Auto-generated catch block
- //e.printStackTrace();
+ // e.printStackTrace();
assertTrue(true);
} catch (ConfigDbException e) {
// TODO Auto-generated catch block
@@ -75,18 +125,18 @@ public class UIServiceImplTest {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+ assertTrue(true);
+
}
-
+
@Test
public void testGetApiKey() {
-
- UIServiceImpl service = new UIServiceImpl();
+
try {
- service.getApiKey(new DMaaPContext(), "k56HmWT72J");
+ service.getApiKey(dmaapContext, "admin");
} catch (NullPointerException e) {
// TODO Auto-generated catch block
- //e.printStackTrace();
+ // e.printStackTrace();
assertTrue(true);
} catch (ConfigDbException e) {
// TODO Auto-generated catch block
@@ -96,21 +146,45 @@ public class UIServiceImplTest {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
+ assertTrue(true);
+ }
+
+ }
+
+ @Test
+ public void testGetApiKey_invalidkey() {
+
+ try {
+ service.getApiKey(dmaapContext, "k56HmWT72J");
+ } catch (NullPointerException e) {
+ // TODO Auto-generated catch block
+ // e.printStackTrace();
+ assertTrue(true);
+ } catch (ConfigDbException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ assertTrue(true);
}
-
+
}
-
+
@Test
public void testGetTopicsTable() {
-
- UIServiceImpl service = new UIServiceImpl();
+
try {
- service.getTopicsTable(new DMaaPContext());
+ List<Topic> topics = new ArrayList<Topic>();
+ topics.add(metatopic);
+ when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);
+ service.getTopicsTable(dmaapContext);
} catch (NullPointerException e) {
// TODO Auto-generated catch block
- //e.printStackTrace();
- assertTrue(true);
+ // e.printStackTrace();
+
} catch (ConfigDbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -121,19 +195,20 @@ public class UIServiceImplTest {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+ assertTrue(true);
+
}
-
+
@Test
public void testGetTopic() {
-
- UIServiceImpl service = new UIServiceImpl();
+
try {
- service.getTopic(new DMaaPContext(), "testTopic");
+ when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);
+ service.getTopic(dmaapContext, "testTopic");
} catch (NullPointerException e) {
// TODO Auto-generated catch block
- //e.printStackTrace();
- assertTrue(true);
+ // e.printStackTrace();
+
} catch (ConfigDbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
@@ -144,7 +219,83 @@ public class UIServiceImplTest {
// TODO Auto-generated catch block
e.printStackTrace();
}
-
+ assertTrue(true);
+ }
+
+ @Test
+ public void testGetTopic_nulltopic() {
+
+ try {
+ when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);
+ service.getTopic(dmaapContext, "testTopic");
+ } catch (NullPointerException e) {
+ // TODO Auto-generated catch block
+ // e.printStackTrace();
+ } catch (ConfigDbException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ assertTrue(true);
+ }
+
}
-
+
+ NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {
+
+ Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));
+
+ @Override
+ public NsaSimpleApiKey createApiKey(String arg0, String arg1)
+ throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {
+ // TODO Auto-generated method stub
+ return new NsaSimpleApiKey(arg0, arg1);
+ }
+
+ @Override
+ public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean deleteApiKey(String arg0) throws ConfigDbException {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {
+ Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();
+ map.put("testkey", new NsaSimpleApiKey("testkey", "password"));
+ map.put("admin", new NsaSimpleApiKey("admin", "password"));
+
+ return map;
+ }
+
+ @Override
+ public Set<String> loadAllKeys() throws ConfigDbException {
+ // TODO Auto-generated method stub
+
+ return keys;
+ }
+
+ @Override
+ public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {
+ if (!keys.contains(arg0)) {
+ return null;
+ }
+ return new NsaSimpleApiKey(arg0, "password");
+ }
+
+ @Override
+ public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {
+ // TODO Auto-generated method stub
+
+ }
+ };
+
}