diff options
Diffstat (limited to 'src/test/java/org')
38 files changed, 4937 insertions, 0 deletions
diff --git a/src/test/java/org/onap/dmaap/DMaaPCambriaExceptionMapperTest.java b/src/test/java/org/onap/dmaap/DMaaPCambriaExceptionMapperTest.java new file mode 100644 index 0000000..a66da07 --- /dev/null +++ b/src/test/java/org/onap/dmaap/DMaaPCambriaExceptionMapperTest.java @@ -0,0 +1,86 @@ +/*- + * ============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; + +import static org.junit.Assert.assertTrue; + +import org.json.JSONObject; +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.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; + +import org.onap.dmaap.dmf.mr.CambriaApiException; +import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages; +import org.onap.dmaap.dmf.mr.exception.ErrorResponse; +@RunWith(PowerMockRunner.class) +public class DMaaPCambriaExceptionMapperTest { + + @InjectMocks + DMaaPCambriaExceptionMapper mapper; + + @Mock + private ErrorResponse errRes; + + @Mock + private DMaaPErrorMessages msgs; + + @Mock + CambriaApiException exc; + + @Mock + JSONObject json; + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testToResponse() { + try { + mapper.toResponse(null); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + + @Test + public void testToResponseCambriaApiException2() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new CambriaApiException(404,"Not found")); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/DMaaPWebExceptionMapperTest.java b/src/test/java/org/onap/dmaap/DMaaPWebExceptionMapperTest.java new file mode 100644 index 0000000..78f67c2 --- /dev/null +++ b/src/test/java/org/onap/dmaap/DMaaPWebExceptionMapperTest.java @@ -0,0 +1,142 @@ +/*- + * ============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; + +import static org.junit.Assert.*; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.InternalServerErrorException; +import javax.ws.rs.NotAllowedException; +import javax.ws.rs.NotAuthorizedException; +import javax.ws.rs.NotFoundException; +import javax.ws.rs.ServiceUnavailableException; + +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.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; + +import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages; + +@RunWith(PowerMockRunner.class) +public class DMaaPWebExceptionMapperTest { + + @InjectMocks + DMaaPWebExceptionMapper mapper; + + @Mock + DMaaPErrorMessages msgs; + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testToResponse() { + + try { + mapper.toResponse(null); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseNotFoundException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new NotFoundException()); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseInternalServerErrorException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new InternalServerErrorException()); + + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseNotAuthorizedException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new NotAuthorizedException("Error", "Error")); + + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseBadRequestException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new BadRequestException()); + + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseNotAllowedException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new NotAllowedException("Not Allowed")); + + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testToResponseServiceUnavailableException() { + PowerMockito.when(msgs.getNotFound()).thenReturn("Not found"); + try { + mapper.toResponse(new ServiceUnavailableException()); + + } catch (NullPointerException e) { + assertTrue(true); + } + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/DummyTest.java b/src/test/java/org/onap/dmaap/DummyTest.java new file mode 100644 index 0000000..8b01f94 --- /dev/null +++ b/src/test/java/org/onap/dmaap/DummyTest.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 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========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ + package org.onap.dmaap; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class DummyTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void test() { + assertTrue("Dummy test case", true); + } + +} diff --git a/src/test/java/org/onap/dmaap/HelloWorldTest.java b/src/test/java/org/onap/dmaap/HelloWorldTest.java new file mode 100644 index 0000000..0067b8f --- /dev/null +++ b/src/test/java/org/onap/dmaap/HelloWorldTest.java @@ -0,0 +1,52 @@ +/*- + * ============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; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class HelloWorldTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testToResponse() { + + HelloWorld hello = new HelloWorld(); + + try { + hello.speak(null); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/JUnitTestSuite.java new file mode 100644 index 0000000..d20d0d6 --- /dev/null +++ b/src/test/java/org/onap/dmaap/JUnitTestSuite.java @@ -0,0 +1,43 @@ +/*- + * ============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; + +import junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ DMaaPCambriaExceptionMapperTest.class, DMaaPWebExceptionMapper.class, + JaxrsEchoServiceTest.class, HelloWorldTest.class, JaxrsUserServiceTest.class }) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/JaxrsEchoServiceTest.java b/src/test/java/org/onap/dmaap/JaxrsEchoServiceTest.java new file mode 100644 index 0000000..94a6b96 --- /dev/null +++ b/src/test/java/org/onap/dmaap/JaxrsEchoServiceTest.java @@ -0,0 +1,60 @@ +/*- + * ============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; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class JaxrsEchoServiceTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testPing() { + + JaxrsEchoService service = new JaxrsEchoService(); + + service.ping("hello"); + + assertTrue(true); + + } + + @Test + public void testGetProperty() { + + JaxrsEchoService service = new JaxrsEchoService(); + + service.getProperty("filename", "hello"); + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/JaxrsUserServiceTest.java b/src/test/java/org/onap/dmaap/JaxrsUserServiceTest.java new file mode 100644 index 0000000..7d8ba47 --- /dev/null +++ b/src/test/java/org/onap/dmaap/JaxrsUserServiceTest.java @@ -0,0 +1,50 @@ +/*- + * ============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; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class JaxrsUserServiceTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testLookupUser() { + + JaxrsUserService service = new JaxrsUserService(); + + service.lookupUser("userid"); + + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/TestRunner.java b/src/test/java/org/onap/dmaap/TestRunner.java new file mode 100644 index 0000000..4542566 --- /dev/null +++ b/src/test/java/org/onap/dmaap/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} diff --git a/src/test/java/org/onap/dmaap/filemonitor/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/filemonitor/JUnitTestSuite.java new file mode 100644 index 0000000..9c87f24 --- /dev/null +++ b/src/test/java/org/onap/dmaap/filemonitor/JUnitTestSuite.java @@ -0,0 +1,43 @@ +/*- + * ============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.filemonitor; + +import junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ ServicePropertiesListenerTest.class, ServicePropertiesMapTest.class, + ServicePropertyServiceTest.class, }) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesListenerTest.java b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesListenerTest.java new file mode 100644 index 0000000..cae0c68 --- /dev/null +++ b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesListenerTest.java @@ -0,0 +1,57 @@ +/*- + * ============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.filemonitor; + +import static org.junit.Assert.*; + +import java.io.File; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ServicePropertiesListenerTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + //@Test + public void testUpdate() {/* + + ServicePropertiesListener listener = new ServicePropertiesListener(); + + try { + listener.update(new File(":/file")); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assertTrue(true); + + */} + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesMapTest.java b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesMapTest.java new file mode 100644 index 0000000..1e8c9d7 --- /dev/null +++ b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertiesMapTest.java @@ -0,0 +1,128 @@ +/*- + * ============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.filemonitor; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; + +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.powermock.api.mockito.PowerMockito; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(PowerMockRunner.class) +public class ServicePropertiesMapTest { + + @InjectMocks + ServicePropertiesMap map; + + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + /*@Test + public void testRefresh() { + + try { + map.refresh(new File(":/file")); + } catch (Exception e) { + e.printStackTrace(); + } + + assertTrue(true); + + } + + @Test() + public void testRefreshJsonFile() throws Exception { + //Path resourceDirectory = Paths.get("src/test/resources"); + // map.refresh(new File(resourceDirectory+"\\"+"test.json")); + ClassLoader classLoader = getClass().getClassLoader(); + map.refresh(new File(classLoader.getResource("test.json").getFile())); + + assertTrue(true); + } + + @Test + public void testRefreshPropsFile() throws Exception { + Path resourceDirectory = Paths.get("src/test/resources"); + map.refresh(new File(resourceDirectory+"\\"+"test.properties")); + ClassLoader classLoader = getClass().getClassLoader(); + map.refresh(new File(classLoader.getResource("test.json").getFile())); + assertTrue(true); + }*/ + + @Test + public void testGetProperty() { + + try { + map.getProperty("filename", "propertykey"); + } catch (Exception e) { + e.printStackTrace(); + } + + assertTrue(true); + } + + @Test + public void testGetProperties() { + + try { + map.getProperties("filename"); + } catch (Exception e) { + e.printStackTrace(); + } + + assertTrue(true); + + } + + @Test + public void testIfNullThenEmpty() { + + try { + map.getProperties("filename"); + } catch (Exception e) { + e.printStackTrace(); + } + + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/filemonitor/ServicePropertyServiceTest.java b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertyServiceTest.java new file mode 100644 index 0000000..461ad60 --- /dev/null +++ b/src/test/java/org/onap/dmaap/filemonitor/ServicePropertyServiceTest.java @@ -0,0 +1,153 @@ +/*- + * ============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.filemonitor; + +import static org.junit.Assert.*; + +import java.io.File; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ServicePropertyServiceTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testInit() { + + ServicePropertyService service = new ServicePropertyService(); + try { + service.init(); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + assertTrue(true); + + } + + @Test + public void testsetLoadOnStartup() { + + ServicePropertyService service = new ServicePropertyService(); + try { + service.setLoadOnStartup(true); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + assertTrue(true); + + } + + @Test + public void testSetSsfFileMonitorPollingInterval() { + + ServicePropertyService service = new ServicePropertyService(); + try { + service.setSsfFileMonitorPollingInterval("interval"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + assertTrue(true); + + } + + @Test + public void testSetSsfFileMonitorThreadpoolSize() { + + ServicePropertyService service = new ServicePropertyService(); + try { + service.setSsfFileMonitorThreadpoolSize("threadPoolSize"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + assertTrue(true); + + } + + @Test + public void testIsLoadOnStartup() { + ServicePropertyService service = new ServicePropertyService(); + service.isLoadOnStartup(); + assertTrue(true); + + } + + @Test + public void testGetSsfFileMonitorPollingInterval() { + ServicePropertyService service = new ServicePropertyService(); + service.getSsfFileMonitorPollingInterval(); + assertTrue(true); + + } + + @Test + public void testGetSsfFileMonitorThreadpoolSize() { + ServicePropertyService service = new ServicePropertyService(); + service.getSsfFileMonitorThreadpoolSize(); + assertTrue(true); + + } + + @Test + public void testGetFileChangedListener() { + ServicePropertyService service = new ServicePropertyService(); + service.getFileChangedListener(); + assertTrue(true); + + } + + @Test + public void testSetFileChangedListener() { + ServicePropertyService service = new ServicePropertyService(); + service.setFileChangedListener(null); + assertTrue(true); + + } + + @Test + public void testGetFilePropertiesMap() { + ServicePropertyService service = new ServicePropertyService(); + service.getFilePropertiesMap(); + assertTrue(true); + + } + + @Test + public void testSetFilePropertiesMap() { + ServicePropertyService service = new ServicePropertyService(); + service.setFilePropertiesMap(null); + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/filemonitor/TestRunner.java b/src/test/java/org/onap/dmaap/filemonitor/TestRunner.java new file mode 100644 index 0000000..1af59c2 --- /dev/null +++ b/src/test/java/org/onap/dmaap/filemonitor/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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.filemonitor; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} diff --git a/src/test/java/org/onap/dmaap/mmagent/CreateMirrorMakerTest.java b/src/test/java/org/onap/dmaap/mmagent/CreateMirrorMakerTest.java new file mode 100644 index 0000000..bad987d --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/CreateMirrorMakerTest.java @@ -0,0 +1,82 @@ +/*- + * ============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.mmagent; + +import org.onap.dmaap.dmf.mr.CambriaApiException; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + + +public class CreateMirrorMakerTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetCreateMirrorMaker() { + + CreateMirrorMaker mMaker = new CreateMirrorMaker(); + mMaker.getCreateMirrorMaker(); + + assertTrue(true); + + } + + @Test + public void testSetCreateMirrorMaker() throws CambriaApiException { + + CreateMirrorMaker mMaker = new CreateMirrorMaker(); + mMaker.setCreateMirrorMaker(new MirrorMaker()); + + assertTrue(true); + + } + + @Test + public void testGetMessageID() { + + CreateMirrorMaker mMaker = new CreateMirrorMaker(); + mMaker.getMessageID(); + + assertTrue(true); + + } + + @Test + public void testSetMessageID() { + + CreateMirrorMaker mMaker = new CreateMirrorMaker(); + mMaker.setMessageID("messageID"); + + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/mmagent/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/mmagent/JUnitTestSuite.java new file mode 100644 index 0000000..909ecd8 --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/JUnitTestSuite.java @@ -0,0 +1,43 @@ +/*- + * ============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.mmagent; + +import junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ CreateMirrorMakerTest.class, MirrorMakerTest.class, + UpdateMirrorMakerTest.class, UpdateWhiteListTest.class,}) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/mmagent/MirrorMakerTest.java b/src/test/java/org/onap/dmaap/mmagent/MirrorMakerTest.java new file mode 100644 index 0000000..192ed1d --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/MirrorMakerTest.java @@ -0,0 +1,142 @@ +/*- + * ============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.mmagent; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class MirrorMakerTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetStatus() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.getStatus(); + + assertTrue(true); + + } + + @Test + public void testSetStatus() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.setStatus("status"); + + assertTrue(true); + + } + + @Test + public void testGetName() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.getName(); + + assertTrue(true); + + } + + @Test + public void testSetName() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.setName("name"); + + assertTrue(true); + + } + + + @Test + public void testGetConsumer() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.getConsumer(); + + assertTrue(true); + + } + + @Test + public void testSetConsumer() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.setConsumer("consumer"); + + assertTrue(true); + + } + + @Test + public void testGetProducer() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.getProducer(); + + assertTrue(true); + + } + + @Test + public void testSetProducer() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.setProducer("producer"); + + assertTrue(true); + + } + + + @Test + public void testGetWhitelist() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.getWhitelist(); + + assertTrue(true); + + } + + @Test + public void testSetWhitelist() { + + MirrorMaker mMaker = new MirrorMaker(); + mMaker.setWhitelist("whitelist"); + + assertTrue(true); + + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/mmagent/TestRunner.java b/src/test/java/org/onap/dmaap/mmagent/TestRunner.java new file mode 100644 index 0000000..3e4ece3 --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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.mmagent; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} diff --git a/src/test/java/org/onap/dmaap/mmagent/UpdateMirrorMakerTest.java b/src/test/java/org/onap/dmaap/mmagent/UpdateMirrorMakerTest.java new file mode 100644 index 0000000..1826026 --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/UpdateMirrorMakerTest.java @@ -0,0 +1,81 @@ +/*- + * ============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.mmagent; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class UpdateMirrorMakerTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetUpdateMirrorMaker() { + + UpdateMirrorMaker mMaker = new UpdateMirrorMaker(); + mMaker.getUpdateMirrorMaker(); + + assertTrue(true); + + } + + @Test + public void testSetUpdateMirrorMaker() { + + UpdateMirrorMaker mMaker = new UpdateMirrorMaker(); + mMaker.setUpdateMirrorMaker(new MirrorMaker()); + + assertTrue(true); + + } + + @Test + public void testGetMessageID() { + + UpdateMirrorMaker mMaker = new UpdateMirrorMaker(); + mMaker.getMessageID(); + + assertTrue(true); + + } + + @Test + public void testSetMessageID() { + + UpdateMirrorMaker mMaker = new UpdateMirrorMaker(); + mMaker.setMessageID("messageID"); + + assertTrue(true); + + } + + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/mmagent/UpdateWhiteListTest.java b/src/test/java/org/onap/dmaap/mmagent/UpdateWhiteListTest.java new file mode 100644 index 0000000..75a9104 --- /dev/null +++ b/src/test/java/org/onap/dmaap/mmagent/UpdateWhiteListTest.java @@ -0,0 +1,85 @@ +/*- + * ============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.mmagent; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class UpdateWhiteListTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetUpdateWhiteList() { + + UpdateWhiteList wList = new UpdateWhiteList(); + wList.getUpdateWhiteList(); + + assertTrue(true); + + } + + @Test + public void testSetUpdateWhiteList() { + + UpdateWhiteList wList = new UpdateWhiteList(); + wList.setUpdateWhiteList(new MirrorMaker()); + + assertTrue(true); + + } + + @Test + public void testGetMessageID() { + + UpdateWhiteList wList = new UpdateWhiteList(); + wList.getMessageID(); + + assertTrue(true); + + } + + @Test + public void testSetMessageID() { + + UpdateWhiteList wList = new UpdateWhiteList(); + wList.setMessageID("messageID"); + + assertTrue(true); + + } + + + + + + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/service/AdminRestServiceTest.java b/src/test/java/org/onap/dmaap/service/AdminRestServiceTest.java new file mode 100644 index 0000000..0f99ff5 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/AdminRestServiceTest.java @@ -0,0 +1,295 @@ +/*- + * ============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 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.onap.dmaap.dmf.mr.CambriaApiException; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.dmaap.dmf.mr.beans.DMaaPContext; + +import java.io.IOException; +import java.util.Enumeration; +import org.onap.dmaap.dmf.mr.service.AdminService; +import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; +import org.onap.dmaap.dmf.mr.beans.DMaaPContext; +import org.onap.dmaap.dmf.mr.utils.ConfigurationReader; +import org.powermock.core.classloader.annotations.PrepareForTest; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ ServiceUtil.class }) +public class AdminRestServiceTest { + + @InjectMocks + AdminRestService adminRestService; + + @Mock + AdminService adminService; + + @Mock + DMaaPContext dmaapContext; + + @Mock + HttpServletRequest httpServReq; + @Mock + private HttpServletResponse response; + + @Mock + Enumeration headerNames; + @Mock + private DMaaPContext dmaaPContext; + @Mock + private ConfigurationReader configReader; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetConsumerCache() throws CambriaApiException, AccessDeniedException { + adminRestService.getConsumerCache(); + + } + + @Test + public void testGetConsumerCache_error() throws CambriaApiException, AccessDeniedException, IOException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new IOException("error")).when(adminService).showConsumerCache(dmaaPContext); + try { + adminRestService.getConsumerCache(); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testDropConsumerCache() throws CambriaApiException, AccessDeniedException { + adminRestService.dropConsumerCache(); + + } + + @Test + public void testDropConsumerCach_error() throws CambriaApiException, AccessDeniedException ,IOException{ + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new IOException("error")).when(adminService).dropConsumerCache(dmaaPContext); + try { + adminRestService.dropConsumerCache(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + + } + @Test + public void testDropConsumerCach_error1() throws CambriaApiException, AccessDeniedException,IOException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).dropConsumerCache(dmaaPContext); + try { + adminRestService.dropConsumerCache(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + + } + + @Test + public void testGetBlacklist() throws CambriaApiException, AccessDeniedException { + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeaderNames()).thenReturn(headerNames); + when(headerNames.nextElement()).thenReturn("key"); + when(httpServReq.getHeader("key")).thenReturn("value"); + + adminRestService.getBlacklist(); + + } + + //@Test + public void testGetBlacklist_error() throws CambriaApiException, AccessDeniedException,IOException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new IOException("error")).when(adminService).getBlacklist(dmaaPContext); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeaderNames()).thenReturn(headerNames); + when(headerNames.nextElement()).thenReturn("key"); + when(httpServReq.getHeader("key")).thenReturn("value"); + when(headerNames.hasMoreElements()).thenReturn(false); + try { + adminRestService.getBlacklist(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + } + + ////@Test + public void testGetBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).getBlacklist(dmaaPContext); + when(dmaapContext.getRequest()).thenReturn(httpServReq); + when(httpServReq.getHeaderNames()).thenReturn(headerNames); + when(headerNames.nextElement()).thenReturn("key"); + when(httpServReq.getHeader("key")).thenReturn("value"); + when(headerNames.hasMoreElements()).thenReturn(false); + try { + adminRestService.getBlacklist(); + } + catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testAddToBlacklist() throws CambriaApiException, AccessDeniedException { + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + + adminRestService.addToBlacklist("120.120.120.120"); + + } + + @Test + public void testAddToBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException { + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120"); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + try { + adminRestService.addToBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testAddToBlacklist_error1() throws CambriaApiException, AccessDeniedException,IOException, ConfigDbException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new IOException("error")).when(adminService).addToBlacklist(dmaaPContext,"120.120.120.120"); + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + try { + adminRestService.addToBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException { + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + + adminRestService.removeFromBlacklist("120.120.120.120"); + + } + + @Test + public void testRemoveFromBlacklist_error() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException{ + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new IOException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120"); + + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + try { + + adminRestService.removeFromBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + catch (AccessDeniedException e) { + assertTrue(true); + } + catch (ConfigDbException e) { + assertTrue(true); + } + + } + + @Test + public void testRemoveFromBlacklist_error1() throws CambriaApiException, AccessDeniedException, ConfigDbException,IOException { + + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaaPContext); + PowerMockito.doThrow(new AccessDeniedException("error")).when(adminService).removeFromBlacklist(dmaaPContext,"120.120.120.120"); + + + when(dmaapContext.getRequest()).thenReturn(httpServReq); + try { + + adminRestService.removeFromBlacklist("120.120.120.120"); + }catch (CambriaApiException e) { + assertTrue(true); + } + catch (AccessDeniedException e) { + assertTrue(true); + } + catch (ConfigDbException e) { + assertTrue(true); + } + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/service/ApiKeysRestServiceTest.java b/src/test/java/org/onap/dmaap/service/ApiKeysRestServiceTest.java new file mode 100644 index 0000000..20438f3 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/ApiKeysRestServiceTest.java @@ -0,0 +1,232 @@ +/*- + * ============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 java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.onap.dmaap.dmf.mr.CambriaApiException; +import org.onap.dmaap.dmf.mr.beans.ApiKeyBean; +import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.json.JSONException; + +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.dmaap.dmf.mr.beans.DMaaPContext; + +import org.onap.dmaap.dmf.mr.utils.ConfigurationReader; +import org.onap.dmaap.dmf.mr.service.ApiKeysService; +import org.onap.dmaap.dmf.mr.utils.ConfigurationReader; +import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.security.db.NsaApiDb.KeyExistsException; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ ServiceUtil.class }) +public class ApiKeysRestServiceTest { + + @InjectMocks + private ApiKeysRestService service; + + @Mock + ApiKeysService apiKeyService; + + @Mock + DMaaPContext dmaapContext; + + @Mock + HttpServletRequest httpServReq; + @Mock + private HttpServletResponse response; + @Mock + private ConfigurationReader configReader; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetAllApiKeys() { + + try { + service.getAllApiKeys(); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testGetAllApiKeys_error() throws ConfigDbException, IOException { + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext); + PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getAllApiKeys(dmaapContext); + try { + service.getAllApiKeys(); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + assertTrue(true); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testGetApiKey() { + + try { + service.getApiKey("apikeyName"); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testGetApiKey_error() throws ConfigDbException, IOException { + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext); + PowerMockito.doThrow(new IOException("error")).when(apiKeyService).getApiKey(dmaapContext, "apikeyName"); + + try { + service.getApiKey("apikeyName"); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + assertTrue(true); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testCreateApiKey() { + + try { + service.createApiKey(new ApiKeyBean("hs647a@att.com", "test apikey")); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testCreateApiKey_error() + throws CambriaApiException, JSONException, KeyExistsException, ConfigDbException, IOException { + + ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey"); + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext); + PowerMockito.doThrow(new IOException("error")).when(apiKeyService).createApiKey(dmaapContext, bean); + + try { + service.createApiKey(bean); + } catch (CambriaApiException e) { + assertTrue(true); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testUpdateApiKey() { + + try { + service.updateApiKey("apikeyName", new ApiKeyBean("hs647a@att.com", "test apikey")); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testUpdateApiKey_error() throws CambriaApiException, JSONException, KeyExistsException, + ConfigDbException, IOException, AccessDeniedException { + + ApiKeyBean bean = new ApiKeyBean("test@onap.com", "test apikey"); + PowerMockito.mockStatic(ServiceUtil.class); + PowerMockito.when(ServiceUtil.getDMaaPContext(configReader, httpServReq, response)).thenReturn(dmaapContext); + PowerMockito.doThrow(new IOException("error")).when(apiKeyService).updateApiKey(dmaapContext, "apikeyName", + bean); + try { + service.updateApiKey("apikeyName", bean); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + + @Test + public void testDeleteApiKey() { + + try { + service.deleteApiKey("apikeyName"); + } catch (CambriaApiException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NullPointerException e) { + assertTrue(true); + } + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/service/EventsRestServiceTest.java b/src/test/java/org/onap/dmaap/service/EventsRestServiceTest.java new file mode 100644 index 0000000..7f30cf4 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/EventsRestServiceTest.java @@ -0,0 +1,328 @@ +/*- + * ============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 java.util.Date; + +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.Mockito; +import org.mockito.MockitoAnnotations; +import static org.mockito.Matchers.any; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.api.mockito.PowerMockito; +import static org.mockito.Mockito.when; + +import com.att.ajsc.beans.PropertiesMapBean; +import org.onap.dmaap.dmf.mr.CambriaApiException; +import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException; +import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages; +import org.onap.dmaap.dmf.mr.service.EventsService; +import com.att.nsa.configs.ConfigDbException; +import org.onap.dmaap.dmf.mr.utils.Utils; +import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting; +import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Date; + +import javax.servlet.ServletInputStream; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; + +import org.onap.dmaap.dmf.mr.beans.DMaaPContext; +import org.onap.dmaap.dmf.mr.exception.DMaaPAccessDeniedException; +import org.onap.dmaap.dmf.mr.exception.ErrorResponse; +import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ PropertiesMapBean.class }) +public class EventsRestServiceTest { + + @InjectMocks + EventsRestService eventsRestRestService; + + @Mock + private EventsService eventsService; + + @Mock + ErrorResponse errorResponse; + + @Mock + DMaaPContext dmaapContext; + + @Mock + InputStream iStream; + + @Mock + ServletInputStream servletInputStream; + + @Mock + HttpServletRequest request; + + @Mock + private DMaaPErrorMessages errorMessages; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetEvents() throws CambriaApiException { + + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + + } + + @Test + public void testGetEvents_error() { + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).getEvents(any(), any(), + any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).getEvents(any(), any(), + any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).getEvents(any(), + any(), any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | UnavailableException | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.getEvents("topicName", "consumergroup", "consumerid"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test(expected = TopicExistsException.class) + public void testGetEvents_TopicExistException() throws CambriaApiException, ConfigDbException, TopicExistsException, + UnavailableException, IOException, AccessDeniedException { + + Mockito.doThrow(new TopicExistsException("topic exists")).when(eventsService).getEvents(any(), + any(), any(), any()); + + eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); + + } + + @Test(expected = DMaaPAccessDeniedException.class) + public void testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, ConfigDbException, + TopicExistsException, UnavailableException, IOException, AccessDeniedException { + + Mockito.doThrow(new DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents(any(), + any(), any(), any()); + + eventsService.getEvents(dmaapContext, "topicName", "consumergroup", "consumerid"); + + } + + /* + * @Test(expected = DMaaPAccessDeniedException.class) public void + * testGetEvents_DMaaPAccessDeniedException() throws CambriaApiException, + * ConfigDbException, TopicExistsException, UnavailableException, + * IOException, AccessDeniedException { + * + * Mockito.doThrow(new + * DMaaPAccessDeniedException(errorResponse)).when(eventsService).getEvents( + * dmaapContext, "topicName", "consumergroup", "consumerid"); + * + * eventsService.getEvents(dmaapContext, "topicName", "consumergroup", + * "consumerid"); + * + * } + */ + + @Test + public void testPushEvents() throws CambriaApiException { + + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + + } + + @Test + public void testPushEvents_error() { + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(), + any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(), any(), + any(), any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(), + any(), any(), any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testPushEvents_TopicExistException() throws CambriaApiException { + + eventsRestRestService.pushEvents("topicName", iStream, "partitionKey"); + + } + + @Test + public void tesTPushEventsWithTransaction() throws CambriaApiException, IOException { + when(request.getInputStream()).thenReturn(servletInputStream); + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + + } + + @Test + public void tesTPushEventsWithTransaction_error() throws IOException { + when(request.getInputStream()).thenReturn(servletInputStream); + ServletInputStream stream = request.getInputStream(); + + try { + PowerMockito.doThrow(new TopicExistsException("error")).when(eventsService).pushEvents(any(), + any(), any(), any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new AccessDeniedException()).when(eventsService).pushEvents(any(),any(), + any(), any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + try { + PowerMockito.doThrow(new IOException()).when(eventsService).pushEvents(any(), any(), any(), + any(), any()); + } catch (TopicExistsException | DMaaPAccessDeniedException | AccessDeniedException | ConfigDbException + | missingReqdSetting | IOException excp) { + assertTrue(false); + } catch (CambriaApiException e) { + assertTrue(false); + } + + try { + eventsRestRestService.pushEventsWithTransaction("topicName", "partitionKey"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + +} diff --git a/src/test/java/org/onap/dmaap/service/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/service/JUnitTestSuite.java new file mode 100644 index 0000000..c1518af --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/JUnitTestSuite.java @@ -0,0 +1,44 @@ +/*- + * ============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 junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ AdminRestServiceTest.class, ApiKeysRestServiceTest.class, + EventsRestServiceTest.class, MetricsRestServiceTest.class, MMRestServiceTest.class, + TopicRestServiceTest.class, TransactionRestServiceTest.class, UIRestServicesTest.class,}) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/service/MMRestServiceTest.java b/src/test/java/org/onap/dmaap/service/MMRestServiceTest.java new file mode 100644 index 0000000..4832d84 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/MMRestServiceTest.java @@ -0,0 +1,384 @@ +/*- + * ============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.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyString; + +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; + +//import static org.mockito.Matchers.anyString; +//import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.InputStream; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.IOUtils; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.After; +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 com.att.ajsc.filemonitor.AJSCPropertiesMap; +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.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.DMaaPAAFAuthenticatorImpl; +import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticator; +import org.onap.dmaap.dmf.mr.service.MMService; +import org.onap.dmaap.dmf.mr.utils.ConfigurationReader; +import com.att.nsa.configs.ConfigDbException; +import org.onap.dmaap.mmagent.CreateMirrorMaker; +import org.onap.dmaap.mmagent.MirrorMaker; +import org.onap.dmaap.mmagent.UpdateMirrorMaker; +import com.att.nsa.security.NsaAcl; +import com.att.nsa.security.NsaApiKey; +import com.att.nsa.security.db.simple.NsaSimpleApiKey; +import com.google.gson.Gson; + +//@RunWith(MockitoJUnitRunner.class) +@RunWith(PowerMockRunner.class) +@PrepareForTest({ PropertiesMapBean.class, AJSCPropertiesMap.class }) +public class MMRestServiceTest { + + @InjectMocks + MMRestService mmRestService; + + @Mock + private MMService mmservice; + + @Mock + CreateMirrorMaker cMirroMaker; + + @Mock + UpdateMirrorMaker uMirroMaker; + + private TopicRestService service = new TopicRestService(); + @Mock + private DMaaPErrorMessages errorMessages; + + @Mock + DMaaPContext dmaapContext; + + @Mock + ConfigurationReader configReader; + + @Mock + ServletOutputStream oStream; + + @Mock + DMaaPAuthenticator<NsaSimpleApiKey> dmaaPAuthenticator; + + @Mock + MirrorMaker mMaker; + + @Mock + DMaaPAAFAuthenticator dmaapAAFauthenticator; + + @Mock + DMaaPAAFAuthenticatorImpl impl; + + @Mock + NsaApiKey user; + + @Mock + NsaSimpleApiKey nsaSimpleApiKey; + + @Mock + HttpServletRequest httpServReq; + + @Mock + HttpServletResponse httpServRes; + + @Mock + InputStream iStream; + + @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 + public void testCallCreateMirrorMaker() throws Exception { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + @Test + public void testCallCreateMirrorMaker_error4() throws Exception { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test@#\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + @Test + public void testCallCreateMirrorMaker_3() throws Exception { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + @Test + public void testCallCreateMirrorMaker_error2() throws Exception { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\",whitelist:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + + @Test + public void testCallCreateMirrorMaker_error1() throws Exception { + prepareForTestCommon(); + + // String sampleJson = ""{ messageID:\"test\", createMirrorMaker: { + // name:\"test\", consumer:\"test\", producer:\"test\", + // whitelist:\"test\",status:\"test\" }}"; + String sampleJson = "{ messageID:\"test\"}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callCreateMirrorMaker(inputSteam); + assertTrue(true); + + } + + @Test + public void testCallListAllMirrorMaker() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", createMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callListAllMirrorMaker(inputSteam); + assertTrue(true); + } + + @Test + public void testCallUpdateMirrorMaker() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + + @Test + public void testCallUpdateMirrorMaker_error1() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test@1\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error2() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"\", consumer:\"test\", producer:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error3() throws Exception{ + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", updateMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + @Test + public void testCallUpdateMirrorMaker_error4() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callUpdateMirrorMaker(inputSteam); + assertTrue(true); + } + + @Test + public void testCallDeleteMirrorMaker() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ messageID:\"test\", deleteMirrorMaker: { name:\"test\", consumer:\"test\", producer:\"test\", whitelist:\"test\",status:\"test\" }}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.callDeleteMirrorMaker(inputSteam); + assertTrue(true); + } + + @Test + public void testListWhiteList() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ name:\"test\", namespace:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.listWhiteList(inputSteam); + assertTrue(true); + } + + @Test + public void testCreateWhiteList() throws Exception { + prepareForTestCommon(); + String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + + mmRestService.createWhiteList(inputSteam); + assertTrue(true); + } + + @Test + public void testDeleteWhiteList() throws Exception { + prepareForTestCommon(); + + String sampleJson = "{ name:\"test\", namespace:\"test\", whitelistTopicName:\"test\"}}"; + InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes()); + mmRestService.deleteWhiteList(inputSteam); + assertTrue(true); + } + + private void prepareForTestCommon() throws Exception { + Assert.assertNotNull(mmRestService); + PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true); + PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true); + + PowerMockito.mockStatic(AJSCPropertiesMap.class); + + assertTrue(true); + + PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf")) + .thenReturn("admin"); + PowerMockito + .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf.create")) + .thenReturn("aafcreate"); + + PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf")) + .thenReturn("admin"); + + PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout")) + .thenReturn("100"); + + PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic")) + .thenReturn("mirrormaker.topic"); + + PowerMockito + .when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup")) + .thenReturn("mirrormaker.consumergroup"); + + PowerMockito.when(AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid")) + .thenReturn("mirrormaker.consumerid"); + + PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq); + + PowerMockito.when(httpServReq.isUserInRole("admin")).thenReturn(true); + + // PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin"); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin.aaf")).thenReturn(true); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true); + PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Admin"); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "admin")).thenReturn(true); + PowerMockito.when(dmaapAAFauthenticator.aafAuthentication(httpServReq, "aafcreatetest|create")) + .thenReturn(true); + + PowerMockito.when(cMirroMaker.getCreateMirrorMaker()).thenReturn(mMaker); + + PowerMockito.when(mMaker.getName()).thenReturn("mirroMakerName"); + PowerMockito.when(dmaapContext.getConfigReader()).thenReturn(configReader); + PowerMockito.when(dmaapContext.getRequest()).thenReturn(httpServReq); + PowerMockito.when(httpServReq.getHeader("Authorization")).thenReturn("Authorization"); + + PowerMockito.when(dmaapContext.getResponse()).thenReturn(httpServRes); + PowerMockito.when(configReader.getfMetaBroker()).thenReturn(dmaapKafkaMetaBroker); + PowerMockito.when(httpServReq.getMethod()).thenReturn("HEAD"); + + PowerMockito.when(dmaapKafkaMetaBroker.getTopic(anyString())).thenReturn(null); + } + +} diff --git a/src/test/java/org/onap/dmaap/service/MetricsRestServiceTest.java b/src/test/java/org/onap/dmaap/service/MetricsRestServiceTest.java new file mode 100644 index 0000000..1188945 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/MetricsRestServiceTest.java @@ -0,0 +1,74 @@ +/*- + * ============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 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.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.service.MetricsService; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ PropertiesMapBean.class }) +public class MetricsRestServiceTest { + + @InjectMocks + MetricsRestService metricsRestService; + + @Mock + private MetricsService metricsService; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetMetrics() throws CambriaApiException { + + metricsRestService.getMetrics(); + + } + + @Test + public void testGetMetricsByName() throws CambriaApiException { + + metricsRestService.getMetricsByName("metricsName"); + + } + + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/service/TestRunner.java b/src/test/java/org/onap/dmaap/service/TestRunner.java new file mode 100644 index 0000000..0441645 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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 org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} diff --git a/src/test/java/org/onap/dmaap/service/TopicRestServiceTest.java b/src/test/java/org/onap/dmaap/service/TopicRestServiceTest.java new file mode 100644 index 0000000..0c563b4 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/TopicRestServiceTest.java @@ -0,0 +1,838 @@ +/*- + * ============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); + } + } + +} diff --git a/src/test/java/org/onap/dmaap/service/TransactionRestServiceTest.java b/src/test/java/org/onap/dmaap/service/TransactionRestServiceTest.java new file mode 100644 index 0000000..b849daa --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/TransactionRestServiceTest.java @@ -0,0 +1,123 @@ +/*- + * ============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 java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +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.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.service.EventsService; +import org.onap.dmaap.dmf.mr.service.TransactionService; +import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; +import com.att.aft.dme2.internal.jettison.json.JSONException; +import org.powermock.api.mockito.PowerMockito; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ PropertiesMapBean.class }) +public class TransactionRestServiceTest { + + @InjectMocks + TransactionRestService transactionRestService; + + @Mock + private TransactionService transactionService; + + @Mock + DMaaPContext dmaapContext; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testGetAllTransactionObjs() throws CambriaApiException { + + transactionRestService.getAllTransactionObjs(); + assertTrue(true); + + } + + @Test + public void testGetTransactionObj() throws CambriaApiException { + + transactionRestService.getTransactionObj("transactionId"); + assertTrue(true); + + } + + @Test + public void testGetAllTransactionObjsError() throws CambriaApiException { + + try { + PowerMockito.doThrow(new IOException()).when(transactionService).getAllTransactionObjs(dmaapContext); + } catch (ConfigDbException | IOException e) { + assertTrue(false); + } + + try { + transactionRestService.getAllTransactionObjs(); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + + @Test + public void testGetTransactionObjError() { + + try { + PowerMockito.doThrow(new IOException()).when(transactionService).getTransactionObj(dmaapContext, + "transactionId"); + } catch (ConfigDbException | JSONException | IOException e) { + assertTrue(false); + } + + try { + transactionRestService.getTransactionObj("transactionId"); + } catch (CambriaApiException e) { + assertTrue(true); + } + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/service/UIRestServicesTest.java b/src/test/java/org/onap/dmaap/service/UIRestServicesTest.java new file mode 100644 index 0000000..3024484 --- /dev/null +++ b/src/test/java/org/onap/dmaap/service/UIRestServicesTest.java @@ -0,0 +1,142 @@ +/*- + * ============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 java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.onap.dmaap.dmf.mr.CambriaApiException; +import com.att.nsa.configs.ConfigDbException; +import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException; + +public class UIRestServicesTest { + + private UIRestServices service = null; + + @Before + public void setUp() throws Exception { + service = new UIRestServices(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testHello() { + + try { + service.hello(); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } + + @Test + public void testGetApiKeysTable() { + + try { + service.getApiKeysTable(); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } + + + @Test + public void testGetApiKey() { + + try { + service.getApiKey("apikey"); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } + + @Test + public void testGetTopicsTable() { + + try { + service.getTopicsTable(); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } + + @Test + public void testGetTopic() { + + try { + service.getTopic("topicName"); + } catch (NullPointerException e) { + assertTrue(true); + } + assertTrue(true); + } + + @Test + public void testGetDmaapContext() { + Class clazz = null; + Method method = null; + try { + clazz = Class.forName("UIRestServices"); + Object obj = clazz.newInstance(); + method = clazz.getDeclaredMethod("getDmaapContext", null); + method.setAccessible(true); + method.invoke(obj, null); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assertTrue(true); + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/tools/ConfigToolContextTest.java b/src/test/java/org/onap/dmaap/tools/ConfigToolContextTest.java new file mode 100644 index 0000000..0dad4af --- /dev/null +++ b/src/test/java/org/onap/dmaap/tools/ConfigToolContextTest.java @@ -0,0 +1,86 @@ +/*- + * ============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.tools; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ConfigToolContextTest { + + private ConfigToolContext context; + @Before + public void setUp() throws Exception { + context = new ConfigToolContext(null, "connStr", null); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testRequestShutdown() { + + context.requestShutdown(); + + assertTrue(true); + + } + + @Test + public void testShouldContinue() { + + context.shouldContinue(); + + assertTrue(true); + + } + + @Test + public void testGetDb() { + + context.getDb(); + + assertTrue(true); + + } + + @Test + public void testGetMetrics() { + + context.getMetrics(); + + assertTrue(true); + + } + + @Test + public void testGetConnectionString() { + + context.getConnectionString(); + + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/tools/ConfigToolTest.java b/src/test/java/org/onap/dmaap/tools/ConfigToolTest.java new file mode 100644 index 0000000..85b41dc --- /dev/null +++ b/src/test/java/org/onap/dmaap/tools/ConfigToolTest.java @@ -0,0 +1,523 @@ +/*- + * ============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.tools; + +import static org.junit.Assert.*; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ConfigToolTest { + + private String[] parts = new String[5]; + + @Before + public void setUp() throws Exception { + + for (int i = 0; i < parts.length; i++) { + parts[i] = "string" + (i + 1); + } + } + + @After + public void tearDown() throws Exception { + } + + public void callMethodViaReflection(String outer, String inner, String methodName, Object... args) { + + String foreNameString = outer + "$" + inner; + Object parent = new ConfigTool(); + + Class<?> innerClass; + try { + innerClass = Class.forName(foreNameString); + Constructor<?> constructor = innerClass.getDeclaredConstructor(ConfigTool.class); + constructor.setAccessible(true); + Object child = constructor.newInstance(parent); + + // invoking method on inner class object + Method method = innerClass.getDeclaredMethod(methodName, null); + method.setAccessible(true);// in case of unaccessible method + method.invoke(child, args); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (NoSuchMethodException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InstantiationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InvocationTargetException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + assertTrue(true); + + } + + @Test + public void testGetMatches() { + + callMethodViaReflection("ConfigTool", "ListTopicCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady() { + + callMethodViaReflection("ConfigTool", "ListTopicCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute() { + + callMethodViaReflection("ConfigTool", "ListTopicCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp() { + + callMethodViaReflection("ConfigTool", "ListTopicCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches2() { + + callMethodViaReflection("ConfigTool", "WriteTopicCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady2() { + + callMethodViaReflection("ConfigTool", "WriteTopicCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute2() { + + callMethodViaReflection("ConfigTool", "WriteTopicCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp2() { + + callMethodViaReflection("ConfigTool", "WriteTopicCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches3() { + + callMethodViaReflection("ConfigTool", "ReadTopicCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady3() { + + callMethodViaReflection("ConfigTool", "ReadTopicCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute3() { + + callMethodViaReflection("ConfigTool", "ReadTopicCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp3() { + + callMethodViaReflection("ConfigTool", "ReadTopicCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches4() { + + callMethodViaReflection("ConfigTool", "InitSecureTopicCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady4() { + + callMethodViaReflection("ConfigTool", "InitSecureTopicCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute4() { + + callMethodViaReflection("ConfigTool", "InitSecureTopicCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp4() { + + callMethodViaReflection("ConfigTool", "InitSecureTopicCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches5() { + + callMethodViaReflection("ConfigTool", "SetTopicOwnerCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady5() { + + callMethodViaReflection("ConfigTool", "SetTopicOwnerCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute5() { + + callMethodViaReflection("ConfigTool", "SetTopicOwnerCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp5() { + + callMethodViaReflection("ConfigTool", "SetTopicOwnerCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches6() { + + callMethodViaReflection("ConfigTool", "ListApiKeysCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady6() { + + callMethodViaReflection("ConfigTool", "ListApiKeysCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute6() { + + callMethodViaReflection("ConfigTool", "ListApiKeysCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp6() { + + callMethodViaReflection("ConfigTool", "ListApiKeysCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches7() { + + callMethodViaReflection("ConfigTool", "PutApiCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady7() { + + callMethodViaReflection("ConfigTool", "PutApiCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute7() { + + callMethodViaReflection("ConfigTool", "PutApiCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp7() { + + callMethodViaReflection("ConfigTool", "PutApiCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches8() { + + callMethodViaReflection("ConfigTool", "writeApiKeyCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady8() { + + callMethodViaReflection("ConfigTool", "writeApiKeyCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute8() { + + callMethodViaReflection("ConfigTool", "writeApiKeyCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp8() { + + callMethodViaReflection("ConfigTool", "writeApiKeyCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches9() { + + callMethodViaReflection("ConfigTool", "EncryptApiKeysCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady9() { + + callMethodViaReflection("ConfigTool", "EncryptApiKeysCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute9() { + + callMethodViaReflection("ConfigTool", "EncryptApiKeysCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp9() { + + callMethodViaReflection("ConfigTool", "EncryptApiKeysCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches10() { + + callMethodViaReflection("ConfigTool", "DecryptApiKeysCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady10() { + + callMethodViaReflection("ConfigTool", "DecryptApiKeysCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute10() { + + callMethodViaReflection("ConfigTool", "DecryptApiKeysCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp10() { + + callMethodViaReflection("ConfigTool", "DecryptApiKeysCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches11() { + + callMethodViaReflection("ConfigTool", "NodeFetchCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady11() { + + callMethodViaReflection("ConfigTool", "NodeFetchCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute11() { + + callMethodViaReflection("ConfigTool", "NodeFetchCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp11() { + + callMethodViaReflection("ConfigTool", "NodeFetchCommand", "displayHelp", null); + + assertTrue(true); + + } + + @Test + public void testGetMatches12() { + + callMethodViaReflection("ConfigTool", "DropOldConsumerGroupsCommand", "getMatches"); + + assertTrue(true); + + } + + @Test + public void testCheckReady12() { + + callMethodViaReflection("ConfigTool", "DropOldConsumerGroupsCommand", "checkReady", null); + + assertTrue(true); + + } + + @Test + public void testExecute12() { + + callMethodViaReflection("ConfigTool", "DropOldConsumerGroupsCommand", "execute", parts, null, null); + + assertTrue(true); + + } + + @Test + public void testDisplayHelp12() { + + callMethodViaReflection("ConfigTool", "DropOldConsumerGroupsCommand", "displayHelp", null); + + assertTrue(true); + + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/tools/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/tools/JUnitTestSuite.java new file mode 100644 index 0000000..b50c950 --- /dev/null +++ b/src/test/java/org/onap/dmaap/tools/JUnitTestSuite.java @@ -0,0 +1,42 @@ +/*- + * ============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.tools; + +import junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ ConfigToolTest.class, ConfigToolContextTest.class,}) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/tools/TestRunner.java b/src/test/java/org/onap/dmaap/tools/TestRunner.java new file mode 100644 index 0000000..2266be0 --- /dev/null +++ b/src/test/java/org/onap/dmaap/tools/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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.tools; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} diff --git a/src/test/java/org/onap/dmaap/util/ContentLengthInterceptorTest.java b/src/test/java/org/onap/dmaap/util/ContentLengthInterceptorTest.java new file mode 100644 index 0000000..0608d2e --- /dev/null +++ b/src/test/java/org/onap/dmaap/util/ContentLengthInterceptorTest.java @@ -0,0 +1,93 @@ +/*- + * ============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.util; + +import static org.junit.Assert.assertTrue; + +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +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.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ System.class }) +public class ContentLengthInterceptorTest { + @InjectMocks + ContentLengthInterceptor interceptor = null; + + @Mock + Map map; + + @Mock + HttpServletRequest req; + + @Mock + HttpServletResponse res; + + @Before + public void setUp() throws Exception { + // interceptor = new ContentLengthInterceptor(); + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testAllowOrReject() throws Exception { + PowerMockito.when(req.getHeader("Transfer-Encoding")).thenReturn("UTF-8"); + PowerMockito.when(req.getHeader("Content-Length")).thenReturn("1027"); + + interceptor.allowOrReject(req, res, map); + assertTrue(true); + } + + //@Test(expected = NullPointerException.class) + public void testAllowOrRejectWithException() throws Exception { + PowerMockito.when(req.getHeader("Transfer-Encoding")).thenThrow(new NumberFormatException()); + interceptor.allowOrReject(req, res, map); + assertTrue(true); + } + + + @Test + public void testGetDefLength() { + interceptor.getDefLength(); + assertTrue(true); + } + + @Test + public void testSetDefLength() { + interceptor.setDefLength("defLength"); + assertTrue(true); + + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/util/DMaaPAuthFilterTest.java b/src/test/java/org/onap/dmaap/util/DMaaPAuthFilterTest.java new file mode 100644 index 0000000..ad94441 --- /dev/null +++ b/src/test/java/org/onap/dmaap/util/DMaaPAuthFilterTest.java @@ -0,0 +1,100 @@ +/*- + * ============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.util; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; + +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; + +import com.att.ajsc.beans.PropertiesMapBean; +import org.onap.dmaap.dmf.mr.beans.DMaaPContext; +import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode; + + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ PropertiesMapBean.class, DMaaPResponseCode.class }) +public class DMaaPAuthFilterTest { + + @InjectMocks + DMaaPAuthFilter filter; + + @Mock + HttpServletRequest req; + + @Mock + ServletResponse res; + + @Mock + FilterChain chain; + + @Mock + DMaaPContext dmaapContext; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @After + public void tearDown() throws Exception { + } + + //@Test + public void testDoFilter() throws IOException, ServletException { + + PowerMockito.when(dmaapContext.getRequest()).thenReturn(req); + PowerMockito.when(req.getHeader("Authorization")).thenReturn("Authorization"); + // when(dmaapContext.getResponse()).thenReturn(res); + filter.doFilter(req, res, chain); + assertTrue(true); + + } + + //@Test + public void testDoFilter_nullAuth() throws IOException, ServletException { + + PowerMockito.when(dmaapContext.getRequest()).thenReturn(req); + PowerMockito.when(req.getHeader("Authorization")).thenReturn("Authorization"); + + // when(dmaapContext.getResponse()).thenReturn(res); + filter.doFilter(req, res, chain); + assertTrue(true); + + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/util/JUnitTestSuite.java b/src/test/java/org/onap/dmaap/util/JUnitTestSuite.java new file mode 100644 index 0000000..9de79ab --- /dev/null +++ b/src/test/java/org/onap/dmaap/util/JUnitTestSuite.java @@ -0,0 +1,42 @@ +/*- + * ============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.util; + +import junit.framework.TestSuite; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.apache.log4j.Logger; + +@RunWith(Suite.class) +@SuiteClasses({ ContentLengthInterceptorTest.class, DMaaPAuthFilterTest.class, ServicePropertiesMapBeanTest.class}) +public class JUnitTestSuite { + private static final Logger LOGGER = Logger.getLogger(JUnitTestSuite.class); + + public static void main(String[] args) { + LOGGER.info("Running the test suite"); + + TestSuite tstSuite = new TestSuite(); + LOGGER.info("Total Test Counts " + tstSuite.countTestCases()); + } + +} diff --git a/src/test/java/org/onap/dmaap/util/ServicePropertiesMapBeanTest.java b/src/test/java/org/onap/dmaap/util/ServicePropertiesMapBeanTest.java new file mode 100644 index 0000000..036bd13 --- /dev/null +++ b/src/test/java/org/onap/dmaap/util/ServicePropertiesMapBeanTest.java @@ -0,0 +1,53 @@ +/*- + * ============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.util; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ServicePropertiesMapBeanTest { + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetProperty() { + + try { + ServicePropertiesMapBean.getProperty(null, null); + } catch (Exception e) { + e.printStackTrace(); + } + + assertTrue(true); + + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/dmaap/util/TestRunner.java b/src/test/java/org/onap/dmaap/util/TestRunner.java new file mode 100644 index 0000000..72a77a1 --- /dev/null +++ b/src/test/java/org/onap/dmaap/util/TestRunner.java @@ -0,0 +1,41 @@ +/*- + * ============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.util; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; +import org.apache.log4j.Logger; + +public class TestRunner { + private static final Logger LOGGER = Logger.getLogger(TestRunner.class); + + public static void main(String[] args) { + // TODO Auto-generated method stub + Result result = JUnitCore.runClasses(JUnitTestSuite.class); + for (Failure failure : result.getFailures()) { + LOGGER.info(failure.toString()); + + } + LOGGER.info(result.wasSuccessful()); + } + +} |