diff options
author | Lathishbabu Ganesan <lathishbabu.ganesan@ericsson.com> | 2019-02-13 10:05:13 -0500 |
---|---|---|
committer | Takamune Cho <takamune.cho@att.com> | 2019-02-14 14:16:04 +0000 |
commit | b186a6a5c746c660a885492f421c29224f56b310 (patch) | |
tree | a065db0f15326c551d0489fa45fbbdaf9d242047 /appc-inbound/appc-design-services/provider/src | |
parent | 29039d7e4abee0419cf07714dac8f85efdf51b61 (diff) |
Added test case for DesignServiceImpl Class
Increased the coverage from 0% to 97.5%
Issue-ID: APPC-1420
Change-Id: Iea12e07e91f6d84645dbbc060518c26c1695fd40
Signed-off-by: Lathishbabu Ganesan <lathishbabu.ganesan@ericsson.com>
Diffstat (limited to 'appc-inbound/appc-design-services/provider/src')
4 files changed, 241 insertions, 80 deletions
diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/impl/DesignServicesImpl.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/impl/DesignServicesImpl.java index bef8ce4d2..890f40d3d 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/impl/DesignServicesImpl.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/services/impl/DesignServicesImpl.java @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * 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 @@ -99,8 +101,8 @@ public class DesignServicesImpl implements DesignServicesService { StatusBuilder statusBuilder = new StatusBuilder(); try { - XInterfaceService xInterfaceService = new XInterfaceService(); - XResponseProcessor responseProcessor = new XResponseProcessor(); + XInterfaceService xInterfaceService = XInterfaceService.getInstance(); + XResponseProcessor responseProcessor = XResponseProcessor.getInstance(); String response = responseProcessor.parseResponse( xInterfaceService.execute(input.getDesignRequest().getAction(), input.getDesignRequest().getPayload()), input.getDesignRequest().getAction()); diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XInterfaceService.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XInterfaceService.java index 51ff09c46..42f13243d 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XInterfaceService.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XInterfaceService.java @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * 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 @@ -24,32 +26,36 @@ package org.onap.appc.design.xinterface; import org.onap.appc.design.services.util.DesignServiceConstants; -import org.onap.appc.design.xinterface.XResponseProcessor; - import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.google.common.base.Strings; public class XInterfaceService { - - private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class); - public String execute(String action, String payload) throws Exception { - //File targetFile = new File("/tmp/" + action + "-response.txt" ); - String interfaceResponse = null; - try{ - if(Strings.isNullOrEmpty(payload)) - throw new Exception("Payload is null or empty.."); - if(DesignServiceConstants.GETINSTARDATA.equalsIgnoreCase(action)){ - XResponseProcessor xResponseProcessor = new XResponseProcessor(); - interfaceResponse = xResponseProcessor.parseResponse(payload, DesignServiceConstants.GETINSTARDATA); - } else { - throw new Exception("No Such Action, Please enter valid Action"); - } - }catch(Exception e){ - e.printStackTrace(); - throw e; - } - return interfaceResponse; + + private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class); + + public static XInterfaceService getInstance() { + return new XInterfaceService(); + } + + public String execute(String action, String payload) throws Exception { + // File targetFile = new File("/tmp/" + action + "-response.txt" ); + String interfaceResponse = null; + try { + if (Strings.isNullOrEmpty(payload)) + throw new Exception("Payload is null or empty.."); + if (DesignServiceConstants.GETINSTARDATA.equalsIgnoreCase(action)) { + XResponseProcessor xResponseProcessor = new XResponseProcessor(); + interfaceResponse = + xResponseProcessor.parseResponse(payload, DesignServiceConstants.GETINSTARDATA); + } else { + throw new Exception("No Such Action, Please enter valid Action"); + } + } catch (Exception e) { + e.printStackTrace(); + throw e; } + return interfaceResponse; + } } diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XResponseProcessor.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XResponseProcessor.java index 813d50495..4340acca9 100644 --- a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XResponseProcessor.java +++ b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XResponseProcessor.java @@ -6,6 +6,8 @@ * ================================================================================ * Copyright (C) 2017 Amdocs * ============================================================================= + * Modifications Copyright (C) 2019 Ericsson + * ============================================================================= * 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 @@ -24,10 +26,8 @@ package org.onap.appc.design.xinterface; import java.util.HashMap; - import org.onap.appc.design.services.util.DesignServiceConstants; import org.onap.appc.instar.dme2client.Dme2Client; - import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.fasterxml.jackson.databind.JsonNode; @@ -35,60 +35,64 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class XResponseProcessor { - private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class); - Dme2Client dme2Client; - - public String parseResponse(String execute, String action) throws Exception { - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode payloadObject = objectMapper.readTree(execute); - log.info("payloadObject " + payloadObject); - - //String queryParam = null; - String instarResponse = null; - HashMap<String, String> payload = null; - String ipAddress = null; - String mask = null; - - try { - - // check the payload whether its having ipaddr along with subnet - ipAddress = payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS) != null - ? payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS).textValue() - : (payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS) !=null) - ?payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS).textValue().toUpperCase() - :null; - - mask = payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK) != null - ? payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK).textValue() - : (payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK) != null) - ? payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK).textValue().toUpperCase() - : null; - - // TODO -short format - - /*if (mask != null) { - queryParam = ipAddress + "," +mask ; - log.info("Calling Instar with IpAddress "+ ipAddress + " Mask value: "+ mask ); - } else { - queryParam = "ipAddress "+ipAddress ; - log.info("Calling Instar with IpAddress "+ ipAddress); - }*/ - - payload = new HashMap<String, String>(); - payload.put("ipAddress", ipAddress); - payload.put("mask", mask); - log.info("Calling Instar with IpAddress "+ ipAddress + " Mask value: "+ mask ); - dme2Client = new Dme2Client("getVnfbyIpadress", "payload", payload); - - instarResponse = dme2Client.send(); - - log.debug("Resposne from Instar = " + instarResponse); - if (instarResponse == null || instarResponse.length() < 0) - throw new Exception("No Data received from Instar for this action " + action); - } catch (Exception e) { - e.printStackTrace(); - throw e; - } - return instarResponse; + private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class); + Dme2Client dme2Client; + + public static XResponseProcessor getInstance() { + return new XResponseProcessor(); + } + + public String parseResponse(String execute, String action) throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode payloadObject = objectMapper.readTree(execute); + log.info("payloadObject " + payloadObject); + + // String queryParam = null; + String instarResponse = null; + HashMap<String, String> payload = null; + String ipAddress = null; + String mask = null; + + try { + + // check the payload whether its having ipaddr along with subnet + ipAddress = payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS) != null + ? payloadObject.get(DesignServiceConstants.INSTAR_V4_ADDRESS).textValue() + : (payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS) != null) + ? payloadObject.get(DesignServiceConstants.INSTAR_V6_ADDRESS).textValue() + .toUpperCase() + : null; + + mask = payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK) != null + ? payloadObject.get(DesignServiceConstants.INSTAR_V4_MASK).textValue() + : (payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK) != null) + ? payloadObject.get(DesignServiceConstants.INSTAR_V6_MASK).textValue().toUpperCase() + : null; + + // TODO -short format + + /* + * if (mask != null) { queryParam = ipAddress + "," +mask ; + * log.info("Calling Instar with IpAddress "+ ipAddress + " Mask value: "+ mask ); } else { + * queryParam = "ipAddress "+ipAddress ; log.info("Calling Instar with IpAddress "+ + * ipAddress); } + */ + + payload = new HashMap<String, String>(); + payload.put("ipAddress", ipAddress); + payload.put("mask", mask); + log.info("Calling Instar with IpAddress " + ipAddress + " Mask value: " + mask); + dme2Client = new Dme2Client("getVnfbyIpadress", "payload", payload); + + instarResponse = dme2Client.send(); + + log.debug("Resposne from Instar = " + instarResponse); + if (instarResponse == null || instarResponse.length() < 0) + throw new Exception("No Data received from Instar for this action " + action); + } catch (Exception e) { + e.printStackTrace(); + throw e; } + return instarResponse; + } } diff --git a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/impl/TestDesignServicesImpl.java b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/impl/TestDesignServicesImpl.java new file mode 100644 index 000000000..cfe642a4b --- /dev/null +++ b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/services/impl/TestDesignServicesImpl.java @@ -0,0 +1,149 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Ericsson. 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.appc.design.services.impl; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.onap.appc.design.dbervices.DesignDBService; +import org.onap.appc.design.xinterface.XInterfaceService; +import org.onap.appc.design.xinterface.XResponseProcessor; +import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.DbserviceInput; +import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.ValidatorInput; +import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.XinterfaceserviceInput; +import org.opendaylight.yang.gen.v1.org.onap.appc.rev170627.design.request.DesignRequest; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({DesignDBService.class,XInterfaceService.class,XResponseProcessor.class}) +public class TestDesignServicesImpl { + + private DesignServicesImpl designServicesImpl; + private DbserviceInput dbserviceInput; + private XinterfaceserviceInput xinterfaceserviceInput; + private ValidatorInput validatorInput; + private DesignRequest designRequest; + private DesignDBService designDBService; + private XInterfaceService xInterfaceService; + private XResponseProcessor xResponseProcessor; + + + @Before + public void setUp() throws Exception { + designServicesImpl = new DesignServicesImpl(); + dbserviceInput = Mockito.mock(DbserviceInput.class); + designRequest = Mockito.mock(DesignRequest.class); + designDBService = Mockito.mock(DesignDBService.class); + xInterfaceService = Mockito.mock(XInterfaceService.class); + PowerMockito.mockStatic(DesignDBService.class); + PowerMockito.mockStatic(XResponseProcessor.class); + PowerMockito.mockStatic(XInterfaceService.class); + xResponseProcessor = PowerMockito.mock(XResponseProcessor.class); + xinterfaceserviceInput = Mockito.mock(XinterfaceserviceInput.class); + validatorInput = Mockito.mock(ValidatorInput.class); + when(dbserviceInput.getDesignRequest()).thenReturn(designRequest); + when(xinterfaceserviceInput.getDesignRequest()).thenReturn(designRequest); + when(validatorInput.getDesignRequest()).thenReturn(designRequest); + when(designRequest.getRequestId()).thenReturn("123"); + when(designRequest.getAction()).thenReturn("getDesigns"); + when(designRequest.getPayload()).thenReturn("{\"artifact-contents\":\"\",\"userID\":\"user\"}"); + } + + @Test + public void testDbService() throws Exception { + PowerMockito.when(DesignDBService.initialise()).thenReturn(designDBService); + when(designDBService.execute(eq("getDesigns"), anyString(), eq("123"))).thenReturn("success"); + assertEquals("400", + designServicesImpl.dbservice(dbserviceInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testDbServiceWithException() throws Exception { + PowerMockito.when(DesignDBService.initialise()).thenReturn(designDBService); + when(designDBService.execute(eq("getDesigns"), anyString(), eq("123"))) + .thenThrow(new Exception()); + assertEquals("401", + designServicesImpl.dbservice(dbserviceInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testXinterfaceservice() throws Exception { + PowerMockito.when(XInterfaceService.getInstance()).thenReturn(xInterfaceService); + PowerMockito.when(XResponseProcessor.getInstance()).thenReturn(xResponseProcessor); + when(xResponseProcessor.parseResponse(anyString(), eq("getDesigns"))).thenReturn("success"); + assertEquals("400", designServicesImpl.xinterfaceservice(xinterfaceserviceInput).get() + .getResult().getStatus().getCode()); + } + + @Test + public void testXinterfaceserviceWithException() throws Exception { + PowerMockito.when(XInterfaceService.getInstance()).thenReturn(xInterfaceService); + PowerMockito.when(XResponseProcessor.getInstance()).thenReturn(xResponseProcessor); + when(xResponseProcessor.parseResponse(anyString(), eq("getDesigns"))).thenThrow(new Exception()); + assertEquals("401", designServicesImpl.xinterfaceservice(xinterfaceserviceInput).get() + .getResult().getStatus().getCode()); + } + + @Test + public void testValidatorJson() throws Exception { + when(designRequest.getDataType()).thenReturn("JSON"); + assertEquals("400", + designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testValidatorXml() throws Exception { + when(designRequest.getPayload()).thenReturn("<artifact-contents></artifact-contents>"); + when(designRequest.getDataType()).thenReturn("XML"); + assertEquals("400", + designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testValidatorYaml() throws Exception { + when(designRequest.getPayload()).thenReturn("artifact-contents: 34843"); + when(designRequest.getDataType()).thenReturn("YAML"); + assertEquals("400", + designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testValidatorVelocity() throws Exception { + when(designRequest.getPayload()).thenReturn("artifact-contents: 34843"); + when(designRequest.getDataType()).thenReturn("VELOCITY"); + assertEquals("400", + designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode()); + } + + @Test + public void testValidatorInvalid() throws Exception { + when(designRequest.getDataType()).thenReturn("XYZ"); + assertEquals("401", + designServicesImpl.validator(validatorInput).get().getResult().getStatus().getCode()); + } + +} |