diff options
Diffstat (limited to 'adapters/mso-sdnc-adapter/src/test/java/org/onap')
15 files changed, 1437 insertions, 0 deletions
diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/BaseTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/BaseTest.java new file mode 100644 index 0000000000..29a1db89aa --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/BaseTest.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc; + +import org.junit.After; +import org.junit.Rule; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.onap.so.cloud.Application; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +@AutoConfigureWireMock(port = 0) +public abstract class BaseTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + protected TestRestTemplate restTemplate = new TestRestTemplate("test", "test"); + + @Value("${wiremock.server.port}") + protected String wireMockPort; + + @After + public void after() { + + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/FileUtil.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/FileUtil.java new file mode 100644 index 0000000000..f2fba5900f --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/FileUtil.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc; + +import org.onap.so.logger.MsoLogger; + +import java.io.IOException; +import java.io.InputStream; + +/** + * file utility class + */ +public class FileUtil { + + private static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, FileUtil.class); + + /** + * Read the specified resource file and return the contents as a String. + * + * @param fileName Name of the resource file + * @return the contents of the resource file as a String + * @throws IOException if there is a problem reading the file + */ + public static String readResourceFile(String fileName) { + InputStream stream; + try { + stream = getResourceAsStream(fileName); + byte[] bytes; + bytes = new byte[stream.available()]; + if(stream.read(bytes) > 0) { + stream.close(); + return new String(bytes); + } else { + stream.close(); + return ""; + } + } catch (IOException e) { + LOGGER.debug("Exception:", e); + return ""; + } + } + + /** + * Get an InputStream for the resource specified. + * + * @param resourceName Name of resource for which to get InputStream. + * @return an InputStream for the resource specified. + * @throws IOException If we can't get the InputStream for whatever reason. + */ + private static InputStream getResourceAsStream(String resourceName) throws IOException { + InputStream stream = + FileUtil.class.getClassLoader().getResourceAsStream(resourceName); + if (stream == null) { + throw new IOException("Can't access resource '" + resourceName + "'"); + } + return stream; + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/ObjectFactoryTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/ObjectFactoryTest.java new file mode 100644 index 0000000000..7a9b039089 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/ObjectFactoryTest.java @@ -0,0 +1,113 @@ +/*- + * ============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.adapters.sdnc; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.containsString; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringWriter; +import java.nio.charset.Charset; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.junit.Test; + +public class ObjectFactoryTest { + + private Marshaller jaxbMarshaller; + private Unmarshaller jaxbUnmarshaller; + + /** + * Test method for {@link org.onap.so.adapters.sdnc.ObjectFactory#createRequestHeader()}. + */ + @Test + public final void testCreateRequestHeader () { + ObjectFactory of = new ObjectFactory (); + RequestHeader rh = of.createRequestHeader (); + rh.setCallbackUrl ("callback"); + rh.setMsoAction ("action"); + rh.setRequestId ("reqid"); + rh.setSvcAction ("svcAction"); + rh.setSvcInstanceId ("svcId"); + rh.setSvcOperation ("op"); + + try { + JAXBContext jaxbContext = JAXBContext.newInstance(RequestHeader.class); + jaxbMarshaller = jaxbContext.createMarshaller(); + + JAXBContext jaxbContext2 = JAXBContext.newInstance(RequestHeader.class); + jaxbUnmarshaller = jaxbContext2.createUnmarshaller(); + } + catch (JAXBException e) { + e.printStackTrace (); + fail(); + return; + } + + StringWriter writer = new StringWriter(); + try { + jaxbMarshaller.marshal (rh, writer); + } catch (JAXBException e) { + e.printStackTrace(); + fail (); + } + String marshalled = writer.toString (); + assertThat(marshalled, containsString("<RequestId>reqid</RequestId>")); + + InputStream inputStream = new ByteArrayInputStream(marshalled.getBytes(Charset.forName("UTF-8"))); + try { + RequestHeader res2 = (RequestHeader) jaxbUnmarshaller.unmarshal (inputStream); + assertEquals("callback", res2.getCallbackUrl ()); + assertEquals("action", res2.getMsoAction ()); + assertEquals("op", res2.getSvcOperation ()); + } catch (JAXBException e) { + e.printStackTrace(); + fail(); + } + } + + /** + * Test method for {@link org.onap.so.adapters.sdnc.ObjectFactory#createSDNCAdapterResponse()}. + */ + @Test + public final void testCreateSDNCAdapterResponse () { + ObjectFactory of = new ObjectFactory (); + SDNCAdapterResponse ar = of.createSDNCAdapterResponse (); + assertNotNull(ar); + } + + @Test + public final void testCreateSDNCAdapterRequest () { + ObjectFactory of = new ObjectFactory (); + SDNCAdapterRequest ar = of.createSDNCAdapterRequest (); + assertNotNull(ar); + } + +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/SDNCAdapterRequestTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/SDNCAdapterRequestTest.java new file mode 100644 index 0000000000..e0a1982db1 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/SDNCAdapterRequestTest.java @@ -0,0 +1,57 @@ +/*- + * ============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.adapters.sdnc; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.so.adapters.sdnc.SDNCAdapterRequest; +import org.onap.so.adapters.sdnc.RequestHeader; + + +public class SDNCAdapterRequestTest { + + static Object sd= new SDNCAdapterRequest(); + static RequestHeader rh=new RequestHeader(); + + @BeforeClass + public static final void RHeader() + { + rh.setCallbackUrl("callback"); + rh.setMsoAction ("action"); + rh.setRequestId ("reqid"); + rh.setSvcAction ("svcAction"); + rh.setSvcInstanceId ("svcId"); + rh.setSvcOperation ("op"); + } + @Test + public final void testtoString(){ + ((SDNCAdapterRequest) sd).setRequestData("data"); + ((SDNCAdapterRequest) sd).setRequestHeader(rh); + assertNotNull(((SDNCAdapterRequest) sd).getRequestData()) ; + assertEquals("data", ((SDNCAdapterRequest) sd).getRequestData()); + assertEquals(rh, ((SDNCAdapterRequest) sd).getRequestHeader()); + } + +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/CallbackHeaderTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/CallbackHeaderTest.java new file mode 100644 index 0000000000..dbb5c9aa44 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/CallbackHeaderTest.java @@ -0,0 +1,49 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * 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.adapters.sdnc.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class CallbackHeaderTest { + + static CallbackHeader cb = new CallbackHeader(); + + @Test + public final void testCallbackHeader() { + cb.setRequestId("413658f4-7f42-482e-b834-23a5c15657da-1474471336781"); + cb.setResponseCode("200"); + cb.setResponseMessage("OK"); + assertNotNull(cb.getRequestId()); + assertNotNull(cb.getResponseCode()); + assertNotNull(cb.getResponseMessage()); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", cb.getRequestId()); + assertEquals("200", cb.getResponseCode()); + assertEquals("OK", cb.getResponseMessage()); + } + + @Test + public void testtoString() { + assertNotNull(cb.toString()); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/SDNCAdapterCallbackRequestTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/SDNCAdapterCallbackRequestTest.java new file mode 100644 index 0000000000..d7f4f8e6f7 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/client/SDNCAdapterCallbackRequestTest.java @@ -0,0 +1,57 @@ +/*- + * ============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.adapters.sdnc.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.onap.so.adapters.sdnc.client.CallbackHeader; +import org.onap.so.adapters.sdnc.client.SDNCAdapterCallbackRequest; + +public class SDNCAdapterCallbackRequestTest { + + static SDNCAdapterCallbackRequest sdc = new SDNCAdapterCallbackRequest(); + static CallbackHeader ch = new CallbackHeader("413658f4-7f42-482e-b834-23a5c15657da-1474471336781","200","OK"); + + @Test + public void testSDNCAdapterCallbackRequest() + { + sdc.setCallbackHeader(ch); + sdc.setRequestData("data"); + assertNotNull(sdc.getCallbackHeader()); + assertNotNull(sdc.getRequestData()); + assertEquals(ch, sdc.getCallbackHeader()); + assertEquals("data", sdc.getRequestData()); + + } + + @Test + public void testtoString() + { + assertNotNull(ch.toString()); + assertNotNull(sdc.toString()); + } + +} + + diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/MapRequestTunablesTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/MapRequestTunablesTest.java new file mode 100644 index 0000000000..cd93f6e71b --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/MapRequestTunablesTest.java @@ -0,0 +1,128 @@ +/*- + * ============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.adapters.sdnc.impl; + +import static com.shazam.shazamcrest.MatcherAssert.assertThat; +import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.so.adapters.sdnc.SDNCAdapterApplication; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SDNCAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles("test") +public class MapRequestTunablesTest { + + @Autowired + private MapRequestTunables tunableMapper; + + @Test + public void test_setTunables(){ + RequestTunables expectedResult = new RequestTunables("testReqId", "","vnf-topology-operation","assign"); + expectedResult.setAsyncInd("N"); + expectedResult.setSdncUrl("https://localhost:8443/restconf/operations/VNF-API:vnf-topology-operation"); + expectedResult.setTimeout("270000"); + expectedResult.setReqMethod("POST"); + expectedResult.setHeaderName("sdnc-request-header"); + expectedResult.setNamespace("org:openecomp:sdnctl:vnf"); + + RequestTunables testMapper = new RequestTunables("testReqId", "","vnf-topology-operation","assign"); + + RequestTunables mappedTunable = tunableMapper.setTunables(testMapper); + + assertThat(mappedTunable, sameBeanAs(expectedResult)); + } + + @Test + public void test_setTunables_EmptyOperation_EmptyMSOAction(){ + RequestTunables expectedResult = new RequestTunables("testReqId", "","","query"); + expectedResult.setAsyncInd("N"); + expectedResult.setSdncUrl("http://localhost:8443/restconf/operations/GENERIC-RESOURCE-API:"); + expectedResult.setTimeout("60000"); + expectedResult.setReqMethod("GET"); + expectedResult.setHeaderName("sdnc-request-header"); + expectedResult.setNamespace(""); + + RequestTunables testMapper = new RequestTunables("testReqId", "","","query"); + + RequestTunables mappedTunable = tunableMapper.setTunables(testMapper); + + assertThat(mappedTunable, sameBeanAs(expectedResult)); + } + + @Test + public void test_setTunables_EmptyOperation(){ + RequestTunables expectedResult = new RequestTunables("testReqId", "infra","","query"); + expectedResult.setAsyncInd("N"); + expectedResult.setSdncUrl("https://localhost:8443/restconf/config"); + expectedResult.setTimeout("60000"); + expectedResult.setReqMethod("GET"); + expectedResult.setHeaderName("sdnc-request-header"); + expectedResult.setNamespace(""); + + RequestTunables testMapper = new RequestTunables("testReqId", "infra","","query"); + + RequestTunables mappedTunable = tunableMapper.setTunables(testMapper); + + assertThat(mappedTunable, sameBeanAs(expectedResult)); + } + + @Test + public void test_setTunables_EmptyOperation_EmptyMSOActionPUT(){ + RequestTunables expectedResult = new RequestTunables("testReqId", "","","put"); + expectedResult.setAsyncInd("N"); + expectedResult.setSdncUrl("https://localhost:8443/restconf/config"); + expectedResult.setTimeout("60000"); + expectedResult.setReqMethod("PUT"); + expectedResult.setHeaderName("sdnc-request-header"); + expectedResult.setNamespace(""); + + RequestTunables testMapper = new RequestTunables("testReqId", "","","put"); + + RequestTunables mappedTunable = tunableMapper.setTunables(testMapper); + + assertThat(mappedTunable, sameBeanAs(expectedResult)); + } + + + @Test + public void test_setTunables_EmptyOperation_EmptyMSOActionRESTDELETE(){ + RequestTunables expectedResult = new RequestTunables("testReqId", "","","restdelete"); + expectedResult.setAsyncInd("N"); + expectedResult.setSdncUrl("https://localhost:8443/restconf/config"); + expectedResult.setTimeout("60000"); + expectedResult.setReqMethod("DELETE"); + expectedResult.setHeaderName("sdnc-request-header"); + expectedResult.setNamespace(""); + + RequestTunables testMapper = new RequestTunables("testReqId", "","","restdelete"); + + RequestTunables mappedTunable = tunableMapper.setTunables(testMapper); + + assertThat(mappedTunable, sameBeanAs(expectedResult)); + } + +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCAdapterPortTypeImplTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCAdapterPortTypeImplTest.java new file mode 100644 index 0000000000..8f73c02285 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCAdapterPortTypeImplTest.java @@ -0,0 +1,111 @@ +/*- + * ============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.adapters.sdnc.impl; + +import static org.junit.Assert.fail; + +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.so.adapters.sdnc.RequestHeader; +import org.onap.so.adapters.sdnc.SDNCAdapterApplication; +import org.onap.so.adapters.sdnc.SDNCAdapterPortType; +import org.onap.so.adapters.sdnc.SDNCAdapterRequest; +import org.onap.so.adapters.sdnc.SDNCAdapterResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SDNCAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@ActiveProfiles({"test","non-async"}) +public class SDNCAdapterPortTypeImplTest { + + @Autowired + private SDNCAdapterPortType sdncAdapter; + + + SDNCAdapterRequest sdncAdapterRequest; + + public void setupTestEntities() throws ParserConfigurationException, SAXException, IOException, TransformerException { + buildTestRequest(); + } + + private void buildTestRequest() throws ParserConfigurationException, SAXException, IOException, TransformerException { + sdncAdapterRequest= new SDNCAdapterRequest(); + File fXmlFile = new File("src/test/resources/sdncTestPayload.xml"); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(fXmlFile); + StringWriter sw = new StringWriter(); + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + + transformer.transform(new DOMSource(doc), new StreamResult(sw)); + System.out.println(sw.toString()); + sdncAdapterRequest.setRequestData(sw.toString()); + RequestHeader requestHeader = new RequestHeader(); + requestHeader.setCallbackUrl("http://localhost:9090/callback"); + requestHeader.setMsoAction("gammainternet"); + requestHeader.setRequestId("testReqId"); + requestHeader.setSvcAction("assign"); + requestHeader.setSvcInstanceId("servInstanceId"); + requestHeader.setSvcOperation("svc-topology-operation"); + sdncAdapterRequest.setRequestHeader(requestHeader ); + } + + + + @Test + public void sendRequest() throws ParserConfigurationException, SAXException, IOException, TransformerException { + // Given + setupTestEntities(); + + // When + SDNCAdapterResponse response = sdncAdapter.sdncAdapter(sdncAdapterRequest); + if(response ==null) + fail("Null infraRequest"); + + // Then + //assertThat(infraRequest, sameBeanAs(testRequest).ignoring("requestBody").ignoring("endTime").ignoring("startTime").ignoring("modifyTime")); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCResponseTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCResponseTest.java new file mode 100644 index 0000000000..498fa3fa6d --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCResponseTest.java @@ -0,0 +1,49 @@ +/* +* ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 TechMahindra + * ================================================================================ + * 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.adapters.sdnc.impl; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class SDNCResponseTest { + + private SDNCResponse sdncresponse = new SDNCResponse(null, 0, null); + + @Test + public void testSDNCResponse() + { + sdncresponse.setReqId("reqId"); + sdncresponse.setRespCode(0); + sdncresponse.setRespMsg("respMsg"); + sdncresponse.setSdncRespXml("sdncRespXml"); + assertEquals(sdncresponse.getReqId(), "reqId"); + assertEquals(sdncresponse.getRespCode(), 0); + assertEquals(sdncresponse.getRespMsg(), "respMsg"); + assertEquals(sdncresponse.getSdncRespXml(),"sdncRespXml"); + } + + @Test + public void testtoString() + { + assertNotNull(sdncresponse.toString()); + } +} + diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCRestClientTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCRestClientTest.java new file mode 100644 index 0000000000..076a9f3c6a --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/SDNCRestClientTest.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc.impl; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static org.junit.Assert.assertNotNull; + +import org.apache.http.HttpStatus; +import org.junit.Test; +import org.onap.so.adapters.sdnc.BaseTest; +import org.springframework.beans.factory.annotation.Autowired; +import static com.github.tomakehurst.wiremock.client.WireMock.*; + + +public class SDNCRestClientTest extends BaseTest { + + @Autowired + private SDNCRestClient sdncClient; + + @Test + public void getSdncRespTestException() { + + RequestTunables rt = new RequestTunables("", "", "", ""); + rt.setTimeout("1000"); + rt.setReqMethod("POST"); + rt.setSdncUrl("http://localhost:" + wireMockPort + "/sdnc"); + + stubFor(post(urlPathEqualTo("/sdnc")) + .willReturn(aResponse().withHeader("Content-Type", "application/xml").withBody("").withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR))); + + SDNCResponse response = sdncClient.getSdncResp("", rt); + assertNotNull(response); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/UtilsTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/UtilsTest.java new file mode 100644 index 0000000000..596f26caa6 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/impl/UtilsTest.java @@ -0,0 +1,64 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc.impl; + +import static org.junit.Assert.assertEquals; +import java.io.File; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import org.junit.Test; +import org.onap.so.adapters.sdnc.SDNCAdapterRequest; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +public class UtilsTest { + + @Test + public final void testUnmarshal () { + + String expectedXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><feature-list xmlns=\"com:att:sdnctl:l3api\"><feature-type>FIREWALL-LITE</feature-type><feature-instance-id>mtjnj40evbc0eceb</feature-instance-id><feature-sub-type>SHARED</feature-sub-type><feature-instance-xpath>/restconf/config/Firewall-API:feature-model/feature-list/FIREWALL-LITE/mtjnj40evbc0eceb/</feature-instance-xpath> </feature-list>"; + + try { + + File file = new File("src/test/resources/sdncBpmnAdiodFirewallRequest.xml"); + JAXBContext jaxbContext = JAXBContext.newInstance(SDNCAdapterRequest.class); + + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + SDNCAdapterRequest request = (SDNCAdapterRequest) jaxbUnmarshaller.unmarshal(file); + + RequestTunables rt = new RequestTunables("0460ba40-60c8-4b07-8878-c8e8d87cde04-1527983610512", + "", + "/L3SDN-API:services/layer3-service-list/MIS%2F1806%2F25057%2FSW_INTERNET/service-data/feature-list/FIREWALL-LITE/", + "put"); + + Node node = (Node) request.getRequestData(); + Document reqDoc = node.getOwnerDocument(); + String sdncReqBody = Utils.genSdncPutReq(reqDoc, rt); + assertEquals(sdncReqBody.replaceAll("[\\t\\n\\r]+", ""), expectedXml); + + } catch (JAXBException e) { + e.printStackTrace(); + } + + } + +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/ObjectMappingTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/ObjectMappingTest.java new file mode 100644 index 0000000000..88849015e0 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/ObjectMappingTest.java @@ -0,0 +1,479 @@ +/*- + * ============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.adapters.sdnc.sdncrest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.Arrays; + +import org.junit.Test; +import org.onap.so.adapters.sdncrest.SDNCEvent; +import org.onap.so.adapters.sdncrest.SDNCServiceError; +import org.onap.so.adapters.sdncrest.SDNCServiceRequest; +import org.onap.so.adapters.sdncrest.SDNCServiceResponse; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + + + +/** + * JSON object mapping tests. + */ +public class ObjectMappingTest { + private static final String EOL = "\n"; + + private final String SDNC_SERVICE_REQUEST = + "{" + EOL + + " \"SDNCServiceRequest\": {" + EOL + + " \"requestInformation\": {" + EOL + + " \"requestId\": \"413658f4-7f42-482e-b834-23a5c15657da\"," + EOL + + " \"source\": \"CCD\"," + EOL + + " \"notificationUrl\": \"https://ccd-host:8080/notifications\"" + EOL + + " }," + EOL + + " \"serviceInformation\": {" + EOL + + " \"serviceType\": \"vHNFaaS\"," + EOL + + " \"serviceInstanceId\": \"74e65b2b637441bca078e63e44bb511b\"," + EOL + + " \"subscriberName\": \"IST_SG_0902_3003\"," + EOL + + " \"subscriberGlobalId\": \"IST15_0902_3003\"" + EOL + + " }," + EOL + + " \"bpNotificationUrl\": \"http://localhost:8080/mso/SDNCAdapterCallbackService\"," + EOL + + "((BP-TIMEOUT))" + + " \"sdncRequestId\": \"413658f4-7f42-482e-b834-23a5c15657da-1474471336781\"," + EOL + + " \"sdncService\": \"vhnf\"," + EOL + + " \"sdncOperation\": \"service-topology-cust-assign-operation\"," + EOL + + " \"sdncServiceDataType\": \"XML\"," + EOL + + " \"sdncServiceData\": \"<vhnf-cust-stage-information><dhv-service-instance-id>c26dfed652164d60a17461734422b085</dhv-service-instance-id><hnportal-primary-vnf-host-name>HOSTNAME</hnportal-primary-vnf-host-name></vhnf-cust-stage-information>\"" + EOL + + " }" + EOL + + "}" + EOL; + + private final String SDNC_SERVICE_RESPONSE = + "{" + EOL + + " \"SDNCServiceResponse\": {" + EOL + + " \"sdncRequestId\": \"413658f4-7f42-482e-b834-23a5c15657da-1474471336781\"," + EOL + + " \"responseCode\": \"200\"," + EOL + + "((RESPONSE-MESSAGE))" + + " \"ackFinalIndicator\": \"Y\"" + EOL + + "((RESPONSE-PARAMS))" + + " }" + EOL + + "}" + EOL; + + private final String SDNC_SERVICE_ERROR = + "{" + EOL + + " \"SDNCServiceError\": {" + EOL + + " \"sdncRequestId\": \"413658f4-7f42-482e-b834-23a5c15657da-1474471336781\"," + EOL + + " \"responseCode\": \"500\"," + EOL + + "((RESPONSE-MESSAGE))" + + " \"ackFinalIndicator\": \"Y\"" + EOL + + " }" + EOL + + "}" + EOL; + + private final String SDNC_EVENT = + "{" + EOL + + " \"SDNCEvent\": {" + EOL + + " \"eventType\": \"ACTIVATION\"," + EOL + + " \"eventCorrelatorType\": \"HOST-NAME\"," + EOL + + " \"eventCorrelator\": \"USOSTCDALTX0101UJZZ31\"" + EOL + + "((EVENT-PARAMS))" + + " }" + EOL + + "}" + EOL; + + private final String PARAMS = + "{\"P1\":\"V1\",\"P2\":\"V2\",\"P3\":\"V3\"}"; + + @Test + public final void jsonToSDNCServiceRequest() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + String json = SDNC_SERVICE_REQUEST; + json = json.replace("((BP-TIMEOUT))", "\"bpTimeout\": \"" + "PT5M" + "\"," + EOL); + + SDNCServiceRequest object = mapper.readValue(json, SDNCServiceRequest.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da", object.getRequestInformation().getRequestId()); + assertEquals("CCD", object.getRequestInformation().getSource()); + assertEquals("https://ccd-host:8080/notifications", object.getRequestInformation().getNotificationUrl()); + assertEquals("vHNFaaS", object.getServiceInformation().getServiceType()); + assertEquals("74e65b2b637441bca078e63e44bb511b", object.getServiceInformation().getServiceInstanceId()); + assertEquals("IST_SG_0902_3003", object.getServiceInformation().getSubscriberName()); + assertEquals("IST15_0902_3003", object.getServiceInformation().getSubscriberGlobalId()); + assertEquals("http://localhost:8080/mso/SDNCAdapterCallbackService", object.getBPNotificationUrl()); + assertEquals("PT5M", object.getBPTimeout()); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("vhnf", object.getSdncService()); + assertEquals("service-topology-cust-assign-operation", object.getSdncOperation()); + assertEquals("XML", object.getSdncServiceDataType()); + assertTrue(object.getSdncServiceData().startsWith("<vhnf-cust-stage-information>")); + } + + @Test + public final void jsonToSDNCServiceRequestWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // bpTimeout is optional. + String json = SDNC_SERVICE_REQUEST; + json = json.replace("((BP-TIMEOUT))", ""); + + SDNCServiceRequest object = mapper.readValue(json, SDNCServiceRequest.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da", object.getRequestInformation().getRequestId()); + assertEquals("CCD", object.getRequestInformation().getSource()); + assertEquals("https://ccd-host:8080/notifications", object.getRequestInformation().getNotificationUrl()); + assertEquals("vHNFaaS", object.getServiceInformation().getServiceType()); + assertEquals("74e65b2b637441bca078e63e44bb511b", object.getServiceInformation().getServiceInstanceId()); + assertEquals("IST_SG_0902_3003", object.getServiceInformation().getSubscriberName()); + assertEquals("IST15_0902_3003", object.getServiceInformation().getSubscriberGlobalId()); + assertEquals("http://localhost:8080/mso/SDNCAdapterCallbackService", object.getBPNotificationUrl()); + assertNull(object.getBPTimeout()); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("vhnf", object.getSdncService()); + assertEquals("service-topology-cust-assign-operation", object.getSdncOperation()); + assertEquals("XML", object.getSdncServiceDataType()); + assertTrue(object.getSdncServiceData().startsWith("<vhnf-cust-stage-information>")); + } + + @Test + public final void jsonFromSDNCServiceRequest() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_REQUEST; + json1 = json1.replace("((BP-TIMEOUT))", "\"bpTimeout\": \"" + "PT5M" + "\"," + EOL); + SDNCServiceRequest object1 = mapper.readValue(json1, SDNCServiceRequest.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceRequest\":{")); + + // Convert generated json string to another object. + SDNCServiceRequest object2 = mapper.readValue(json2, SDNCServiceRequest.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonFromSDNCServiceRequestWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_REQUEST; + json1 = json1.replace("((BP-TIMEOUT))", ""); + SDNCServiceRequest object1 = mapper.readValue(json1, SDNCServiceRequest.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceRequest\":{")); + + // Convert generated json string to another object. + SDNCServiceRequest object2 = mapper.readValue(json2, SDNCServiceRequest.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonToSDNCServiceResponse() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + String json = SDNC_SERVICE_RESPONSE; + json = json.replace("((RESPONSE-MESSAGE))", " \"responseMessage\": \"" + "OK" + "\"," + EOL); + json = json.replace(EOL + "((RESPONSE-PARAMS))", "," + EOL + " \"params\": " + PARAMS + EOL); + + SDNCServiceResponse object = mapper.readValue(json, SDNCServiceResponse.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("200", object.getResponseCode()); + assertEquals("OK", object.getResponseMessage()); + assertEquals("Y", object.getAckFinalIndicator()); + assertEquals("V1", object.getParams().get("P1")); + assertEquals("V2", object.getParams().get("P2")); + assertEquals("V3", object.getParams().get("P3")); + } + + @Test + public final void jsonToSDNCServiceResponseWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // responseMessage is optional. + String json = SDNC_SERVICE_RESPONSE; + json = json.replace("((RESPONSE-MESSAGE))", ""); + json = json.replace("((RESPONSE-PARAMS))", ""); + + SDNCServiceResponse object = mapper.readValue(json, SDNCServiceResponse.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("200", object.getResponseCode()); + assertNull(object.getResponseMessage()); + assertEquals("Y", object.getAckFinalIndicator()); + assertNull(object.getParams()); + } + + @Test + public final void jsonFromSDNCServiceResponse() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_RESPONSE; + json1 = json1.replace("((RESPONSE-MESSAGE))", "\"responseMessage\": \"" + "OK" + "\"," + EOL); + json1 = json1.replace(EOL + "((RESPONSE-PARAMS))", "," + EOL + " \"params\": " + PARAMS + EOL); + SDNCServiceResponse object1 = mapper.readValue(json1, SDNCServiceResponse.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceResponse\":{")); + + // Convert generated json string to another object. + SDNCServiceResponse object2 = mapper.readValue(json2, SDNCServiceResponse.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonFromSDNCServiceResponseWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_RESPONSE; + json1 = json1.replace("((RESPONSE-MESSAGE))", ""); + json1 = json1.replace("((RESPONSE-PARAMS))", ""); + SDNCServiceResponse object1 = mapper.readValue(json1, SDNCServiceResponse.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceResponse\":{")); + + // Convert generated json string to another object. + SDNCServiceResponse object2 = mapper.readValue(json2, SDNCServiceResponse.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonToSDNCServiceError() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + String json = SDNC_SERVICE_ERROR; + json = json.replace("((RESPONSE-MESSAGE))", "\"responseMessage\": \"" + "SOMETHING BAD" + "\"," + EOL); + + SDNCServiceError object = mapper.readValue(json, SDNCServiceError.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("500", object.getResponseCode()); + assertEquals("SOMETHING BAD", object.getResponseMessage()); + assertEquals("Y", object.getAckFinalIndicator()); + } + + @Test + public final void jsonToSDNCServiceErrorWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // responseMessage is optional. + String json = SDNC_SERVICE_ERROR; + json = json.replace("((RESPONSE-MESSAGE))", ""); + + SDNCServiceError object = mapper.readValue(json, SDNCServiceError.class); + assertEquals("413658f4-7f42-482e-b834-23a5c15657da-1474471336781", object.getSdncRequestId()); + assertEquals("500", object.getResponseCode()); + assertNull(object.getResponseMessage()); + assertEquals("Y", object.getAckFinalIndicator()); + } + + @Test + public final void jsonFromSDNCServiceError() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_ERROR; + json1 = json1.replace("((RESPONSE-MESSAGE))", "\"responseMessage\": \"" + "OK" + "\"," + EOL); + SDNCServiceError object1 = mapper.readValue(json1, SDNCServiceError.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceError\":{")); + + // Convert generated json string to another object. + SDNCServiceError object2 = mapper.readValue(json2, SDNCServiceError.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonFromSDNCServiceErrorWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_SERVICE_ERROR; + json1 = json1.replace("((RESPONSE-MESSAGE))", ""); + SDNCServiceError object1 = mapper.readValue(json1, SDNCServiceError.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCServiceError\":{")); + + // Convert generated json string to another object. + SDNCServiceError object2 = mapper.readValue(json2, SDNCServiceError.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + @Test + public final void jsonToSDNCEvent() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + String json = SDNC_EVENT; + json = json.replace(EOL + "((EVENT-PARAMS))", "," + EOL + " \"params\": " + PARAMS + EOL); + + SDNCEvent object = mapper.readValue(json, SDNCEvent.class); + assertEquals("ACTIVATION", object.getEventType()); + assertEquals("HOST-NAME", object.getEventCorrelatorType()); + assertEquals("USOSTCDALTX0101UJZZ31", object.getEventCorrelator()); + assertEquals("V1", object.getParams().get("P1")); + assertEquals("V2", object.getParams().get("P2")); + assertEquals("V3", object.getParams().get("P3")); + } + + @Test + public final void jsonToSDNCEventWithoutOptionalFields() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // params are optional. + String json = SDNC_EVENT; + json = json.replace("((EVENT-PARAMS))", ""); + + SDNCEvent object = mapper.readValue(json, SDNCEvent.class); + assertEquals("ACTIVATION", object.getEventType()); + assertEquals("HOST-NAME", object.getEventCorrelatorType()); + assertEquals("USOSTCDALTX0101UJZZ31", object.getEventCorrelator()); + assertNull(object.getParams()); + } + + @Test + public final void jsonFromSDNCEvent() throws Exception { + logTest(); + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); + mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); + + // Convert source json string to object. + String json1 = SDNC_EVENT; + json1 = json1.replace(EOL + "((EVENT-PARAMS))", "," + EOL + " \"params\": " + PARAMS + EOL); + SDNCEvent object1 = mapper.readValue(json1, SDNCEvent.class); + + // Convert resulting object back to json. + String json2 = object1.toJson(); + System.out.println("Generated JSON for " + object1.getClass().getSimpleName() + + ":" + System.lineSeparator() + json2); + assertTrue(json2.replaceAll("\\s+","").startsWith("{\"SDNCEvent\":{")); + + // Convert generated json string to another object. + SDNCEvent object2 = mapper.readValue(json2, SDNCEvent.class); + + // Compare the first object to the second object. + assertTrue(serializedEquals(object1, object2)); + } + + /** + * Tests equality of two objects by comparing their serialized form. + * WARNING: this works pretty well as long as the objects don't contain + * collections like maps and sets that are semantically equal, but have + * different internal ordering of elements. + */ + private boolean serializedEquals(Serializable object1, Serializable object2) throws IOException { + ByteArrayOutputStream byteStream1 = new ByteArrayOutputStream(); + ObjectOutputStream objectStream1 = new ObjectOutputStream(byteStream1); + objectStream1.writeObject(object1); + objectStream1.close(); + + ByteArrayOutputStream byteStream2 = new ByteArrayOutputStream(); + ObjectOutputStream objectStream2 = new ObjectOutputStream(byteStream2); + objectStream2.writeObject(object2); + objectStream2.close(); + + return Arrays.equals(byteStream1.toByteArray(), byteStream2.toByteArray()); + } + + private void logTest() { + StackTraceElement[] st = Thread.currentThread().getStackTrace(); + String method = st[2].getMethodName(); + System.out.println("RUNNING TEST: " + method); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnectorTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnectorTest.java new file mode 100644 index 0000000000..23830ae3f3 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCConnectorTest.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc.sdncrest; + +import org.apache.http.HttpStatus; +import org.junit.Test; +import org.onap.so.adapters.sdnc.BaseTest; +import org.onap.so.adapters.sdncrest.SDNCResponseCommon; +import org.springframework.beans.factory.annotation.Autowired; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static org.junit.Assert.assertNotNull; +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class SDNCConnectorTest extends BaseTest { + + @Autowired + private SDNCConnector sdncConnector; + + @Test + public void sendTest() { + String content = "<dummy><service-instance-id>1234</service-instance-id></dummy>"; + + String response = "<errors xmlns=\"urn:ietf:params:xml:ns:yang:ietf-restconf\">\n" + + "\t\t// <error>\n" + + "\t\t// <error-type>protocol</error-type>\n" + + "\t\t// <error-tag>malformed-message</error-tag>\n" + + "\t\t// <error-message>Error parsing input: The element type \"input\" must be terminated by the matching end-tag \"</input>\".</error-message>\n" + + "\t\t// </error>\n" + + "\t\t// </errors>"; + + TypedRequestTunables rt = new TypedRequestTunables("", ""); + rt.setTimeout("1000"); + rt.setReqMethod("POST"); + rt.setSdncUrl("http://localhost:" + wireMockPort + "/sdnc"); + + stubFor(post(urlPathEqualTo("/sdnc")) + .willReturn(aResponse().withHeader("Content-Type", "application/xml").withBody(response).withStatus(HttpStatus.SC_MULTIPLE_CHOICES))); + + SDNCResponseCommon responseCommon = sdncConnector.send(content, rt); + assertNotNull(responseCommon); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java new file mode 100644 index 0000000000..eddd74b213 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/sdncrest/SDNCServiceRequestConnectorTest.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 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.adapters.sdnc.sdncrest; + +import org.junit.Test; +import org.onap.so.adapters.sdnc.FileUtil; +import org.onap.so.adapters.sdncrest.SDNCResponseCommon; + +import static org.junit.Assert.assertNotNull; + +public class SDNCServiceRequestConnectorTest { + + @Test + public void parseResponseContentTest() throws Exception { + + String content = FileUtil.readResourceFile("SdncServiceResponse.xml"); + SDNCResponseCommon responseCommon = SDNCServiceRequestConnector.parseResponseContent(content); + + assertNotNull(responseCommon); + } +} diff --git a/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/util/SDNCRequestIdUtilTest.java b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/util/SDNCRequestIdUtilTest.java new file mode 100644 index 0000000000..7ba8b68602 --- /dev/null +++ b/adapters/mso-sdnc-adapter/src/test/java/org/onap/so/adapters/sdnc/util/SDNCRequestIdUtilTest.java @@ -0,0 +1,46 @@ +/*- + * ============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.adapters.sdnc.util; + +import static org.junit.Assert.assertEquals; + +import java.util.UUID; + +import org.junit.Test; + + +public class SDNCRequestIdUtilTest { + + /** + * Test method for {@link org.onap.so.adapters.sdnc.SDNCRequestIdUtil#getSDNCOriginalRequestId()}. + */ + @Test + public final void testGetSDNCOriginalRequestId () { + String originalRequestId = UUID.randomUUID().toString(); + String postfixedRequestId = originalRequestId + "-1466203712068"; + String postfixedRequestId2 = originalRequestId + "-1466203712068-2"; + + assertEquals(originalRequestId, SDNCRequestIdUtil.getSDNCOriginalRequestId(postfixedRequestId)); + assertEquals(postfixedRequestId2, SDNCRequestIdUtil.getSDNCOriginalRequestId(postfixedRequestId2)); + + } + +} |