/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * 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.so.bpmn.common.scripts; import static org.junit.Assert.*; import static org.mockito.Mockito.* import org.junit.Before import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith import org.camunda.bpm.engine.delegate.BpmnError import org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity import org.camunda.bpm.engine.delegate.DelegateExecution import org.onap.so.bpmn.core.WorkflowException import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils import org.onap.so.bpmn.mock.FileUtil import org.mockito.junit.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.Silent.class) public class SDNCAdapterUtilsTest { private def map private ExecutionEntity svcex private WorkflowException wfex private AbstractServiceTaskProcessor tp private String resp private SDNCAdapterUtils utils @Before public void init() { map = new HashMap() svcex = mock(ExecutionEntity.class) wfex = null tp = new AbstractServiceTaskProcessor() { @Override public void preProcessRequest(DelegateExecution execution) { } }; utils = new SDNCAdapterUtils(tp) // svcex gets its variables from "map" when(svcex.getVariable(any())).thenAnswer( { invocation -> return map.get(invocation.getArgument(0)) }) // svcex puts its variables into "map" when(svcex.setVariable(any(), any())).thenAnswer( { invocation -> return map.put( invocation.getArgument(0), invocation.getArgument(1)) }) map.put("isDebugLogEnabled", "true") map.put("prefix", "mypfx-") map.put("testProcessKey", "mykey") } @Test public void testValidateSDNCResponse_Success_NoCode() { resp = """""" utils.validateSDNCResponse(svcex, resp, wfex, true) assertEquals(true, map.get("mypfx-sdncResponseSuccess")) assertEquals("0", map.get("mypfx-sdncRequestDataResponseCode")) assertFalse(map.containsKey("WorkflowException")) } @Test public void testValidateSDNCResponse_200() { utils.validateSDNCResponse(svcex, makeResp("200", "OK", ""), wfex, true) assertEquals(true, map.get("mypfx-sdncResponseSuccess")) assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode")) assertFalse(map.containsKey("WorkflowException")) } @Test public void testValidateSDNCResponse_408() { try { utils.validateSDNCResponse(svcex, makeResp("408", "failed", ""), wfex, true) // this has been commented out as, currently, the code doesn't // throw an exception in this case // fail("missing exception") } catch(BpmnError ex) { ex.printStackTrace() } assertEquals(false, map.get("mypfx-sdncResponseSuccess")) assertEquals("408", map.get("mypfx-sdncRequestDataResponseCode")) wfex = map.get("WorkflowException") assertNotNull(wfex) assertEquals(5320, wfex.getErrorCode()) assertEquals("Received error from SDN-C: failed", wfex.getErrorMessage()) } @Test public void testValidateSDNCResponse_408_200() { utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", "ok")), wfex, true) assertEquals(true , map.get("mypfx-sdncResponseSuccess")) assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode")) assertFalse(map.containsKey("WorkflowException")) } @Test public void testValidateSDNCResponse_408_200_WithEmbeddedLt() { utils.validateSDNCResponse(svcex, makeResp("408", "failed", makeReq("200", " message")), wfex, true) assertEquals(true, map.get("mypfx-sdncResponseSuccess")) assertEquals("200", map.get("mypfx-sdncRequestDataResponseCode")) assertFalse(map.containsKey("WorkflowException")) } @Test public void testUpdateHomingInfo() { String actual = utils.updateHomingInfo(null, "AIC3.0") println actual assertEquals("AIC3.0", actual) } @Test public void testUpdateHomingInfo2() { String homingInfo = "TESTCLLI" String actual = utils.updateHomingInfo(homingInfo, "AIC3.0") println actual assertEquals("TESTCLLIAIC3.0", actual) } private String makeResp(String respcode, String respmsg, String reqdata) { def rc = respcode def rm = respmsg return """ myreq ${MsoUtils.xmlEscape(rc)} ${MsoUtils.xmlEscape(rm)} ${reqdata} """ } private String makeReq(String respcode, String respmsg) { def rc = respcode def rm = respmsg String output = """ 8b46e36e-b44f-4085-9404-427be1bc8a3 ${MsoUtils.xmlEscape(rc)} ${MsoUtils.xmlEscape(rm)} Y """ output = output return """${MsoUtils.xmlEscape(output)}""" } }