diff options
5 files changed, 327 insertions, 22 deletions
diff --git a/src/main/java/com/att/nsa/dmaap/service/AdminRestService.java b/src/main/java/com/att/nsa/dmaap/service/AdminRestService.java index 5201dc8..2ab574d 100644 --- a/src/main/java/com/att/nsa/dmaap/service/AdminRestService.java +++ b/src/main/java/com/att/nsa/dmaap/service/AdminRestService.java @@ -91,6 +91,8 @@ public class AdminRestService { */ @Autowired private AdminService adminService; + + private DMaaPContext dmaaPContext; /** * Fetches a list of all the registered consumers along with their created @@ -107,7 +109,7 @@ public class AdminRestService { public void getConsumerCache() throws CambriaApiException, AccessDeniedException { LOGGER.info("Fetching list of registered consumers."); try { - adminService.showConsumerCache(getDMaaPContext()); + adminService.showConsumerCache(ServiceUtil.getDMaaPContext(configReader, request, response)); LOGGER.info("Fetching Consumer Cache Successfully"); } catch (IOException e) { LOGGER.error("Error while Fetching list of registered consumers : " @@ -136,7 +138,7 @@ public class AdminRestService { public void dropConsumerCache() throws CambriaApiException, AccessDeniedException { LOGGER.info("Dropping consumer cache"); try { - adminService.dropConsumerCache(getDMaaPContext()); + adminService.dropConsumerCache(ServiceUtil.getDMaaPContext(configReader, request, response)); LOGGER.info("Dropping Consumer Cache successfully"); } catch ( AccessDeniedException excp) { LOGGER.error("Error while dropConsumerCache : " @@ -170,14 +172,14 @@ public class AdminRestService { public void getBlacklist() throws CambriaApiException { LOGGER.info("Fetching list of blacklist ips."); try { - Enumeration headerNames = getDMaaPContext().getRequest().getHeaderNames(); + Enumeration headerNames = ServiceUtil.getDMaaPContext(configReader, request, response).getRequest().getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); } - adminService.getBlacklist(getDMaaPContext()); + adminService.getBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response)); LOGGER.info("Fetching list of blacklist ips Successfully"); }catch ( AccessDeniedException excp) { LOGGER.error("Error while Fetching list of blacklist ips : " @@ -215,7 +217,7 @@ public class AdminRestService { { LOGGER.info("Adding ip to list of blacklist ips."); try { - adminService.addToBlacklist(getDMaaPContext(), ip); + adminService.addToBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response), ip); LOGGER.info("Fetching list of blacklist ips Successfully"); } catch ( AccessDeniedException excp) { LOGGER.error("Error while blacklist : " @@ -253,7 +255,7 @@ public class AdminRestService { public void removeFromBlacklist(@PathParam("ip") String ip) throws CambriaApiException, AccessDeniedException, ConfigDbException { LOGGER.info("Fetching list of blacklist ips."); try { - adminService.removeFromBlacklist(getDMaaPContext(), ip); + adminService.removeFromBlacklist(ServiceUtil.getDMaaPContext(configReader, request, response), ip); LOGGER.info("Fetching list of blacklist ips Successfully"); }catch ( AccessDeniedException excp) { LOGGER.error("Error while blacklist : " @@ -278,16 +280,6 @@ public class AdminRestService { } } - /** - * Create a dmaap context - * @return DMaaPContext - */ - private DMaaPContext getDMaaPContext() { - DMaaPContext dmaaPContext = new DMaaPContext(); - dmaaPContext.setConfigReader(configReader); - dmaaPContext.setRequest(request); - dmaaPContext.setResponse(response); - return dmaaPContext; - } + } diff --git a/src/main/java/com/att/nsa/dmaap/service/MMRestService.java b/src/main/java/com/att/nsa/dmaap/service/MMRestService.java index a715e1f..d90d832 100644 --- a/src/main/java/com/att/nsa/dmaap/service/MMRestService.java +++ b/src/main/java/com/att/nsa/dmaap/service/MMRestService.java @@ -168,7 +168,7 @@ public class MMRestService { sendErrResponse(ctx, errorMessages.getIncorrectJson()); LOGGER.error("JsonSyntaxException: ", ex); } - String name = createMirrorMaker.getCreateMirrorMaker().getName(); + String name = createMirrorMaker.getCreateMirrorMaker()==null? "":createMirrorMaker.getCreateMirrorMaker().getName(); // send error message if it is not a CreateMirrorMaker request. if (createMirrorMaker.getCreateMirrorMaker() == null) { sendErrResponse(ctx, "This is not a CreateMirrorMaker request. Please try again."); @@ -318,8 +318,7 @@ public class MMRestService { LOGGER.error("JsonSyntaxException: ", ex); } - String name = updateMirrorMaker.getUpdateMirrorMaker().getName(); - + String name = updateMirrorMaker.getUpdateMirrorMaker()==null? "":updateMirrorMaker.getUpdateMirrorMaker().getName(); // send error message if it is not a UpdateMirrorMaker request. if (updateMirrorMaker.getUpdateMirrorMaker() == null) { sendErrResponse(ctx, "This is not a UpdateMirrorMaker request. Please try again."); diff --git a/src/main/java/com/att/nsa/dmaap/service/ServiceUtil.java b/src/main/java/com/att/nsa/dmaap/service/ServiceUtil.java new file mode 100644 index 0000000..928ab9f --- /dev/null +++ b/src/main/java/com/att/nsa/dmaap/service/ServiceUtil.java @@ -0,0 +1,39 @@ +/*- + * ============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 com.att.nsa.dmaap.service; + +import com.att.nsa.cambria.beans.DMaaPContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import com.att.nsa.cambria.utils.ConfigurationReader; + +public class ServiceUtil { + private static DMaaPContext dmaaPContext; + + + public static DMaaPContext getDMaaPContext(ConfigurationReader configReader,HttpServletRequest request,HttpServletResponse response) { + dmaaPContext = new DMaaPContext(); + dmaaPContext.setConfigReader(configReader); + dmaaPContext.setRequest(request); + dmaaPContext.setResponse(response); + return dmaaPContext; + } + +} diff --git a/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java index d9b39dc..e9a14c7 100644 --- a/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/AdminRestServiceTest.java @@ -29,22 +29,32 @@ import org.mockito.InjectMocks; import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import com.att.nsa.cambria.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 com.att.nsa.cambria.beans.DMaaPContext;
+
+import java.io.IOException;
import java.util.Enumeration;
import com.att.nsa.cambria.service.AdminService;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
+import com.att.nsa.cambria.beans.DMaaPContext;
+import com.att.nsa.cambria.utils.ConfigurationReader;
+import org.powermock.core.classloader.annotations.PrepareForTest;
@RunWith(PowerMockRunner.class)
+@PrepareForTest({ ServiceUtil.class })
public class AdminRestServiceTest {
-
+
@InjectMocks
AdminRestService adminRestService;
@@ -56,13 +66,20 @@ public class AdminRestServiceTest { @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
@@ -76,10 +93,55 @@ public class AdminRestServiceTest { }
@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 {
@@ -92,6 +154,46 @@ public class AdminRestServiceTest { 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 {
@@ -103,11 +205,90 @@ public class AdminRestServiceTest { }
@Test
- public void testRemoveFromBlacklist() throws CambriaApiException, AccessDeniedException, ConfigDbException {
+ 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);
+ }
}
diff --git a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java index d85ef9b..b25578c 100644 --- a/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java +++ b/src/test/java/com/att/nsa/dmaap/service/MMRestServiceTest.java @@ -173,6 +173,63 @@ public class MMRestServiceTest { assertTrue(true);
}
+ @Test
+ public void testCallCreateMirrorMaker_error4() throws DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+ 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 DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+ 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 DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+ 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 DMaaPAccessDeniedException, CambriaApiException, IOException,
+ TopicExistsException, JSONException, ConfigDbException {
+ 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 DMaaPAccessDeniedException, CambriaApiException, IOException,
@@ -194,6 +251,43 @@ public class MMRestServiceTest { mmRestService.callUpdateMirrorMaker(inputSteam);
assertTrue(true);
}
+
+ @Test
+ public void testCallUpdateMirrorMaker_error1() throws ConfigDbException, CambriaApiException {
+ 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 ConfigDbException, CambriaApiException {
+ 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 ConfigDbException, CambriaApiException {
+ 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 ConfigDbException, CambriaApiException {
+ prepareForTestCommon();
+
+ String sampleJson = "{ messageID:\"test\"}}";
+ InputStream inputSteam = new ByteArrayInputStream(sampleJson.getBytes());
+ mmRestService.callUpdateMirrorMaker(inputSteam);
+ assertTrue(true);
+ }
@Test
public void testCallDeleteMirrorMaker() throws ConfigDbException, CambriaApiException {
|