diff options
Diffstat (limited to 'src/test/java/org/onap/aai/modelloader/restclient')
3 files changed, 217 insertions, 119 deletions
diff --git a/src/test/java/org/onap/aai/modelloader/restclient/AaiRestClientTest.java b/src/test/java/org/onap/aai/modelloader/restclient/AaiRestClientTest.java deleted file mode 100644 index d82bb47..0000000 --- a/src/test/java/org/onap/aai/modelloader/restclient/AaiRestClientTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/** - * ============LICENSE_START========================================== - * org.onap.aai - * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs - * =================================================================== - * 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.aai.modelloader.restclient; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import org.onap.aai.modelloader.config.ModelLoaderConfig; -import org.onap.aai.modelloader.entity.ArtifactType; -import org.onap.aai.modelloader.entity.model.ModelArtifact; - -public class AaiRestClientTest { - - // This test requires a running A&AI system. Uncomment to test locally. - /* - * @Test public void testRestClient() throws Exception { final String - * MODEL_FILE = "src/test/resources/models/vnf-model.xml"; - * - * Properties props = new Properties(); - * props.setProperty("ml.distribution.ARTIFACT_TYPES", - * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); - * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321"); - * props.setProperty("ml.aai.MODEL_URL", - * "/aai/v8/service-design-and-creation/models/model/"); - * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12"); - * props.setProperty("ml.aai.KEYSTORE_PASSWORD", - * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o"); - * - * ModelLoaderConfig config = new ModelLoaderConfig(props, ""); - * - * String payload = readFile(MODEL_FILE); System.out.println("FILE:" + - * payload); - * - * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory = - * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = - * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); - * - * // Get the ID of the model String modelId = null; NodeList nodeList = - * doc.getDocumentElement().getChildNodes(); for (int i = 0; i < - * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if - * (currentNode.getNodeName().equals("model-name-version-id")) { modelId = - * currentNode.getTextContent(); break; } } - * - * // Add the model try { ModelArtifact model = new ModelArtifact(); - * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL); - * model.setPayload(payload); - * - * AAIRestClient aaiClient = new AAIRestClient(config); - * - * // GET model System.out.println("Calling GET API ..."); ClientResponse - * getResponse = aaiClient.getResource(getURL(model, config), - * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println( - * "GET result: " + getResponse.getStatus()); - * assertTrue(getResponse.getStatus() == - * Response.Status.NOT_FOUND.getStatusCode()); - * - * // Add the model System.out.println("Calling PUT API ..."); ClientResponse - * res = aaiClient.putResource(getURL(model, config), model.getPayload(), - * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println( - * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() == - * Response.Status.CREATED.getStatusCode()); - * - * // Delete the model System.out.println("Calling DELETE API ..."); res = - * aaiClient.getAndDeleteResource(getURL(model, config), - * "example-trans-id-3"); System.out.println("DELETE result: " + - * res.getStatus()); assertTrue(res.getStatus() == - * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) { - * e.printStackTrace(); } } - */ - - static String readFile(String path) throws IOException { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded); - } - - private String getURL(ModelArtifact model, ModelLoaderConfig config) { - String baseURL = config.getAaiBaseUrl().trim(); - String subURL = null; - if (model.getType().equals(ArtifactType.MODEL)) { - subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim(); - } else { - subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim(); - } - - if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) { - baseURL = baseURL + "/"; - } - - if (baseURL.endsWith("/") && subURL.startsWith("/")) { - baseURL = baseURL.substring(0, baseURL.length() - 1); - } - - if (!subURL.endsWith("/")) { - subURL = subURL + "/"; - } - - String url = baseURL + subURL + model.getUniqueIdentifier(); - return url; - } -} diff --git a/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java b/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java new file mode 100644 index 0000000..d3dab2e --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/restclient/TestAaiRestClient.java @@ -0,0 +1,149 @@ +/** + * ============LICENSE_START========================================== + * org.onap.aai + * =================================================================== + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * =================================================================== + * 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.aai.modelloader.restclient; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Properties; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.junit.Ignore; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.entity.model.ModelArtifact; +import org.onap.aai.modelloader.entity.model.ModelArtifactParser; +import org.onap.aai.modelloader.restclient.AaiRestClient; +import org.onap.aai.modelloader.entity.ArtifactType; +import org.onap.aai.restclient.client.OperationResult; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class TestAaiRestClient { + + // This test requires a running A&AI system. To test locally, annotate with org.junit.Test + @Ignore + public void testRestClient() throws Exception { + final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml"; + + Properties props = new Properties(); + props.setProperty("ml.distribution.ARTIFACT_TYPES", "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); + props.setProperty("ml.aai.BASE_URL", "https://localhost:8443"); + props.setProperty("ml.aai.MODEL_URL", "/aai/v9/service-design-and-creation/models/model/"); + props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12"); + props.setProperty("ml.aai.KEYSTORE_PASSWORD", "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o"); + + ModelLoaderConfig config = new ModelLoaderConfig(props, "."); + + File xmlFile = new File(MODEL_FILE); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(xmlFile); + + NodeList nodesList = doc.getDocumentElement().getChildNodes(); + + // Get the model IDs + + // @formatter:off + String modelInvariantId = + getNodesStream(nodesList) + .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_INVARIANT_ID)) + .findFirst() + .map(Node::getTextContent) + .orElse(null); + + String modelId = getNodesStream(nodesList) + .flatMap(n -> getNodesStream(n.getChildNodes())) + .filter(childNode -> childNode.getNodeName().equals(ModelArtifactParser.MODEL_VER)) + .findFirst() + .map(n -> n.getChildNodes().item(1).getTextContent()) + .orElse(null); + // @formatter:on + + try { + // Build the model artifact + ModelArtifact model = new ModelArtifact(); + model.setModelInvariantId(modelInvariantId); + model.setModelVerId(modelId); + model.setPayload(readFile(MODEL_FILE)); + model.setModelNamespace("http://org.openecomp.aai.inventory/v9"); + + AaiRestClient aaiClient = new AaiRestClient(config); + + // GET model + OperationResult opResult = + aaiClient.getResource(getURL(model, config), "example-trans-id-0", MediaType.APPLICATION_XML_TYPE); + assertTrue(opResult.getResultCode() == Response.Status.NOT_FOUND.getStatusCode()); + + // PUT the model + opResult = aaiClient.putResource(getURL(model, config), model.getPayload(), "example-trans-id-1", + MediaType.APPLICATION_XML_TYPE); + assertTrue(opResult.getResultCode() == Response.Status.CREATED.getStatusCode()); + + // DELETE the model + opResult = aaiClient.getAndDeleteResource(getURL(model, config), "example-trans-id-3"); + assertTrue(opResult.getResultCode() == Response.Status.NO_CONTENT.getStatusCode()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private Stream<Node> getNodesStream(NodeList nodeList) { + return IntStream.range(0, nodeList.getLength()).mapToObj(nodeList::item); + } + + static String readFile(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded); + } + + private String getURL(ModelArtifact model, ModelLoaderConfig config) { + String baseURL = config.getAaiBaseUrl().trim(); + String subURL = null; + if (model.getType().equals(ArtifactType.MODEL)) { + subURL = config.getAaiModelUrl(model.getModelNamespaceVersion()).trim(); + } else { + subURL = config.getAaiNamedQueryUrl(model.getModelNamespaceVersion()).trim(); + } + + if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) { + baseURL = baseURL + "/"; + } + + if (baseURL.endsWith("/") && subURL.startsWith("/")) { + baseURL = baseURL.substring(0, baseURL.length() - 1); + } + + if (!subURL.endsWith("/")) { + subURL = subURL + "/"; + } + + return baseURL + subURL + model.getModelInvariantId(); + } +} diff --git a/src/test/java/org/onap/aai/modelloader/restclient/TestBabelServiceClient.java b/src/test/java/org/onap/aai/modelloader/restclient/TestBabelServiceClient.java new file mode 100644 index 0000000..974c034 --- /dev/null +++ b/src/test/java/org/onap/aai/modelloader/restclient/TestBabelServiceClient.java @@ -0,0 +1,68 @@ +/** + * ============LICENSE_START========================================== + * org.onap.aai + * =================================================================== + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * =================================================================== + * 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.aai.modelloader.restclient; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Properties; +import org.junit.Ignore; +import org.onap.aai.babel.service.data.BabelArtifact; +import org.onap.aai.modelloader.config.ModelLoaderConfig; +import org.onap.aai.modelloader.restclient.BabelServiceClient; + +/** + * Local testing of the Babel service + * + */ +public class TestBabelServiceClient { + + // Load properties from src/test/resources + protected static String CONFIG_FILE = "model-loader.properties"; + + // This test requires a running Babel system. To test locally, annotate with org.junit.Test + @Ignore + public void testRestClient() throws Exception { // NOSONAR + Properties configProperties = new Properties(); + try { + configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); + } catch (IOException e) { + fail(); + } + BabelServiceClient client = new BabelServiceClient(new ModelLoaderConfig(configProperties, ".")); + List<BabelArtifact> result = + client.postArtifact(readBytesFromFile("compressedArtifacts/service-VscpaasTest-csar.csar"), + "service-Vscpass-Test", "1.0", "Test-Transaction-ID-BabelClient"); + + assertThat(result.size(), is(equalTo(3))); + } + + private byte[] readBytesFromFile(String resourceFile) throws IOException, URISyntaxException { + return Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(resourceFile).toURI())); + } +} |