package com.att.nsa.cambria.service.impl;\r
\r
import static org.junit.Assert.*;\r
+import static org.mockito.Mockito.when;\r
\r
import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
\r
+import com.att.nsa.cambria.backends.ConsumerFactory;\r
import com.att.nsa.cambria.beans.DMaaPContext;\r
+import com.att.nsa.cambria.beans.DMaaPKafkaMetaBroker;\r
+import com.att.nsa.cambria.metabroker.Topic;\r
+import com.att.nsa.cambria.security.DMaaPAuthenticatorImpl;\r
+import com.att.nsa.cambria.utils.ConfigurationReader;\r
+import com.att.nsa.cambria.utils.DMaaPResponseBuilder;\r
import com.att.nsa.configs.ConfigDbException;\r
-import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
+import com.att.nsa.security.db.NsaApiDb;\r
+import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
\r
import org.junit.After;\r
import org.junit.Before;\r
import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.InjectMocks;\r
+import org.mockito.Mock;\r
+import org.mockito.MockitoAnnotations;\r
+import org.powermock.api.mockito.PowerMockito;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
\r
+@RunWith(PowerMockRunner.class)\r
+@PrepareForTest({ DMaaPAuthenticatorImpl.class, DMaaPResponseBuilder.class })\r
public class UIServiceImplTest {\r
\r
+ @InjectMocks\r
+ UIServiceImpl service;\r
+\r
+ @Mock\r
+ DMaaPContext dmaapContext;\r
+ @Mock\r
+ ConsumerFactory factory;\r
+\r
+ @Mock\r
+ ConfigurationReader configReader;\r
+\r
+ @Mock\r
+ DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;\r
+\r
+ @Mock\r
+ Topic metatopic;\r
+\r
@Before\r
public void setUp() throws Exception {\r
+ MockitoAnnotations.initMocks(this);\r
+ PowerMockito.mockStatic(DMaaPAuthenticatorImpl.class);\r
+ NsaSimpleApiKey user = new NsaSimpleApiKey("admin", "password");\r
+\r
+ PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
+ PowerMockito.when(configReader.getfConsumerFactory()).thenReturn(factory);\r
+\r
+ PowerMockito.when(configReader.getfApiKeyDb()).thenReturn(fApiKeyDb);\r
+ PowerMockito.when(DMaaPAuthenticatorImpl.getAuthenticatedUser(dmaapContext)).thenReturn(user);\r
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
+ PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
}\r
\r
@After\r
\r
@Test\r
public void testHello() {\r
- \r
- UIServiceImpl service = new UIServiceImpl();\r
+\r
try {\r
- service.hello(new DMaaPContext());\r
+ service.hello(dmaapContext);\r
} catch (IOException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
- } \r
- \r
+ }\r
+\r
String trueValue = "True";\r
assertTrue(trueValue.equalsIgnoreCase("True"));\r
- \r
+\r
}\r
- \r
+\r
@Test\r
public void testGetApiKeysTable() {\r
- \r
- UIServiceImpl service = new UIServiceImpl();\r
+\r
try {\r
- service.getApiKeysTable(new DMaaPContext());\r
+ service.getApiKeysTable(dmaapContext);\r
} catch (NullPointerException e) {\r
// TODO Auto-generated catch block\r
- //e.printStackTrace();\r
+ // e.printStackTrace();\r
assertTrue(true);\r
} catch (ConfigDbException e) {\r
// TODO Auto-generated catch block\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
- \r
+ assertTrue(true);\r
+\r
}\r
- \r
+\r
@Test\r
public void testGetApiKey() {\r
- \r
- UIServiceImpl service = new UIServiceImpl();\r
+\r
try {\r
- service.getApiKey(new DMaaPContext(), "k56HmWT72J");\r
+ service.getApiKey(dmaapContext, "admin");\r
} catch (NullPointerException e) {\r
// TODO Auto-generated catch block\r
- //e.printStackTrace();\r
+ // e.printStackTrace();\r
assertTrue(true);\r
} catch (ConfigDbException e) {\r
// TODO Auto-generated catch block\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
} catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ assertTrue(true);\r
+ }\r
+\r
+ }\r
+\r
+ @Test\r
+ public void testGetApiKey_invalidkey() {\r
+\r
+ try {\r
+ service.getApiKey(dmaapContext, "k56HmWT72J");\r
+ } catch (NullPointerException e) {\r
+ // TODO Auto-generated catch block\r
+ // e.printStackTrace();\r
+ assertTrue(true);\r
+ } catch (ConfigDbException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
+ } catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ assertTrue(true);\r
}\r
- \r
+\r
}\r
- \r
+\r
@Test\r
public void testGetTopicsTable() {\r
- \r
- UIServiceImpl service = new UIServiceImpl();\r
+\r
try {\r
- service.getTopicsTable(new DMaaPContext());\r
+ List<Topic> topics = new ArrayList<Topic>();\r
+ topics.add(metatopic);\r
+ when(dmaapKafkaMetaBroker.getAllTopics()).thenReturn(topics);\r
+ service.getTopicsTable(dmaapContext);\r
} catch (NullPointerException e) {\r
// TODO Auto-generated catch block\r
- //e.printStackTrace();\r
- assertTrue(true);\r
+ // e.printStackTrace();\r
+\r
} catch (ConfigDbException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
- \r
+ assertTrue(true);\r
+\r
}\r
- \r
+\r
@Test\r
public void testGetTopic() {\r
- \r
- UIServiceImpl service = new UIServiceImpl();\r
+\r
try {\r
- service.getTopic(new DMaaPContext(), "testTopic");\r
+ when(dmaapKafkaMetaBroker.getTopic("testTopic")).thenReturn(metatopic);\r
+ service.getTopic(dmaapContext, "testTopic");\r
} catch (NullPointerException e) {\r
// TODO Auto-generated catch block\r
- //e.printStackTrace();\r
- assertTrue(true);\r
+ // e.printStackTrace();\r
+\r
} catch (ConfigDbException e) {\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
// TODO Auto-generated catch block\r
e.printStackTrace();\r
}\r
- \r
+ assertTrue(true);\r
+ }\r
+\r
+ @Test\r
+ public void testGetTopic_nulltopic() {\r
+\r
+ try {\r
+ when(dmaapKafkaMetaBroker.getTopic("topicNamespace.topic")).thenReturn(null);\r
+ service.getTopic(dmaapContext, "testTopic");\r
+ } catch (NullPointerException e) {\r
+ // TODO Auto-generated catch block\r
+ // e.printStackTrace();\r
+ } catch (ConfigDbException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (IOException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } catch (Exception e) {\r
+ // TODO Auto-generated catch block\r
+ assertTrue(true);\r
+ }\r
+\r
}\r
- \r
+\r
+ NsaApiDb<NsaSimpleApiKey> fApiKeyDb = new NsaApiDb<NsaSimpleApiKey>() {\r
+\r
+ Set<String> keys = new HashSet<>(Arrays.asList("testkey", "admin"));\r
+\r
+ @Override\r
+ public NsaSimpleApiKey createApiKey(String arg0, String arg1)\r
+ throws com.att.nsa.security.db.NsaApiDb.KeyExistsException, ConfigDbException {\r
+ // TODO Auto-generated method stub\r
+ return new NsaSimpleApiKey(arg0, arg1);\r
+ }\r
+\r
+ @Override\r
+ public boolean deleteApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {\r
+ // TODO Auto-generated method stub\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public boolean deleteApiKey(String arg0) throws ConfigDbException {\r
+ // TODO Auto-generated method stub\r
+ return false;\r
+ }\r
+\r
+ @Override\r
+ public Map<String, NsaSimpleApiKey> loadAllKeyRecords() throws ConfigDbException {\r
+ Map<String, NsaSimpleApiKey> map = new HashMap<String, NsaSimpleApiKey>();\r
+ map.put("testkey", new NsaSimpleApiKey("testkey", "password"));\r
+ map.put("admin", new NsaSimpleApiKey("admin", "password"));\r
+\r
+ return map;\r
+ }\r
+\r
+ @Override\r
+ public Set<String> loadAllKeys() throws ConfigDbException {\r
+ // TODO Auto-generated method stub\r
+\r
+ return keys;\r
+ }\r
+\r
+ @Override\r
+ public NsaSimpleApiKey loadApiKey(String arg0) throws ConfigDbException {\r
+ if (!keys.contains(arg0)) {\r
+ return null;\r
+ }\r
+ return new NsaSimpleApiKey(arg0, "password");\r
+ }\r
+\r
+ @Override\r
+ public void saveApiKey(NsaSimpleApiKey arg0) throws ConfigDbException {\r
+ // TODO Auto-generated method stub\r
+\r
+ }\r
+ };\r
+\r
}\r