diff options
Diffstat (limited to 'restconf-client')
6 files changed, 415 insertions, 49 deletions
diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java index 91ce3338..a76b27d0 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiCallNode.java @@ -20,6 +20,14 @@ package org.onap.ccsdk.sli.plugins.restconfapicall; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonWriter; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin; @@ -35,30 +43,39 @@ import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.MdsalSerializerHe import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.SerializerHelper; import org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.YangParameters; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeSerializer; +import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.Namespace; import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.PropertiesNodeSerializer; import org.opendaylight.restconf.common.context.InstanceIdentifierContext; import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.ws.rs.core.UriBuilder; +import java.io.StringWriter; +import java.io.Writer; import java.net.SocketException; import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; +import static com.google.common.base.Strings.repeat; import static java.lang.String.format; +import static java.lang.String.valueOf; import static org.apache.commons.lang3.StringUtils.join; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.DELETE; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; -import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.ATTEMPTS_MSG; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COLON; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMMA; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.COMM_FAIL; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.HEADER; @@ -74,10 +91,16 @@ import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RES_PR import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_COUNT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.RETRY_FAIL; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.UPDATED_URL; +import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getUpdatedXmlReq; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getSchemaCtxFromDir; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.getYangParameters; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfListenerFactory.instance; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.FORMAT_ERR; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.UTF_HEADER; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.XML_TREE_ERR; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DfSerializerUtil.getXmlWriter; +import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.MdsalPropertiesNodeUtils.getModuleNamespace; import static org.osgi.framework.FrameworkUtil.getBundle; /** @@ -154,9 +177,11 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { InstanceIdentifierContext<?> insIdCtx = getInsIdCtx(p, uri); String req = null; - if (p.httpMethod == POST || p.httpMethod == PUT - || p.httpMethod == PATCH) { + if (p.httpMethod != GET && p.httpMethod != DELETE) { req = serializeRequest(props, p, uri, insIdCtx); + if (p.httpMethod == PUT || p.httpMethod == PATCH) { + updateReq(req, p, insIdCtx); + } } if (req == null && p.requestBody != null) { req = p.requestBody; @@ -210,8 +235,8 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { } if (r != null && r.code >= 300) { - throw new SvcLogicException( - String.valueOf(r.code) + ": " + r.message); + throw new SvcLogicException(valueOf(r.code) + + COLON + " " + r.message); } } @@ -316,7 +341,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { */ public String getResponse(SvcLogicContext ctx, YangParameters params, String pre, HttpResponse res) { - ctx.setAttribute(pre + RES_CODE, String.valueOf(res.code)); + ctx.setAttribute(pre + RES_CODE, valueOf(res.code)); ctx.setAttribute(pre + RES_MSG, res.message); if (params.dumpHeaders && res.headers != null) { @@ -345,7 +370,7 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { HttpResponse res = new HttpResponse(); res.code = 500; res.message = errMsg; - ctx.setAttribute(prefix + RES_CODE, String.valueOf(res.code)); + ctx.setAttribute(prefix + RES_CODE, valueOf(res.code)); ctx.setAttribute(prefix + RES_MSG, res.message); } @@ -373,4 +398,114 @@ public class RestconfApiCallNode implements SvcLogicJavaPlugin { log.debug(UPDATED_URL + retryUri.toString()); log.debug(format(COMM_FAIL, hostName, retryString)); } + + /** + * Updates request message for JSON and XML data format, when the HTTP + * method points it as PUT or PATCH. + * + * @param req current request message + * @param p YANG parameters + * @param insIdCtx instance identifier context + * @return update request message + * @throws SvcLogicException when the data format type is wrong + */ + public String updateReq(String req, YangParameters p, + InstanceIdentifierContext<?> insIdCtx) + throws SvcLogicException { + + SchemaNode schemaNode = insIdCtx.getSchemaNode(); + Namespace modNs = getModuleNamespace(schemaNode.getQName(), + insIdCtx.getSchemaContext()); + String nodeName = schemaNode.getQName().getLocalName(); + + switch (p.format) { + case JSON: + return getUpdatedJsonReq(req, nodeName, modNs.moduleName()); + + case XML: + return getXmlReqForPutOp(req, nodeName, modNs.moduleNs()); + + default: + throw new SvcLogicException(format(FORMAT_ERR, p.format)); + } + } + + /** + * Returns the updated JSON request message, when the HTTP method + * points to PUT or PATCH. + * + * @param req current JSON request message + * @param nodeName root node name + * @param modName module name of the root node + * @return update JSON request message + */ + private String getUpdatedJsonReq(String req, String nodeName, + String modName) { + Writer writer = new StringWriter(); + JsonWriter jsonWriter = new JsonWriter(writer); + jsonWriter.setIndent(repeat(" ", 4)); + + JsonParser jsonParser = new JsonParser(); + JsonObject oldJson = (JsonObject)jsonParser.parse(req); + oldJson = remChildModName(oldJson, modName); + JsonObject newJson = new JsonObject(); + newJson.add(modName + COLON + nodeName, oldJson.deepCopy()); + + Gson gson= new Gson(); + gson.toJson(newJson, jsonWriter); + return writer.toString(); + } + + /** + * Removes module name from all the updated first level child node, if it + * is same as the root node added. + * + * @param oldJson JSON object for old request + * @param modName module name of root node + * @return JSON object for old request with updated child module name + */ + private JsonObject remChildModName(JsonObject oldJson, String modName) { + Iterator<Map.Entry<String, JsonElement>> it = oldJson.entrySet().iterator(); + Map<String, JsonElement> m = new HashMap<>(); + while (it.hasNext()) { + Map.Entry<String, JsonElement> jNode = it.next(); + if (jNode.getKey().contains(COLON)) { + String[] modArr = jNode.getKey().split(COLON); + if (modArr[0].equals(modName)) { + it.remove(); + m.put(modArr[1], jNode.getValue()); + } + } + } + if (!m.isEmpty()) { + for (Map.Entry<String, JsonElement> element : m.entrySet()) { + oldJson.add(element.getKey(), element.getValue()); + } + } + return oldJson; + } + + /** + * Returns the updated XML request message, when the HTTP method points + * to PUT or PATCH. + * + * @param req current JSON request message + * @param nodeName root node name + * @param modNs module namespace of the root node + * @return update JSON request message + * @throws SvcLogicException when XML parsing fails + */ + private String getXmlReqForPutOp(String req, String nodeName, + URI modNs) throws SvcLogicException { + req = getUpdatedXmlReq(req, nodeName, modNs.toString()); + Document oldDoc; + try { + oldDoc = DocumentHelper.parseText(req); + } catch (DocumentException e) { + throw new SvcLogicException(XML_TREE_ERR, e); + } + Writer writer = getXmlWriter( + UTF_HEADER + oldDoc.getRootElement().asXML(), "4"); + return writer.toString(); + } } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java index b51272f7..3a0b5fb3 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/restconfapicall/RestconfApiUtils.java @@ -41,7 +41,6 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.getParameters; import static org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode.parseParam; import static org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode.DEFAULT_MODE; @@ -66,6 +65,8 @@ public final class RestconfApiUtils { static final String COMMA = ","; + static final String COLON = ":"; + static final String HTTP_RES = "httpResponse"; static final String REST_API_URL = "restapiUrl"; @@ -97,9 +98,6 @@ public final class RestconfApiUtils { private static final String URL_SYNTAX = "The following URL cannot be " + "parsed into URI : "; - private static final String PUT_NODE_ERR = "The following URL does not " + - "contain minimum two nodes for PUT operation."; - private static final String YANG = ".yang"; private static final String YANG_FILE_ERR = "Unable to parse the YANG " + @@ -145,12 +143,6 @@ public final class RestconfApiUtils { String path = uri.getPath(); path = getParsedPath(path); - if (method == PUT) { - if (!path.contains(SLASH)) { - throw new SvcLogicException(PUT_NODE_ERR + url); - } - path = path.substring(0, path.lastIndexOf(SLASH)); - } return path; } @@ -162,15 +154,15 @@ public final class RestconfApiUtils { */ private static String getParsedPath(String path) { String firstHalf; - if (path.contains(":")) { - String[] p = path.split(":"); + if (path.contains(COLON)) { + String[] p = path.split(COLON); if (p[0].contains(SLASH)) { int slash = p[0].lastIndexOf(SLASH); firstHalf = p[0].substring(slash + 1); } else { firstHalf = p[0]; } - return firstHalf + ":" + p[1]; + return firstHalf + COLON + p[1]; } return path; } @@ -222,6 +214,13 @@ public final class RestconfApiUtils { } } + /** + * Processes all the obtained files by isolating all the YANG files from + * all the directory of the given path recursively. + * + * @param files files in the given path + * @param yangFiles YANG files list + */ private static void processFiles(File[] files, List<File> yangFiles) { for (File file : files) { if (file.isFile() && file.getName().endsWith(YANG)) { @@ -231,4 +230,20 @@ public final class RestconfApiUtils { } } } + + /** + * Returns the updated XML request message by adding root node to it. + * + * @param req XML request + * @param nodeName root node name + * @param modNs module namespace of the root node + * @return updated XML request message + */ + static String getUpdatedXmlReq(String req, String nodeName, String modNs) { + String rootNode = "\n<" + nodeName + " xmlns=\"" + modNs.toString() + + "\">\n"; + req = req.replaceFirst("\n", rootNode); + req = req + "</" + nodeName + ">"; + return req.replaceAll(">\\s+<", "><"); + } } diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java index fbebd2b2..707c2944 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DfSerializerUtil.java @@ -68,24 +68,12 @@ public final class DfSerializerUtil { static final String XML_LIS_ERR = "The XML serializer doesn't have XML " + "listener"; - static final String JSON_TREE_ERR = "Unable to form JSON tree object from" + - " the JSON body provided."; - - static final String XML_TREE_ERR = "Unable to form XML tree object from " + - "the XML body provided."; - - static final String FORMAT_ERR = "Only JSON and XML formats are supported" + - ". %s is not supported"; - static final String PROP_NODE_ERR = "The property node doesn't have " + "schema node bound to it."; static final String DF_ERR = "Type mismatch for the node %s. The schema " + "node does not match with the data format node type %s."; - static final String UTF_HEADER = "<?xml version=\"1.0\" " + - "encoding=\"UTF-8\"?>"; - static final String XML_PREFIX = "yangid"; private static final String YES = "yes"; @@ -98,6 +86,30 @@ public final class DfSerializerUtil { private static final String URI_ERR = "Unable to parse the URI"; + /** + * Data format error message for unsupported types. + */ + public static final String FORMAT_ERR = "Only JSON and XML formats are " + + "supported. %s is not supported"; + + /** + * UTF header message for XML data format message. + */ + public static final String UTF_HEADER = "<?xml version=\"1.0\" " + + "encoding=\"UTF-8\"?>"; + + /** + * Error message when a JSON tree creation fails. + */ + public static final String JSON_TREE_ERR = "Unable to form JSON tree " + + "object from the JSON body provided."; + + /** + * Error message when a XML tree creation fails. + */ + public static final String XML_TREE_ERR = "Unable to form XML tree object" + + " from the XML body provided."; + //No instantiation. private DfSerializerUtil() { } @@ -110,7 +122,7 @@ public final class DfSerializerUtil { * @return writer with XML * @throws SvcLogicException when transformation of source fails */ - static Writer getXmlWriter(String input, String indent) + public static Writer getXmlWriter(String input, String indent) throws SvcLogicException { try { Transformer transformer = TransformerFactory.newInstance() diff --git a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java index d7b4818b..ccc4f2bd 100644 --- a/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java +++ b/restconf-client/provider/src/main/java/org/onap/ccsdk/sli/plugins/yangserializers/pnserializer/MdsalPropertiesNodeUtils.java @@ -340,7 +340,15 @@ public final class MdsalPropertiesNodeUtils { return null; } - private static Namespace getModuleNamespace(QName qName, SchemaContext ctx) + /** + * Returns module namespace from a given qName. + * + * @param qName qName of a node + * @param ctx schema context + * @return module namespace of the node + * @throws SvcLogicException when the module is not available + */ + public static Namespace getModuleNamespace(QName qName, SchemaContext ctx) throws SvcLogicException { Optional<Module> module = ctx.findModule(qName.getModule()); if (!module.isPresent()) { diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java index 1185eea1..08fa33ee 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatSerializerTest.java @@ -42,19 +42,24 @@ import static org.mockito.Mockito.doCallRealMethod; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.GET; +import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PATCH; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.POST; import static org.onap.ccsdk.sli.plugins.restapicall.HttpMethod.PUT; import static org.onap.ccsdk.sli.plugins.restconfapicall.RestconfApiUtils.parseUrl; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.DECODE_FROM_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_ID_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_AUG_POST; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_JSON_YANG_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_ID_PUT; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_RPC; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG; import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_AUG_POST; +import static org.onap.ccsdk.sli.plugins.yangserializers.dfserializer.DataFormatUtilsTest.ENCODE_TO_XML_YANG_PUT; /** @@ -102,6 +107,9 @@ public class DataFormatSerializerTest { doAnswer(dfCaptor).when(restconf).serializeRequest( any(Map.class), any(YangParameters.class), any(String.class), any(InstanceIdentifierContext.class)); + doAnswer(dfCaptor).when(restconf).updateReq( + any(String.class), any(YangParameters.class), + any(InstanceIdentifierContext.class)); } /** @@ -141,6 +149,46 @@ public class DataFormatSerializerTest { } /** + * Verifies encoding of parameters to JSON data format with identity-ref + * and inter-file linking for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonIdWithPut() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + ctx.setAttribute(pre + "l", "abc"); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT)); + } + + /** + * Verifies encoding of parameters to JSON data format with identity-ref + * and inter-file linking for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonIdWithPatch() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + ctx.setAttribute(pre + "l", "abc"); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_ID_PUT)); + } + + /** * Verifies encoding of parameters to XML data format with identity-ref * and inter-file linking. * @@ -160,6 +208,44 @@ public class DataFormatSerializerTest { } /** + * Verifies encoding of parameters to XML data format with identity-ref + * and inter-file linking for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlIdWithPut() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT)); + } + + /** + * Verifies encoding of parameters to XML data format with identity-ref + * and inter-file linking for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlIdWithPatch() throws SvcLogicException { + String pre = "identity-test:test."; + SvcLogicContext ctx = createAttList(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/identity-test:test"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_ID_PUT)); + } + + /** * Verifies decoding of parameters from JSON data format with identity-ref * and inter-file linking. * @@ -220,6 +306,44 @@ public class DataFormatSerializerTest { } /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for put operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPut() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT)); + } + + /** + * Verifies encoding of parameters to JSON data format with containers, + * grouping and augment for patch operation-type. + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToJsonYangWithPatch() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "json"); + p.put("httpMethod", "patch"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_JSON_YANG_PUT)); + } + + /** * Verifies encoding of parameters to JSON data format with augment as * root child. * @@ -278,6 +402,44 @@ public class DataFormatSerializerTest { } /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for put operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPut() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT)); + } + + /** + * Verifies encoding of parameters to XML data format with containers, + * grouping and augment for patch operation-type + * + * @throws SvcLogicException when test case fails + */ + @Test + public void encodeToXmlYangWithPatch() throws SvcLogicException { + String pre = "test-yang:cont1.cont2."; + SvcLogicContext ctx = createAttListYang(pre); + p.put("dirPath", "src/test/resources"); + p.put("format", "xml"); + p.put("httpMethod", "put"); + p.put("restapiUrl", "http://echo.getpostman" + + ".com/restconf/operations/test-yang:cont1/cont2/cont4"); + restconf.sendRequest(p, ctx); + assertThat(dfCaptor.getResult(), is(ENCODE_TO_XML_YANG_PUT)); + } + + /** * Verifies encoding of parameters to XML data format with augment as * root child. * @@ -369,23 +531,24 @@ public class DataFormatSerializerTest { @Test public void validateUrlParser() throws SvcLogicException { String actVal = "identity-test:test"; + String putId = "/for-put"; String url1 = "http://echo.getpostman.com/restconf/operations/" + actVal; String url2 = "http://echo.getpostman.com/restconf/data/" + actVal; String url3 = "https://echo.getpostman.com/restconf/operations/" + actVal; String url4 = "https://echo.getpostman.com/restconf/data/" + actVal + - "/for-put"; + putId; String url5 = "http://localhost:8282/restconf/operations/" + actVal; String url6 = "https://localhost:8282/restconf/operations/" + actVal; String url7 = "http://localhost:8282/restconf/data/" + actVal + - "/for-put"; + putId; String url8 = "https://localhost:8282/restconf/data/" + actVal; String url9 = "http://182.2.61.24:2250/restconf/data/" + actVal; String url10 = "https://182.2.61.24:2250/restconf/operations/" + actVal; String val1 = parseUrl(url1, POST); String val2 = parseUrl(url2, GET); - String val3 = parseUrl(url3, POST); + String val3 = parseUrl(url3, PATCH); String val4 = parseUrl(url4, PUT); String val5 = parseUrl(url5, GET); String val6 = parseUrl(url6, POST); @@ -396,10 +559,10 @@ public class DataFormatSerializerTest { assertThat(val1, is(actVal)); assertThat(val2, is(actVal)); assertThat(val3, is(actVal)); - assertThat(val4, is(actVal)); + assertThat(val4, is(actVal + putId)); assertThat(val5, is(actVal)); assertThat(val6, is(actVal)); - assertThat(val7, is(actVal)); + assertThat(val7, is(actVal + putId)); assertThat(val8, is(actVal)); assertThat(val9, is(actVal)); assertThat(val10, is(actVal)); diff --git a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java index a0a154bf..02760448 100644 --- a/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java +++ b/restconf-client/provider/src/test/java/org/onap/ccsdk/sli/plugins/yangserializers/dfserializer/DataFormatUtilsTest.java @@ -26,9 +26,9 @@ package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer; */ public final class DataFormatUtilsTest { - static final String ENCODE_TO_JSON_ID = "{\n" + - " \"identity-test:con1\": {\n" + - " \"interfaces\": {\n" + + static final String ENCODE_TO_JSON_ID_COMMON = "\n \"interfaces\"" + + ": " + + "{\n" + " \"int-list\": [\n" + " {\n" + " \"iden\": \"optical\",\n" + @@ -61,14 +61,23 @@ public final class DataFormatUtilsTest { " ]\n" + " },\n" + " \"interface\": \"identity-types:physical\"\n" + - " },\n" + + " }"; + + static final String ENCODE_TO_JSON_ID = "{\n" + + " \"identity-test:con1\": {" + ENCODE_TO_JSON_ID_COMMON + + ",\n" + " \"identity-test:l\": \"abc\"\n" + "}"; - static final String ENCODE_TO_XML_ID = "<?xml version=\"1.0\" encoding=" + - "\"UTF-8\" standalone=\"no\"?>\n" + - "<con1 xmlns=\"identity:ns:test:json:ser\">\n" + - " <interfaces>\n" + + static final String ENCODE_TO_JSON_ID_PUT = "{\n" + + " \"identity-test:test\": {\n" + + " \"con1\": {" + addSpace(ENCODE_TO_JSON_ID_COMMON, 4) + + ",\n" + + " \"l\": \"abc\"\n" + + " }\n" + + "}"; + + static final String ENCODE_TO_XML_ID_COMMON = "\n <interfaces>\n" + " <int-list>\n" + " <iden>optical</iden>\n" + " <available>\n" + @@ -97,8 +106,19 @@ public final class DataFormatUtilsTest { " </int-list>\n" + " </interfaces>\n" + " <interface xmlns:yangid=\"identity:list:ns:test:json:ser\">" + - "yangid:physical</interface>\n" + - "</con1>\n"; + "yangid:physical</interface>"; + + static final String ENCODE_TO_XML_ID = "<?xml version=\"1.0\" encoding=" + + "\"UTF-8\" standalone=\"no\"?>\n" + + "<con1 xmlns=\"identity:ns:test:json:ser\">" + + ENCODE_TO_XML_ID_COMMON + "\n</con1>\n"; + + static final String ENCODE_TO_XML_ID_PUT = "<?xml version=\"1.0\" enco" + + "ding=\"UTF-8\" standalone=\"no\"?>\n" + + "<test xmlns=\"identity:ns:test:json:ser\">\n" + + " <con1>" + addSpace(ENCODE_TO_XML_ID_COMMON, 4) + + "\n </con1>\n" + + "</test>\n"; static final String ENCODE_TO_JSON_YANG_COMMON = "\n " + "\"test-augment:cont13\": {\n" + @@ -249,6 +269,13 @@ public final class DataFormatUtilsTest { " }\n" + "}"; + static final String ENCODE_TO_JSON_YANG_PUT = "{\n" + + " \"test-yang:cont4\": {" + addSpace( + ENCODE_TO_JSON_YANG_COMMON, 4) + ",\n" + + " \"leaf10\": \"abc\"\n" + + " }\n" + + "}"; + static final String ENCODE_TO_XML_YANG_COMMON = "\n" + "<cont13 xmlns=\"urn:opendaylight:params:xml:ns:yang:" + "augment\">\n" + @@ -290,6 +317,12 @@ public final class DataFormatUtilsTest { "</leaf10>" + ENCODE_TO_XML_YANG_COMMON + "\n"; + static final String ENCODE_TO_XML_YANG_PUT = "<?xml version=\"1.0\" enc" + + "oding=\"UTF-8\" standalone=\"no\"?>\n" + + "<cont4 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" + + " <leaf10>abc</leaf10>" + + addSpace(ENCODE_TO_XML_YANG_COMMON, 4) + "\n</cont4>\n"; + static final String ENCODE_TO_XML_YANG = "<?xml version=\"1.0\" encoding" + "=\"UTF-8\" standalone=\"no\"?>\n" + "<cont2 xmlns=\"urn:opendaylight:params:xml:ns:yang:test\">\n" + |