-/*-\r
- * ============LICENSE_START=======================================================\r
- * ONAP Policy Engine\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package com.att.nsa.dmaap.service;\r
-\r
-import static org.junit.Assert.*;\r
-\r
-import static org.mockito.Matchers.anyString;\r
-import static org.mockito.Matchers.any;\r
-import static org.mockito.Mockito.when;\r
-\r
-import java.io.IOException;\r
-import java.util.ConcurrentModificationException;\r
-\r
-import javax.servlet.ServletOutputStream;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import org.junit.After;\r
-import org.json.JSONArray;\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-import org.junit.Assert;\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-\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
-import com.att.ajsc.beans.PropertiesMapBean;\r
-import org.onap.dmaap.dmf.mr.CambriaApiException;\r
-import org.onap.dmaap.dmf.mr.beans.DMaaPContext;\r
-import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;\r
-import org.onap.dmaap.dmf.mr.beans.TopicBean;\r
-import org.onap.dmaap.dmf.mr.constants.CambriaConstants;\r
-import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;\r
-import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;\r
-import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;\r
-import org.onap.dmaap.dmf.mr.metabroker.Topic;\r
-import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;\r
-import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;\r
-import org.onap.dmaap.dmf.mr.service.TopicService;\r
-import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;\r
-import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;\r
-import com.att.nsa.configs.ConfigDbException;\r
-import com.att.nsa.security.NsaAcl;\r
-import com.att.nsa.security.NsaApiKey;\r
-import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;\r
-import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
-\r
-//@RunWith(MockitoJUnitRunner.class)\r
-@RunWith(PowerMockRunner.class)\r
-@PrepareForTest({ PropertiesMapBean.class })\r
-public class TopicRestServiceTest {\r
-\r
- @InjectMocks\r
- TopicRestService topicRestService;\r
-\r
- @Mock\r
- private TopicService topicService;\r
-\r
- private TopicRestService service = new TopicRestService();\r
- @Mock\r
- private DMaaPErrorMessages errorMessages;\r
-\r
- @Mock\r
- DMaaPContext dmaapContext;\r
-\r
- @Mock\r
- ConfigurationReader configReader;\r
-\r
- @Mock\r
- ServletOutputStream oStream;\r
-\r
- @Mock\r
- DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;\r
-\r
- @Mock\r
- DMaaPAAFAuthenticator dmaapAAFauthenticator;\r
- @Mock\r
- NsaApiKey user;\r
-\r
- @Mock\r
- NsaSimpleApiKey nsaSimpleApiKey;\r
-\r
- @Mock\r
- HttpServletRequest httpServReq;\r
-\r
- @Mock\r
- HttpServletResponse httpServRes;\r
-\r
- @Mock\r
- DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;\r
-\r
- @Mock\r
- Topic createdTopic;\r
-\r
- @Mock\r
- NsaAcl nsaAcl;\r
-\r
- @Mock\r
- JSONObject jsonObj;\r
-\r
- @Mock\r
- JSONArray jsonArray;\r
-\r
- @Before\r
- public void setUp() throws Exception {\r
-\r
- MockitoAnnotations.initMocks(this);\r
- }\r
-\r
- @After\r
- public void tearDown() throws Exception {\r
- }\r
-\r
- @Test(expected = DMaaPAccessDeniedException.class)\r
- public void testGetTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
- when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);\r
- when(httpServReq.getMethod()).thenReturn("HEAD");\r
-\r
- when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);\r
-\r
- topicRestService.getTopics();\r
- }\r
-\r
- @Test\r
- public void testGetTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
- String perms = "namespace" + "|" + "*" + "|" + "view";\r
- when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true);\r
-\r
- when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);\r
-\r
- topicRestService.getTopics();\r
- }\r
-\r
- @Test\r
- public void testGetTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
- String perms = "namespace" + "|" + "*" + "|" + "view";\r
- when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true);\r
-\r
- when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).getTopics(any());\r
- } catch (JSONException | ConfigDbException | IOException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.getTopics();\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test(expected = DMaaPAccessDeniedException.class)\r
- public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- topicRestService.getAllTopics();\r
- }\r
-\r
- @Test\r
- public void testGetAllTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- topicRestService.getAllTopics();\r
- }\r
-\r
- @Test\r
- public void testGetAllTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))\r
- .thenReturn("namespace");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).getAllTopics(any());\r
- } catch (JSONException | ConfigDbException | IOException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.getAllTopics();\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test(expected = DMaaPAccessDeniedException.class)\r
- public void testGetTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
- .thenReturn("enfTopicName");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- topicRestService.getTopic("topicName");\r
- }\r
-\r
- @Test\r
- public void testGetTopic_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, JSONException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
- .thenReturn("enfTopicName");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- topicRestService.getTopic("topicName");\r
- }\r
-\r
- @Test\r
- public void testGetTopic_error() throws DMaaPAccessDeniedException, ConfigDbException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- PowerMockito.mockStatic(PropertiesMapBean.class);\r
-\r
- assertTrue(true);\r
- when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))\r
- .thenReturn("enfTopicName");\r
-\r
- PowerMockito.mockStatic(DMaaPResponseBuilder.class);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(httpServReq.getHeader("Authorization")).thenReturn(null);\r
-\r
- when(dmaapContext.getResponse()).thenReturn(httpServRes);\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).getTopic(any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.getTopic("topicName");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testCreateTopic()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.createTopic(topicBean);\r
- }\r
-\r
- @Test\r
- public void testCreateTopic_error() {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).createTopic(any(), any());\r
- } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.createTopic(topicBean);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new TopicExistsException("error")).when(topicService).createTopic(any(), any());\r
- } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.createTopic(topicBean);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new AccessDeniedException()).when(topicService).createTopic(any(), any());\r
- } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.createTopic(topicBean);\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testDeleteTopic()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.deleteTopic("enfTopicNamePlusExtra");\r
- }\r
-\r
- @Test\r
- public void testDeleteTopic_error()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).deleteTopic(any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.deleteTopic("enfTopicNamePlusExtra");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new AccessDeniedException()).when(topicService).deleteTopic(any(),\r
- any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.deleteTopic("enfTopicNamePlusExtra");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testGetPublishersByTopicName()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.getPublishersByTopicName("enfTopicNamePlusExtra");\r
- }\r
-\r
- @Test\r
- public void testGetPublishersByTopicName_error() {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).getPublishersByTopicName(any(),\r
- any());\r
- } catch (TopicExistsException | ConfigDbException | IOException e) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.getPublishersByTopicName("enfTopicNamePlusExtra");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testPermitPublisherForTopic()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- }\r
-\r
- @Test\r
- public void testPermitPublisherForTopic_error()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).permitPublisherForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new AccessDeniedException()).when(topicService).permitPublisherForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testDenyPublisherForTopic()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- }\r
-\r
- @Test\r
- public void testDenyPublisherForTopic_error()\r
- throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).denyPublisherForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new AccessDeniedException()).when(topicService).denyPublisherForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- }\r
-\r
- @Test\r
- public void testGetConsumersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.getConsumersByTopicName("enfTopicNamePlusExtra");\r
- }\r
-\r
- @Test\r
- public void testGetConsumersByTopicName_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).getConsumersByTopicName(any(),\r
- any());\r
- } catch (TopicExistsException | ConfigDbException | IOException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.getConsumersByTopicName("enfTopicNamePlusExtra");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- }\r
-\r
- @Test\r
- public void testPermitConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).permitConsumerForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
- @Test\r
- public void testPermitConsumerForTopicWithException() throws DMaaPAccessDeniedException, CambriaApiException,\r
- IOException, TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- }\r
-\r
- @Test\r
- public void testDenyConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- }\r
-\r
- @Test\r
- public void testDenyConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,\r
- TopicExistsException, AccessDeniedException {\r
-\r
- Assert.assertNotNull(topicRestService);\r
-\r
- when(dmaapContext.getRequest()).thenReturn(httpServReq);\r
- when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);\r
- when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);\r
- when(dmaapContext.getConfigReader()).thenReturn(configReader);\r
-\r
- TopicBean topicBean = new TopicBean();\r
- topicBean.setTopicName("enfTopicNamePlusExtra");\r
-\r
- try {\r
- PowerMockito.doThrow(new IOException()).when(topicService).denyConsumerForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
-\r
- try {\r
- PowerMockito.doThrow(new AccessDeniedException()).when(topicService).denyConsumerForTopic(any(),\r
- any(), any());\r
- } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException\r
- | DMaaPAccessDeniedException excp) {\r
- assertTrue(false);\r
- }\r
-\r
- try {\r
- topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");\r
- } catch (CambriaApiException excp) {\r
- assertTrue(true);\r
- }\r
- }\r
-\r
-}\r
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+ package org.onap.dmaap.service;
+
+import static org.junit.Assert.*;
+
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.ConcurrentModificationException;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.After;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.junit.Assert;
+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;
+
+import com.att.ajsc.beans.PropertiesMapBean;
+import org.onap.dmaap.dmf.mr.CambriaApiException;
+import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
+import org.onap.dmaap.dmf.mr.beans.DMaaPKafkaMetaBroker;
+import org.onap.dmaap.dmf.mr.beans.TopicBean;
+import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
+import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException;
+import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
+import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
+import org.onap.dmaap.dmf.mr.metabroker.Topic;
+import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;
+import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator;
+import org.onap.dmaap.dmf.mr.service.TopicService;
+import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
+import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
+import com.att.nsa.configs.ConfigDbException;
+import com.att.nsa.security.NsaAcl;
+import com.att.nsa.security.NsaApiKey;
+import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
+import com.att.nsa.security.db.simple.NsaSimpleApiKey;
+
+//@RunWith(MockitoJUnitRunner.class)
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ PropertiesMapBean.class })
+public class TopicRestServiceTest {
+
+ @InjectMocks
+ TopicRestService topicRestService;
+
+ @Mock
+ private TopicService topicService;
+
+ private TopicRestService service = new TopicRestService();
+ @Mock
+ private DMaaPErrorMessages errorMessages;
+
+ @Mock
+ DMaaPContext dmaapContext;
+
+ @Mock
+ ConfigurationReader configReader;
+
+ @Mock
+ ServletOutputStream oStream;
+
+ @Mock
+ DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator;
+
+ @Mock
+ DMaaPAAFAuthenticator dmaapAAFauthenticator;
+ @Mock
+ NsaApiKey user;
+
+ @Mock
+ NsaSimpleApiKey nsaSimpleApiKey;
+
+ @Mock
+ HttpServletRequest httpServReq;
+
+ @Mock
+ HttpServletResponse httpServRes;
+
+ @Mock
+ DMaaPKafkaMetaBroker dmaapKafkaMetaBroker;
+
+ @Mock
+ Topic createdTopic;
+
+ @Mock
+ NsaAcl nsaAcl;
+
+ @Mock
+ JSONObject jsonObj;
+
+ @Mock
+ JSONArray jsonArray;
+
+ @Before
+ public void setUp() throws Exception {
+
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ }
+
+ @Test(expected = DMaaPAccessDeniedException.class)
+ public void testGetTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+ when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker);
+ when(httpServReq.getMethod()).thenReturn("HEAD");
+
+ when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
+
+ topicRestService.getTopics();
+ }
+
+ @Test
+ public void testGetTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+ String perms = "namespace" + "|" + "*" + "|" + "view";
+ when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true);
+
+ when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
+
+ topicRestService.getTopics();
+ }
+
+ @Test
+ public void testGetTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+ String perms = "namespace" + "|" + "*" + "|" + "view";
+ when(dmaapAAFauthenticator.aafAuthentication(httpServReq, perms)).thenReturn(true);
+
+ when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null);
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).getTopics(any());
+ } catch (JSONException | ConfigDbException | IOException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.getTopics();
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test(expected = DMaaPAccessDeniedException.class)
+ public void testGetAllTopics() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ topicRestService.getAllTopics();
+ }
+
+ @Test
+ public void testGetAllTopics_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ topicRestService.getAllTopics();
+ }
+
+ @Test
+ public void testGetAllTopics_error() throws DMaaPAccessDeniedException, TopicExistsException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.namespace.aaf"))
+ .thenReturn("namespace");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).getAllTopics(any());
+ } catch (JSONException | ConfigDbException | IOException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.getAllTopics();
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test(expected = DMaaPAccessDeniedException.class)
+ public void testGetTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
+ .thenReturn("enfTopicName");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn("Authorization");
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ topicRestService.getTopic("topicName");
+ }
+
+ @Test
+ public void testGetTopic_nullAuth() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
+ .thenReturn("enfTopicName");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ topicRestService.getTopic("topicName");
+ }
+
+ @Test
+ public void testGetTopic_error() throws DMaaPAccessDeniedException, ConfigDbException {
+
+ Assert.assertNotNull(topicRestService);
+
+ PowerMockito.mockStatic(PropertiesMapBean.class);
+
+ assertTrue(true);
+ when(PropertiesMapBean.getProperty(CambriaConstants.msgRtr_prop, "enforced.topic.name.AAF"))
+ .thenReturn("enfTopicName");
+
+ PowerMockito.mockStatic(DMaaPResponseBuilder.class);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(httpServReq.getHeader("Authorization")).thenReturn(null);
+
+ when(dmaapContext.getResponse()).thenReturn(httpServRes);
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).getTopic(any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.getTopic("topicName");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testCreateTopic()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.createTopic(topicBean);
+ }
+
+ @Test
+ public void testCreateTopic_error() {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).createTopic(any(), any());
+ } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ } catch (CambriaApiException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.createTopic(topicBean);
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new TopicExistsException("error")).when(topicService).createTopic(any(), any());
+ } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ } catch (CambriaApiException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.createTopic(topicBean);
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new AccessDeniedException()).when(topicService).createTopic(any(), any());
+ } catch (TopicExistsException | IOException | AccessDeniedException | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ } catch (CambriaApiException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.createTopic(topicBean);
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testDeleteTopic()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.deleteTopic("enfTopicNamePlusExtra");
+ }
+
+ @Test
+ public void testDeleteTopic_error()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).deleteTopic(any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.deleteTopic("enfTopicNamePlusExtra");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new AccessDeniedException()).when(topicService).deleteTopic(any(),
+ any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.deleteTopic("enfTopicNamePlusExtra");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testGetPublishersByTopicName()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.getPublishersByTopicName("enfTopicNamePlusExtra");
+ }
+
+ @Test
+ public void testGetPublishersByTopicName_error() {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).getPublishersByTopicName(any(),
+ any());
+ } catch (TopicExistsException | ConfigDbException | IOException e) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.getPublishersByTopicName("enfTopicNamePlusExtra");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testPermitPublisherForTopic()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ }
+
+ @Test
+ public void testPermitPublisherForTopic_error()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).permitPublisherForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new AccessDeniedException()).when(topicService).permitPublisherForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.permitPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testDenyPublisherForTopic()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ }
+
+ @Test
+ public void testDenyPublisherForTopic_error()
+ throws DMaaPAccessDeniedException, CambriaApiException, IOException, TopicExistsException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).denyPublisherForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new AccessDeniedException()).when(topicService).denyPublisherForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.denyPublisherForTopic("enfTopicNamePlusExtra", "producerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ }
+
+ @Test
+ public void testGetConsumersByTopicName() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.getConsumersByTopicName("enfTopicNamePlusExtra");
+ }
+
+ @Test
+ public void testGetConsumersByTopicName_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).getConsumersByTopicName(any(),
+ any());
+ } catch (TopicExistsException | ConfigDbException | IOException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.getConsumersByTopicName("enfTopicNamePlusExtra");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testPermitConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ }
+
+ @Test
+ public void testPermitConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(any())).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).permitConsumerForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testPermitConsumerForTopicWithException() throws DMaaPAccessDeniedException, CambriaApiException,
+ IOException, TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.permitConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ }
+
+ @Test
+ public void testDenyConsumerForTopic() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ }
+
+ @Test
+ public void testDenyConsumerForTopic_error() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, AccessDeniedException {
+
+ Assert.assertNotNull(topicRestService);
+
+ when(dmaapContext.getRequest()).thenReturn(httpServReq);
+ when(dmaaPAuthenticator.authenticate(dmaapContext)).thenReturn(nsaSimpleApiKey);
+ when(configReader.getfSecurityManager()).thenReturn(dmaaPAuthenticator);
+ when(dmaapContext.getConfigReader()).thenReturn(configReader);
+
+ TopicBean topicBean = new TopicBean();
+ topicBean.setTopicName("enfTopicNamePlusExtra");
+
+ try {
+ PowerMockito.doThrow(new IOException()).when(topicService).denyConsumerForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+
+ try {
+ PowerMockito.doThrow(new AccessDeniedException()).when(topicService).denyConsumerForTopic(any(),
+ any(), any());
+ } catch (TopicExistsException | ConfigDbException | IOException | AccessDeniedException
+ | DMaaPAccessDeniedException excp) {
+ assertTrue(false);
+ }
+
+ try {
+ topicRestService.denyConsumerForTopic("enfTopicNamePlusExtra", "consumerID");
+ } catch (CambriaApiException excp) {
+ assertTrue(true);
+ }
+ }
+
+}