From 16700753dcf0e3600f600b0b769c89eb2273f1d6 Mon Sep 17 00:00:00 2001 From: "Kajur, Harish (vk250x)" Date: Mon, 13 Aug 2018 05:32:35 -0400 Subject: Initial seed code for graphadmin Issue-ID: AAI-1469 Change-Id: Ic170c326ad1fe4b43960de674797766f6f7b94bf Signed-off-by: Kajur, Harish (vk250x) --- .../java/org/onap/aai/AAIGremlinQueryTest.java | 210 +++++++++ src/test/java/org/onap/aai/AAISetup.java | 137 ++++++ .../org/onap/aai/GraphAdminTestConfiguration.java | 123 ++++++ src/test/java/org/onap/aai/PayloadUtil.java | 111 +++++ src/test/java/org/onap/aai/dbgen/DupeToolTest.java | 144 ++++++ .../org/onap/aai/dbgen/ForceDeleteToolTest.java | 206 +++++++++ .../onap/aai/migration/EdgeSwingMigratorTest.java | 240 ++++++++++ .../migration/MigrationControllerInternalTest.java | 280 ++++++++++++ .../onap/aai/migration/PropertyMigratorTest.java | 130 ++++++ .../org/onap/aai/migration/ValueMigratorTest.java | 150 +++++++ .../org/onap/aai/migration/VertexMergeTest.java | 177 ++++++++ ...ntainmentDeleteOtherVPropertyMigrationTest.java | 112 +++++ .../aai/migration/v12/DeletePInterfaceTest.java | 145 ++++++ ...rateModelVerDistributionStatusPropertyTest.java | 106 +++++ .../MigrateServiceInstanceToConfigurationTest.java | 404 +++++++++++++++++ ...nstanceToConfigurationTestPreMigrationMock.java | 299 +++++++++++++ .../v12/SDWANSpeedChangeMigrationTest.java | 375 ++++++++++++++++ .../onap/aai/migration/v12/UriMigrationTest.java | 169 +++++++ .../v13/MigrateBooleanDefaultsToFalseTest.java | 384 ++++++++++++++++ .../v13/MigrateInMaintDefaultToFalseTest.java | 411 +++++++++++++++++ .../MigrateInstanceGroupModelInvariantIdTest.java | 108 +++++ .../MigrateInstanceGroupModelVersionIdTest.java | 111 +++++ .../v13/MigrateInstanceGroupSubTypeTest.java | 112 +++++ .../v13/MigrateInstanceGroupTypeTest.java | 112 +++++ .../aai/migration/v13/MigrateModelVerTest.java | 487 +++++++++++++++++++++ .../v13/MigratePServerAndPnfEquipTypeTest.java | 138 ++++++ .../v13/MigrateVnfcModelInvariantIdTest.java | 110 +++++ .../v13/MigrateVnfcModelVersionIdTest.java | 112 +++++ .../org/onap/aai/schema/db/ManageSchemaTest.java | 111 +++++ .../util/SendDeleteMigrationNotificationsTest.java | 166 +++++++ .../aai/util/SendMigrationNotificationsTest.java | 184 ++++++++ 31 files changed, 6064 insertions(+) create mode 100644 src/test/java/org/onap/aai/AAIGremlinQueryTest.java create mode 100644 src/test/java/org/onap/aai/AAISetup.java create mode 100644 src/test/java/org/onap/aai/GraphAdminTestConfiguration.java create mode 100644 src/test/java/org/onap/aai/PayloadUtil.java create mode 100644 src/test/java/org/onap/aai/dbgen/DupeToolTest.java create mode 100644 src/test/java/org/onap/aai/dbgen/ForceDeleteToolTest.java create mode 100644 src/test/java/org/onap/aai/migration/EdgeSwingMigratorTest.java create mode 100644 src/test/java/org/onap/aai/migration/MigrationControllerInternalTest.java create mode 100644 src/test/java/org/onap/aai/migration/PropertyMigratorTest.java create mode 100644 src/test/java/org/onap/aai/migration/ValueMigratorTest.java create mode 100644 src/test/java/org/onap/aai/migration/VertexMergeTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/ContainmentDeleteOtherVPropertyMigrationTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/DeletePInterfaceTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/MigrateModelVerDistributionStatusPropertyTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTestPreMigrationMock.java create mode 100644 src/test/java/org/onap/aai/migration/v12/SDWANSpeedChangeMigrationTest.java create mode 100644 src/test/java/org/onap/aai/migration/v12/UriMigrationTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateBooleanDefaultsToFalseTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateInMaintDefaultToFalseTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelInvariantIdTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelVersionIdTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupSubTypeTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupTypeTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateModelVerTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigratePServerAndPnfEquipTypeTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelInvariantIdTest.java create mode 100644 src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelVersionIdTest.java create mode 100644 src/test/java/org/onap/aai/schema/db/ManageSchemaTest.java create mode 100644 src/test/java/org/onap/aai/util/SendDeleteMigrationNotificationsTest.java create mode 100644 src/test/java/org/onap/aai/util/SendMigrationNotificationsTest.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/onap/aai/AAIGremlinQueryTest.java b/src/test/java/org/onap/aai/AAIGremlinQueryTest.java new file mode 100644 index 0000000..6385fee --- /dev/null +++ b/src/test/java/org/onap/aai/AAIGremlinQueryTest.java @@ -0,0 +1,210 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai; + +import com.jayway.jsonpath.JsonPath; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.hamcrest.CoreMatchers; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.junit.runner.RunWith; +import org.onap.aai.config.PropertyPasswordConfiguration; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.util.AAIConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.http.*; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit4.rules.SpringClassRule; +import org.springframework.test.context.junit4.rules.SpringMethodRule; +import org.springframework.web.client.RestTemplate; + +import javax.ws.rs.core.Response; +import java.io.UnsupportedEncodingException; +import java.util.*; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +/** + * A sample junit test using spring boot that provides the ability to spin + * up the application from the junit layer and run rest requests against + * SpringBootTest annotation with web environment requires which spring boot + * class to load and the random port starts the application on a random port + * and injects back into the application for the field with annotation LocalServerPort + *

+ * + * This can be used to potentially replace a lot of the fitnesse tests since + * they will be testing against the same thing except fitnesse uses hbase + */ +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = GraphAdminApp.class) +@ContextConfiguration(initializers = PropertyPasswordConfiguration.class) +@Import(GraphAdminTestConfiguration.class) +public class AAIGremlinQueryTest { + + @ClassRule + public static final SpringClassRule springClassRule = new SpringClassRule(); + + @Rule + public final SpringMethodRule springMethodRule = new SpringMethodRule(); + + @Autowired + RestTemplate restTemplate; + + @LocalServerPort + int randomPort; + + private HttpEntity httpEntity; + + private HttpHeaders headers; + + private String baseUrl; + + @BeforeClass + public static void setupConfig() throws AAIException { + System.setProperty("AJSC_HOME", "./"); + System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/"); + } + + public void createGraph(){ + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + g.addV() + .property("aai-node-type", "pserver") + .property("hostname", "test-pserver") + .property("in-maint", false) + .property("source-of-truth", "JUNIT") + .property("aai-uri", "/cloud-infrastructure/pservers/pserver/test-pserver") + .next(); + + } catch(Exception ex){ + success = false; + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to setup the graph"); + } + } + } + + @Before + public void setup() throws Exception { + + AAIConfig.init(); + AAIGraph.getInstance(); + + createGraph(); + headers = new HttpHeaders(); + + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.add("Real-Time", "true"); + headers.add("X-FromAppId", "JUNIT"); + headers.add("X-TransactionId", "JUNIT"); + + String authorization = Base64.getEncoder().encodeToString("AAI:AAI".getBytes("UTF-8")); + headers.add("Authorization", "Basic " + authorization); + httpEntity = new HttpEntity(headers); + baseUrl = "https://localhost:" + randomPort; + } + + @Test + public void testPserverCountUsingGremlin() throws Exception { + Map gremlinQueryMap = new HashMap<>(); + gremlinQueryMap.put("gremlin-query", "g.V().has('hostname', 'test-pserver').count()"); + + String payload = PayloadUtil.getTemplatePayload("gremlin-query.json", gremlinQueryMap); + + ResponseEntity responseEntity = null; + + String endpoint = "/aai/v11/dbquery?format=console"; + + httpEntity = new HttpEntity(payload, headers); + responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); + assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK)); + + String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result"); + assertThat(result, is("1")); + } + + @Test + public void testPserverCountUsingDsl() throws Exception { + Map dslQuerymap = new HashMap<>(); + dslQuerymap.put("dsl-query", "pserver*('hostname', 'test-pserver')"); + + String payload = PayloadUtil.getTemplatePayload("dsl-query.json", dslQuerymap); + + ResponseEntity responseEntity = null; + + String endpoint = "/aai/v11/dbquery?format=console"; + + httpEntity = new HttpEntity(payload, headers); + responseEntity = restTemplate.exchange(baseUrl + endpoint, HttpMethod.PUT, httpEntity, String.class); + assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK)); + + String result = JsonPath.read(responseEntity.getBody().toString(), "$.results[0].result"); + assertThat(result, containsString("v[")); + } + + @After + public void tearDown() { + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + g.V().has("source-of-truth", "JUNIT") + .toList() + .forEach(v -> v.remove()); + + } catch(Exception ex){ + success = false; + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to teardown the graph"); + } + } + + } +} diff --git a/src/test/java/org/onap/aai/AAISetup.java b/src/test/java/org/onap/aai/AAISetup.java new file mode 100644 index 0000000..59afe60 --- /dev/null +++ b/src/test/java/org/onap/aai/AAISetup.java @@ -0,0 +1,137 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai; + +import static org.junit.Assert.assertNotNull; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import org.apache.commons.io.IOUtils; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.onap.aai.config.*; +import org.onap.aai.db.schema.AuditorFactory; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.MoxyLoader; +import org.onap.aai.nodes.NodeIngestor; +import org.onap.aai.rest.db.HttpEntry; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.setup.AAIConfigTranslator; +import org.onap.aai.setup.SchemaLocationsBean; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.rules.SpringClassRule; +import org.springframework.test.context.junit4.rules.SpringMethodRule; + +@ContextConfiguration(classes = { + SchemaLocationsBean.class, + AAIConfigTranslator.class, + SchemaVersions.class, + NodeIngestor.class, + EdgeIngestor.class, + EdgeSerializer.class, + SpringContextAware.class, + AuditorConfiguration.class, + DslConfiguration.class, + IntrospectionConfig.class, + RestBeanConfig.class +}) +@TestPropertySource(properties = { + "schema.uri.base.path = /aai", + "schema.ingest.file = src/main/resources/application.properties" +}) +public abstract class AAISetup { + + @Autowired + protected NodeIngestor nodeIngestor; + + @Autowired + protected LoaderFactory loaderFactory; + + @Autowired + protected Map moxyLoaderInstance; + + @Autowired + protected HttpEntry traversalHttpEntry; + + @Autowired + protected HttpEntry traversalUriHttpEntry; + + @Autowired + protected EdgeSerializer edgeSerializer; + + @Autowired + protected SchemaVersions schemaVersions; + + @Autowired + protected EdgeIngestor edgeIngestor; + + @Autowired + protected AuditorFactory auditorFactory; + + @Value("${schema.uri.base.path}") + protected String basePath; + + @ClassRule + public static final SpringClassRule springClassRule = new SpringClassRule(); + + @Rule + public final SpringMethodRule springMethodRule = new SpringMethodRule(); + + protected static JanusGraph graph; + protected static JanusGraphTransaction tx; + + @BeforeClass + public static void setupBundleconfig() throws Exception { + System.setProperty("AJSC_HOME", "./"); + System.setProperty("BUNDLECONFIG_DIR", "src/main/resources/"); + System.setProperty("org.onap.aai.graphadmin.started", "true"); + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + } + + @AfterClass + public static void cleanUpGraph() { + tx.tx().rollback(); + graph.close(); + } + + public String getPayload(String filename) throws IOException { + + InputStream inputStream = getClass() + .getClassLoader() + .getResourceAsStream(filename); + + String message = String.format("Unable to find the %s in src/test/resources", filename); + assertNotNull(message, inputStream); + + String resource = IOUtils.toString(inputStream); + return resource; + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/GraphAdminTestConfiguration.java b/src/test/java/org/onap/aai/GraphAdminTestConfiguration.java new file mode 100644 index 0000000..a69e703 --- /dev/null +++ b/src/test/java/org/onap/aai/GraphAdminTestConfiguration.java @@ -0,0 +1,123 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.apache.http.client.HttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.ssl.SSLContextBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; +import org.springframework.http.HttpStatus; +import org.springframework.http.client.ClientHttpResponse; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.util.ResourceUtils; +import org.springframework.web.client.ResponseErrorHandler; +import org.springframework.web.client.RestTemplate; + +import javax.net.ssl.SSLContext; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.KeyStore; + +@TestConfiguration +public class GraphAdminTestConfiguration { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(GraphAdminTestConfiguration.class); + + @Autowired + private Environment env; + + /** + * Create a RestTemplate bean, using the RestTemplateBuilder provided + * by the auto-configuration. + */ + @Bean + RestTemplate restTemplate(RestTemplateBuilder builder) throws Exception { + + char[] trustStorePassword = env.getProperty("server.ssl.trust-store-password").toCharArray(); + char[] keyStorePassword = env.getProperty("server.ssl.key-store-password").toCharArray(); + + String keyStore = env.getProperty("server.ssl.key-store"); + String trustStore = env.getProperty("server.ssl.trust-store"); + + SSLContextBuilder sslContextBuilder = SSLContextBuilder.create(); + + if(env.acceptsProfiles("two-way-ssl")){ + sslContextBuilder = sslContextBuilder.loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword); + } + + SSLContext sslContext = sslContextBuilder + .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword) + .build(); + + HttpClient client = HttpClients.custom() + .setSSLContext(sslContext) + .setSSLHostnameVerifier((s, sslSession) -> true) + .build(); + + RestTemplate restTemplate = builder + .requestFactory(new HttpComponentsClientHttpRequestFactory(client)) + .build(); + + restTemplate.setErrorHandler(new ResponseErrorHandler() { + @Override + public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException { + if (clientHttpResponse.getStatusCode() != HttpStatus.OK) { + + logger.debug("Status code: " + clientHttpResponse.getStatusCode()); + + if (clientHttpResponse.getStatusCode() == HttpStatus.FORBIDDEN) { + logger.debug("Call returned a error 403 forbidden resposne "); + return true; + } + + if(clientHttpResponse.getRawStatusCode() % 100 == 5){ + logger.debug("Call returned a error " + clientHttpResponse.getStatusText()); + return true; + } + } + + return false; + } + + @Override + public void handleError(ClientHttpResponse clientHttpResponse) throws IOException { + } + }); + + return restTemplate; + } + + private KeyStore loadPfx(String file, char[] password) throws Exception { + KeyStore keyStore = KeyStore.getInstance("PKCS12"); + File key = ResourceUtils.getFile(file); + try (InputStream in = new FileInputStream(key)) { + keyStore.load(in, password); + } + return keyStore; + } +} diff --git a/src/test/java/org/onap/aai/PayloadUtil.java b/src/test/java/org/onap/aai/PayloadUtil.java new file mode 100644 index 0000000..8adc9d1 --- /dev/null +++ b/src/test/java/org/onap/aai/PayloadUtil.java @@ -0,0 +1,111 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai; + +import org.apache.commons.io.IOUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.Assert.assertNotNull; + +public class PayloadUtil { + + private static final Map cache = new HashMap<>(); + private static final Pattern TEMPLATE_PATTERN = Pattern.compile("\\$\\{[^}]+\\}"); + + public static String getExpectedPayload(String fileName) throws IOException { + + InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/expected/" + fileName); + + String message = String.format("Unable to find the %s in src/test/resources", fileName); + assertNotNull(message, inputStream); + + String resource = IOUtils.toString(inputStream); + + inputStream.close(); + return resource; + } + + public static String getResourcePayload(String fileName) throws IOException { + + InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/resource/" + fileName); + + String message = String.format("Unable to find the %s in src/test/resources", fileName); + assertNotNull(message, inputStream); + + String resource = IOUtils.toString(inputStream); + + inputStream.close(); + return resource; + } + + public static String getTemplatePayload(String fileName, Map templateValueMap) throws Exception { + + InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/templates/" + fileName); + + String message = String.format("Unable to find the %s in src/test/resources", fileName); + assertNotNull(message, inputStream); + + String resource; + + if(cache.containsKey(fileName)){ + resource = cache.get(fileName); + } else { + resource = IOUtils.toString(inputStream); + cache.put(fileName, resource); + } + + Matcher matcher = TEMPLATE_PATTERN.matcher(resource); + + String resourceWithTemplateValues = resource; + + while(matcher.find()){ + int start = matcher.start() + 2; + int end = matcher.end() - 1; + String key = resource.substring(start, end); + if(templateValueMap.containsKey(key)){ + resourceWithTemplateValues = resourceWithTemplateValues.replaceAll("\\$\\{" + key +"\\}", templateValueMap.get(key)); + } else { + throw new RuntimeException("Unable to find the key value pair in map for the template processing for key " + key); + } + } + + inputStream.close(); + return resourceWithTemplateValues; + } + + public static String getNamedQueryPayload(String fileName) throws IOException { + + InputStream inputStream = PayloadUtil.class.getClassLoader().getResourceAsStream("payloads/named-queries/" + fileName); + + String message = String.format("Unable to find the %s in src/test/resources/payloads/named-queries", fileName); + assertNotNull(message, inputStream); + + String resource = IOUtils.toString(inputStream); + + inputStream.close(); + return resource; + } +} diff --git a/src/test/java/org/onap/aai/dbgen/DupeToolTest.java b/src/test/java/org/onap/aai/dbgen/DupeToolTest.java new file mode 100644 index 0000000..392ce97 --- /dev/null +++ b/src/test/java/org/onap/aai/dbgen/DupeToolTest.java @@ -0,0 +1,144 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.dbgen; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.AAIGraph; + +import static org.junit.Assert.*; + +public class DupeToolTest extends AAISetup { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(DupeToolTest.class); + + private DupeTool dupeTool; + + @Before + public void setup(){ + dupeTool = new DupeTool(loaderFactory, schemaVersions, false); + createGraph(); + } + + private void createGraph() { + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + Vertex cloudRegionVertex = g.addV() + .property("aai-node-type", "cloud-region") + .property("cloud-owner", "test-owner") + .property("cloud-region-id", "test-region") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex tenantVertex = g.addV() + .property("aai-node-type", "tenant") + .property("tenant-id", "test-tenant") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex pserverVertex = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", "test-pserver") + .property("in-maint", false) + .property("source-of-truth", "JUNIT") + .next(); + + for(int i = 0; i < 100; ++i){ + g.addV() + .property("aai-node-type", "p-interface") + .property("interface-name", "p-interface-name") + .property("in-maint", false) + .property("source-of-truth", "JUNIT") + .next(); + } + + edgeSerializer.addTreeEdge(g, cloudRegionVertex, tenantVertex); + edgeSerializer.addEdge(g, cloudRegionVertex, pserverVertex); + + } catch(Exception ex){ + success = false; + logger.error("Unable to create the vertexes", ex); + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to setup the graph"); + } + } + } + + @Test + public void testDupeTool(){ + //TODO: test does not find duplicates + String[] args = { + "-userId", "testuser", + "-nodeType", "p-interface", + "-timeWindowMinutes", "30", + "-autoFix", + "-maxFix", "30", + "-sleepMinutes", "0" + }; + + dupeTool.execute(args); + } + + @After + public void tearDown(){ + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + g.V().has("source-of-truth", "JUNIT") + .toList() + .forEach(v -> v.remove()); + + } catch(Exception ex){ + success = false; + logger.error("Unable to remove the vertexes", ex); + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to teardown the graph"); + } + } + + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/dbgen/ForceDeleteToolTest.java b/src/test/java/org/onap/aai/dbgen/ForceDeleteToolTest.java new file mode 100644 index 0000000..fb6301c --- /dev/null +++ b/src/test/java/org/onap/aai/dbgen/ForceDeleteToolTest.java @@ -0,0 +1,206 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.dbgen; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runners.MethodSorters; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.AAIGraph; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.List; + +import static org.junit.Assert.fail; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class ForceDeleteToolTest extends AAISetup { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(ForceDeleteToolTest.class); + + private ForceDeleteTool deleteTool; + + private Vertex cloudRegionVertex; + + @Before + public void setup(){ + deleteTool = new ForceDeleteTool(); + deleteTool.SHOULD_EXIT_VM = false; + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + cloudRegionVertex = g.addV() + .property("aai-node-type", "cloud-region") + .property("cloud-owner", "test-owner") + .property("cloud-region-id", "test-region") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex tenantVertex = g.addV() + .property("aai-node-type", "tenant") + .property("tenant-id", "test-tenant") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex pserverVertex = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", "test-pserver") + .property("in-maint", false) + .property("source-of-truth", "JUNIT") + .next(); + + edgeSerializer.addTreeEdge(g, cloudRegionVertex, tenantVertex); + edgeSerializer.addEdge(g, cloudRegionVertex, pserverVertex); + + } catch(Exception ex){ + success = false; + logger.error("Unable to create the vertexes", ex); + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to setup the graph"); + } + } + + + } + + @Test + public void testCollectDataForVertex(){ + + String [] args = { + + "-action", + "COLLECT_DATA", + "-userId", + "someuser", + "-params4Collect", + "cloud-owner|test-owner" + }; + + deleteTool.main(args); + } + + @Test + public void testDeleteNode(){ + + String id = cloudRegionVertex.id().toString(); + + String [] args = { + + "-action", + "DELETE_NODE", + "-userId", + "someuser", + "-vertexId", + id + }; + + deleteTool.main(args); + } + + @Test + public void testCollectDataForEdge(){ + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + GraphTraversalSource g = transaction.traversal(); + List edges = g.E().toList(); + String cloudRegionToPserverId = edges.get(0).id().toString(); + + String [] args = { + + "-action", + "COLLECT_DATA", + "-userId", + "someuser", + "-edgeId", + cloudRegionToPserverId + }; + + deleteTool.main(args); + } + + @Test + public void testDeleteForEdge(){ + + InputStream systemInputStream = System.in; + ByteArrayInputStream in = new ByteArrayInputStream("y".getBytes()); + System.setIn(in); + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + GraphTraversalSource g = transaction.traversal(); + List edges = g.E().toList(); + String cloudRegionToPserverId = edges.get(0).id().toString(); + + String [] args = { + + "-action", + "DELETE_EDGE", + "-userId", + "someuser", + "-edgeId", + cloudRegionToPserverId + }; + + deleteTool.main(args); + System.setIn(systemInputStream); + } + @After + public void tearDown(){ + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + boolean success = true; + + try { + + GraphTraversalSource g = transaction.traversal(); + + g.V().has("source-of-truth", "JUNIT") + .toList() + .forEach(v -> v.remove()); + + } catch(Exception ex){ + success = false; + logger.error("Unable to remove the vertexes", ex); + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + fail("Unable to teardown the graph"); + } + } + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/EdgeSwingMigratorTest.java b/src/test/java/org/onap/aai/migration/EdgeSwingMigratorTest.java new file mode 100644 index 0000000..d472b4d --- /dev/null +++ b/src/test/java/org/onap/aai/migration/EdgeSwingMigratorTest.java @@ -0,0 +1,240 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Optional; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Direction; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Property; +import org.javatuples.Pair; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.schema.JanusGraphManagement; + +public class EdgeSwingMigratorTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private GraphTraversalSource g; + private MockEdgeSwingMigrator migration; + private Vertex modelVer1 = null; + private Vertex modelVer3 = null; + + + @Before + public void setUp() throws Exception { + JanusGraphManagement janusgraphManagement = graph.openManagement(); + g = graph.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + createFirstVertexAndRelatedVertexes(); + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + Mockito.doReturn(janusgraphManagement).when(adminSpy).getManagementSystem(); + + + migration = new MockEdgeSwingMigrator(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + private void createFirstVertexAndRelatedVertexes() throws AAIException { + Vertex model1 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-1") + .property("model-type", "widget") + .next(); + modelVer1 = g.addV().property("aai-node-type", "model-ver") + .property("model-version-id", "model-version-id-1") + .property("model-name", "connector") + .property("model-version", "v1.0") + .next(); + edgeSerializer.addTreeEdge(g, model1, modelVer1); + + //Create the cousin vertex - modelElement2 which will point to modelVer1 + Vertex model2 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-2") + .property("model-type", "resource") + .next(); + Vertex modelVer2 = g.addV().property("aai-node-type", "model-ver") + .property("model-version-id", "model-version-id-2") + .property("model-name", "resourceModTestVer") + .property("model-version", "v1.0") + .next(); + edgeSerializer.addTreeEdge(g, model2, modelVer2); + Vertex modelElement2 = g.addV().property("aai-node-type", "model-element") + .property("model-element-uuid", "model-element-uuid-2") + .property("new-data-del-flag", "T") + .property("cardinality", "unbounded") + .next(); + edgeSerializer.addTreeEdge(g, modelVer2, modelElement2); + edgeSerializer.addEdge(g, modelVer1, modelElement2); + + Vertex model3 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-3") + .property("model-type", "widget") + .next(); + modelVer3 = g.addV().property("aai-node-type", "model-ver") + .property("model-version-id", "model-version-id-3") + .property("model-name", "connector") + .property("model-version", "v1.0") + .next(); + edgeSerializer.addTreeEdge(g, model3, modelVer3); + } + + class MockEdgeSwingMigrator extends EdgeSwingMigrator { + + public MockEdgeSwingMigrator(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions) { + super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + } + + @Override + public List> getAffectedNodePairs() { + List> fromToVertPairList = new ArrayList>(); + Vertex fromVert = modelVer1; + Vertex toVert = modelVer3; + fromToVertPairList.add(new Pair<>(fromVert, toVert)); + return fromToVertPairList; + } + + public String getNodeTypeRestriction(){ + return "model-element"; + } + + public String getEdgeLabelRestriction(){ + return "org.onap.relationships.inventory.IsA"; + } + + public String getEdgeDirRestriction(){ + return "IN"; + } + + @Override + public void cleanupAsAppropriate(List> nodePairL) { + // For the scenario we're testing, we would define this to remove the model-ver that + // we moved off of, and also remove its parent model since it was a widget model and + // these are currently one-to-one (model-ver to model). + // + // But what gets cleaned up (if anything) after a node's edges are migrated will vary depending + // on what the edgeSwingMigration is being used for. + + + } + + @Override + public Optional getAffectedNodeTypes() { + return Optional.of(new String[]{"model", "model-element", "model-ver"}); + } + + @Override + public String getMigrationName() { + return "MockEdgeSwingMigrator"; + } + } + + @Test + public void testBelongsToEdgesStillThere() { + assertEquals(true, g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-1") + .out("org.onap.relationships.inventory.BelongsTo").has("model-invariant-id", "model-invariant-id-1").hasNext()); + assertEquals(true, g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-3") + .out("org.onap.relationships.inventory.BelongsTo").has("model-invariant-id", "model-invariant-id-3").hasNext()); + assertEquals(true, g.V().has("aai-node-type", "model-element").has("model-element-uuid", "model-element-uuid-2") + .out("org.onap.relationships.inventory.BelongsTo").has("model-version-id", "model-version-id-2").hasNext()); + } + + @Test + public void testThatNewEdgeAdded() { + assertEquals(true, g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-3") + .in("org.onap.relationships.inventory.IsA").has("model-element-uuid", "model-element-uuid-2").hasNext()); + } + + @Test + public void testThatNewEdgeHasAaiUuidAndDelProperties() { + boolean haveUuidProp = false; + boolean haveDelOtherVProp = false; + GraphTraversal modVerTrav = g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-3"); + while (modVerTrav.hasNext()) { + Vertex modVerVtx = modVerTrav.next(); + Iterator edgeIter = modVerVtx.edges(Direction.IN, "org.onap.relationships.inventory.IsA"); + while( edgeIter.hasNext() ){ + Edge oldOutE = edgeIter.next(); + + Iterator > propsIter2 = oldOutE.properties(); + HashMap propMap2 = new HashMap(); + while( propsIter2.hasNext() ){ + Property ep2 = propsIter2.next(); + if( ep2.key().equals("aai-uuid") ){ + haveUuidProp = true; + } + else if( ep2.key().equals("delete-other-v") ){ + haveDelOtherVProp = true; + } + } + } + } + + assertTrue("New IsA edge has aai-uuid property ", haveUuidProp ); + assertTrue("New IsA edge has delete-other-v property ", haveDelOtherVProp ); + } + + + @Test + public void testThatOldEdgeGone() { + assertEquals(false, g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-1") + .in("org.onap.relationships.inventory.IsA").has("model-element-uuid", "model-element-uuid-2").hasNext()); + } + + +} diff --git a/src/test/java/org/onap/aai/migration/MigrationControllerInternalTest.java b/src/test/java/org/onap/aai/migration/MigrationControllerInternalTest.java new file mode 100644 index 0000000..6cc108b --- /dev/null +++ b/src/test/java/org/onap/aai/migration/MigrationControllerInternalTest.java @@ -0,0 +1,280 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.exceptions.AAIException; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.StringContains.containsString; + +public class MigrationControllerInternalTest extends AAISetup { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(MigrationControllerInternalTest.class); + + private MigrationControllerInternal migrationControllerInternal; + + @Before + public void setup() throws AAIException { + migrationControllerInternal = new MigrationControllerInternal(loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + clearGraph(); + createGraph(); + } + + private void createGraph(){ + + JanusGraphTransaction transaction = AAIGraph.getInstance().getGraph().newTransaction(); + boolean success = true; + + try { + GraphTraversalSource g = transaction.traversal(); + + Vertex servSub1 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servinst1 = g.addV().property( "aai-node-type", "service-instance") + .property("service-type", "DHV") + .property("source-of-truth", "JUNIT") + .next(); + Vertex allotedRsrc1 = g.addV().property( "aai-node-type", "allotted-resource") + .property("id","rsrc1") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servinst2 = g.addV().property( "aai-node-type", "service-instance") + .property("service-type", "VVIG") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servSub2 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex genericvnf1 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId1") + .property("source-of-truth", "JUNIT") + .next(); + Vertex vServer1 = g.addV().property("aai-node-type", "vserver") + .property("source-of-truth", "JUNIT") + .next(); + Vertex pServer1 = g.addV().property("aai-node-type", "pserver") + .property("source-of-truth", "JUNIT") + .next(); + Vertex pInterfaceWan1 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name","ge-0/0/10") + .property("source-of-truth", "JUNIT") + .next(); + Vertex tunnelXConnectAll_Wan1 = g.addV().property( "aai-node-type", "tunnel-xconnect") + .property("id", "tunnelXConnectWan1") + .property("bandwidth-up-wan1", "300") + .property("bandwidth-down-wan1", "400") + .property("bandwidth-up-wan2", "500") + .property("bandwidth-down-wan2", "600") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex pLinkWan1 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan1") + .property("service-provider-bandwidth-up-units", "empty") + .property("service-provider-bandwidth-down-units", "empty") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servSub3 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servinst3 = g.addV().property( "aai-node-type", "service-instance") + .property("service-type", "DHV") + .property("source-of-truth", "JUNIT") + .next(); + Vertex allotedRsrc3 = g.addV().property( "aai-node-type", "allotted-resource") + .property("id","rsrc3") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servinst4 = g.addV().property( "aai-node-type", "service-instance") + .property("service-type", "VVIG") + .property("source-of-truth", "JUNIT") + .next(); + Vertex servSub4 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex genericvnf3 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId3") + .property("source-of-truth", "JUNIT") + .next(); + Vertex vServer3 = g.addV().property("aai-node-type", "vserver") + .property("source-of-truth", "JUNIT") + .next(); + Vertex pServer3 = g.addV().property("aai-node-type", "pserver") + .property("source-of-truth", "JUNIT") + .next(); + Vertex pInterfaceWan3 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name","ge-0/0/11") + .property("source-of-truth", "JUNIT") + .next(); + Vertex tunnelXConnectAll_Wan3 = g.addV().property( "aai-node-type", "tunnel-xconnect") + .property("id", "tunnelXConnectWan3") + .property("bandwidth-up-wan1", "300") + .property("bandwidth-down-wan1", "400") + .property("bandwidth-up-wan2", "500") + .property("bandwidth-down-wan2", "600") + .property("source-of-truth", "JUNIT") + .next(); + + Vertex pLinkWan3 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan3") + .property("service-provider-bandwidth-up-units", "empty") + .property("service-provider-bandwidth-down-units", "empty") + .property("source-of-truth", "JUNIT") + .next(); + + edgeSerializer.addTreeEdge(g,servSub1,servinst1); + edgeSerializer.addEdge(g,servinst1,allotedRsrc1); + edgeSerializer.addTreeEdge(g,servinst2,servSub2); + edgeSerializer.addTreeEdge(g,allotedRsrc1,servinst2); + + edgeSerializer.addTreeEdge(g,allotedRsrc1,tunnelXConnectAll_Wan1); + + + edgeSerializer.addEdge(g,servinst1,genericvnf1); + edgeSerializer.addEdge(g,genericvnf1,vServer1); + edgeSerializer.addEdge(g,vServer1,pServer1); + edgeSerializer.addTreeEdge(g,pServer1,pInterfaceWan1); + edgeSerializer.addEdge(g,pInterfaceWan1,pLinkWan1); + + edgeSerializer.addTreeEdge(g,servSub3,servinst3); + edgeSerializer.addEdge(g,servinst3,allotedRsrc3); + edgeSerializer.addTreeEdge(g,servinst4,servSub4); + edgeSerializer.addTreeEdge(g,allotedRsrc3,servinst4); + + edgeSerializer.addTreeEdge(g,allotedRsrc3,tunnelXConnectAll_Wan3); + + + edgeSerializer.addEdge(g,servinst3,genericvnf3); + edgeSerializer.addEdge(g,genericvnf3,vServer3); + edgeSerializer.addEdge(g,vServer3,pServer3); + edgeSerializer.addTreeEdge(g,pServer3,pInterfaceWan3); + edgeSerializer.addEdge(g,pInterfaceWan3,pLinkWan3); + + } catch(Exception ex){ + success = false; + logger.error("Unable to create the graph {}", ex); + } finally { + if(success){ + transaction.commit(); + } else { + transaction.rollback(); + } + + } + } + + @Ignore + @Test + public void testListAllOfMigrations() throws Exception { + PrintStream oldOutputStream = System.out; + final ByteArrayOutputStream myOut = new ByteArrayOutputStream(); + System.setOut(new PrintStream(myOut)); + + String [] args = { + "-c", "./bundleconfig-local/etc/appprops/janusgraph-realtime.properties", + "-l" + }; + + migrationControllerInternal.run(args); + + String content = myOut.toString(); + assertThat(content, containsString("List of all migrations")); + System.setOut(oldOutputStream); + } + + @Test + public void testRunSpecificMigration() throws Exception { + String [] args = "-c ./bundleconfig-local/etc/appprops/janusgraph-realtime.properties -m SDWANSpeedChangeMigration".split(" "); + migrationControllerInternal.run(args); + } + + @Test + public void testRunSpecificMigrationAndCommit() throws Exception { + String [] args = { + "-c", "./bundleconfig-local/etc/appprops/janusgraph-realtime.properties", + "-m", "SDWANSpeedChangeMigration", + "--commit" + }; + migrationControllerInternal.run(args); + } + + @Test + public void testRunSpecificMigrationFromLoadingSnapshotAndCommit() throws Exception{ + clearGraph(); + String [] args = { + "-d", "./snapshots/sdwan_test_migration.graphson", + "-c", "./bundleconfig-local/etc/appprops/janusgraph-realtime.properties", + "-m", "SDWANSpeedChangeMigration" + }; + migrationControllerInternal.run(args); + } + + @After + public void tearDown(){ + clearGraph(); + } + + public void clearGraph(){ + + JanusGraphTransaction janusgraphTransaction = AAIGraph.getInstance().getGraph().newTransaction(); + + boolean success = true; + + try { + GraphTraversalSource g = janusgraphTransaction.traversal(); + + g.V().has("source-of-truth", "JUNIT") + .toList() + .forEach((v) -> v.remove()); + + } catch(Exception ex) { + success = false; + logger.error("Unable to remove all of the vertexes", ex); + } finally { + if(success){ + janusgraphTransaction.commit(); + } else { + janusgraphTransaction.rollback(); + } + } + + } +} diff --git a/src/test/java/org/onap/aai/migration/PropertyMigratorTest.java b/src/test/java/org/onap/aai/migration/PropertyMigratorTest.java new file mode 100644 index 0000000..654782c --- /dev/null +++ b/src/test/java/org/onap/aai/migration/PropertyMigratorTest.java @@ -0,0 +1,130 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import org.janusgraph.core.Cardinality; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import java.util.List; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class PropertyMigratorTest extends AAISetup { + + private static final EELFLogger logger = EELFManager.getInstance().getLogger(PropertyMigratorTest.class); + + public static class PserverPropMigrator extends PropertyMigrator { + + public PserverPropMigrator(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions, String oldName, String newName, Class type, Cardinality cardinality) { + super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + this.initialize(oldName, newName, type, cardinality); + } + + @Override + public boolean isIndexed() { + return true; + } + + @Override + public Optional getAffectedNodeTypes() { + return Optional.of(new String[]{ "pserver" }); + } + + @Override + public String getMigrationName() { + return "PserverPropMigrator"; + } + } + + @Before + public void setup(){ + AAIGraph.getInstance(); + JanusGraphTransaction janusgraphTransaction = AAIGraph.getInstance().getGraph().newTransaction(); + boolean success = true; + + try { + GraphTraversalSource g = janusgraphTransaction.traversal(); + g.addV() + .property("aai-node-type", "pserver") + .property("hostname", "fake-hostname") + .property("inv-status", "some status") + .property("source-of-truth", "JUNIT") + .next(); + } catch(Exception ex){ + success = false; + logger.error("Unable to commit the transaction {}", ex); + + } finally { + if(success){ + janusgraphTransaction.commit(); + } else { + janusgraphTransaction.rollback(); + } + + } + } + + @Test + public void testAfterPropertyMigration(){ + + String oldPropName = "inv-status"; + String newPropName = "inventory-status"; + + Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion()); + JanusGraphDBEngine dbEngine = new JanusGraphDBEngine(QueryStyle.TRAVERSAL, DBConnectionType.REALTIME, loader); + dbEngine.startTransaction(); + + PropertyMigrator propertyMigrator = new PserverPropMigrator(dbEngine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions, oldPropName, newPropName, String.class, Cardinality.SINGLE); + propertyMigrator.run(); + assertEquals("Expecting the property to be success", Status.SUCCESS, propertyMigrator.getStatus()); + dbEngine.commit(); + + JanusGraphTransaction janusgraphTransaction = AAIGraph.getInstance().getGraph().newTransaction(); + GraphTraversalSource g = janusgraphTransaction.traversal(); + + List oldVList = g.V().has("aai-node-type", "pserver").has(oldPropName).toList(); + List newVList = g.V().has("aai-node-type", "pserver").has(newPropName).toList(); + + assertEquals("Expecting the vertex list with old property to be zero", 0, oldVList.size()); + assertEquals("Expecting the vertex list with new property to be 1", 1, newVList.size()); + assertEquals("Expecting the equipment type to be some equipment", "some status", newVList.get(0).property(newPropName).value()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/ValueMigratorTest.java b/src/test/java/org/onap/aai/migration/ValueMigratorTest.java new file mode 100644 index 0000000..0b64ccd --- /dev/null +++ b/src/test/java/org/onap/aai/migration/ValueMigratorTest.java @@ -0,0 +1,150 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.junit.Assert.assertTrue; + +public class ValueMigratorTest extends AAISetup{ + + public static class SampleValueMigrator extends ValueMigrator { + public SampleValueMigrator(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions, Map map, Boolean updateExistingValues){ + super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions, map, updateExistingValues); + } + @Override + public Status getStatus() { + return Status.SUCCESS; + } + @Override + public Optional getAffectedNodeTypes() { + return null; + } + @Override + public String getMigrationName() { + return "SampleValueMigrator"; + } + } + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private SampleValueMigrator migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + private SampleValueMigrator existingValuesMigration; + + @Before + public void setup() throws Exception{ + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + Map map = new HashMap<>(); + Map pair = new HashMap<>(); + pair.put("in-maint", true); + map.put("pserver", pair); + map.put("pnf", pair); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver0") + .next(); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver2") + .property("in-maint", false) + .next(); + g.addV().property("aai-node-type", "pnf") + .property("pnf-name","pnf1" ) + .property("in-maint", false) + .next(); + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new SampleValueMigrator(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions, map, false); + migration.run(); + + map = new HashMap<>(); + pair = new HashMap<>(); + pair.put("in-maint", true); + map.put("pnf", pair); + existingValuesMigration = new SampleValueMigrator(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions, map, true); + existingValuesMigration.run(); + } + + @Test + public void testMissingProperty(){ + assertTrue("Value of pnf should be updated since the property doesn't exist", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver0").has("in-maint", true).hasNext()); + } + + @Test + public void testExistingValue() { + assertTrue("Value of pserver shouldn't be updated since it already exists", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver2").has("in-maint", false).hasNext()); + } + + @Test + public void testEmptyValue() { + assertTrue("Value of pserver should be updated since the value is an empty string", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver1").has("in-maint", true).hasNext()); + } + + @Test + public void testUpdateExistingValues() { + assertTrue("Value of pnf should be updated even though it already exists", + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf1").has("in-maint", true).hasNext()); + } +} diff --git a/src/test/java/org/onap/aai/migration/VertexMergeTest.java b/src/test/java/org/onap/aai/migration/VertexMergeTest.java new file mode 100644 index 0000000..501072b --- /dev/null +++ b/src/test/java/org/onap/aai/migration/VertexMergeTest.java @@ -0,0 +1,177 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration; + +import org.janusgraph.core.Cardinality; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.schema.JanusGraphManagement; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.db.DBSerializer; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import java.io.UnsupportedEncodingException; +import java.util.*; + +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +@Ignore +public class VertexMergeTest extends AAISetup { + + + private final static SchemaVersion version = new SchemaVersion("v10"); + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private GraphTraversalSource g; + private Graph tx; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + JanusGraphManagement mgmt = graph.openManagement(); + mgmt.makePropertyKey("test-list").dataType(String.class).cardinality(Cardinality.SET).make(); + mgmt.commit(); + Vertex pserverSkeleton = g.addV().property("aai-node-type", "pserver").property("hostname", "TEST1") + .property("source-of-truth", "AAI-EXTENSIONS").property("fqdn", "test1.com").property("test-list", "value1").next(); + + Vertex pInterface1 = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface1") + .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface1").next(); + + Vertex pInterface2 = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface2") + .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2").next(); + + Vertex pInterface2Secondary = g.addV().property("aai-node-type", "p-interface").property("interface-name", "p-interface2").property("special-prop", "value") + .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2").next(); + + Vertex lInterface1 = g.addV().property("aai-node-type", "l-interface").property("interface-name", "l-interface1").property("special-prop", "value") + .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2/l-interfaces/l-interface/l-interface1").next(); + + Vertex lInterface1Canopi = g.addV().property("aai-node-type", "l-interface").property("interface-name", "l-interface1") + .property(AAIProperties.AAI_URI, "/cloud-infrastructure/pservers/pserver/TEST1/p-interfaces/p-interface/p-interface2/l-interfaces/l-interface/l-interface1").next(); + + Vertex logicalLink = g.addV().property("aai-node-type", "logical-link").property("link-name", "logical-link1") + .property(AAIProperties.AAI_URI, "/network/logical-links/logical-link/logical-link1").next(); + Vertex pserverCanopi = g.addV().property("aai-node-type", "pserver").property("hostname", "TEST1") + .property("source-of-truth", "CANOPI-WS").property("fqdn", "test2.com").property("test-list", "value2").next(); + + Vertex complex1 = g.addV().property("aai-node-type", "complex").property("physical-location-id", "complex1") + .property("source-of-truth", "RO").next(); + + Vertex complex2 = g.addV().property("aai-node-type", "complex").property("physical-location-id", "complex2") + .property("source-of-truth", "RCT").next(); + + Vertex vserver1 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver1") + .property("source-of-truth", "RO").next(); + + Vertex vserver2 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver2") + .property("source-of-truth", "RCT").next(); + Vertex vserver3 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver3") + .property("source-of-truth", "RCT").next(); + Vertex vserver4 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver4") + .property("source-of-truth", "RCT").next(); + Vertex vserver5 = g.addV().property("aai-node-type", "vserver").property("vserver-id", "vserver5") + .property("source-of-truth", "RCT").next(); + + + edgeSerializer.addEdge(g, pserverSkeleton, complex1); + edgeSerializer.addEdge(g, pserverSkeleton, vserver1); + edgeSerializer.addEdge(g, pserverSkeleton, vserver2); + edgeSerializer.addTreeEdge(g, pserverSkeleton, pInterface1); + edgeSerializer.addTreeEdge(g, pserverSkeleton, pInterface2Secondary); + edgeSerializer.addTreeEdge(g, pInterface2Secondary, lInterface1); + edgeSerializer.addEdge(g, lInterface1, logicalLink); + edgeSerializer.addEdge(g, pserverCanopi, complex2); + edgeSerializer.addEdge(g, pserverCanopi, vserver3); + edgeSerializer.addEdge(g, pserverCanopi, vserver4); + edgeSerializer.addEdge(g, pserverCanopi, vserver5); + edgeSerializer.addTreeEdge(g, pserverCanopi, pInterface2); + edgeSerializer.addTreeEdge(g, pInterface2, lInterface1Canopi); + + Map> forceCopy = new HashMap<>(); + Set forceSet = new HashSet<>(); + forceSet.add("fqdn"); + forceCopy.put("pserver", forceSet); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "Merge test"); + + VertexMerge merge = new VertexMerge.Builder(loader, spy, serializer).edgeSerializer(edgeSerializer).build(); + merge.performMerge(pserverCanopi, pserverSkeleton, forceCopy, basePath); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void run() throws UnsupportedEncodingException { + + assertEquals("pserver merged", false, g.V().has("hostname", "TEST1").has("source-of-truth", "AAI-EXTENSIONS").hasNext()); + assertThat("pserver list merge", Arrays.asList("value1", "value2"), containsInAnyOrder(g.V().has("hostname", "TEST1").values("test-list").toList().toArray())); + assertEquals("canopi pserver has one edge to vserver2", 1, g.V().has("hostname", "TEST1").both().has("vserver-id", "vserver2").toList().size()); + assertEquals("canopi pserver has one edge to vserver1", 1, g.V().has("hostname", "TEST1").both().has("vserver-id", "vserver1").toList().size()); + assertEquals("canopi pserver retained edge to complex2", true, g.V().has("hostname", "TEST1").both().has("physical-location-id", "complex2").hasNext()); + assertEquals("canopi pserver received forced prop", "test1.com", g.V().has("hostname", "TEST1").values("fqdn").next()); + assertEquals("pserver skeleton child copied", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface1").hasNext()); + assertEquals("pserver skeleton child merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").has("special-prop", "value").hasNext()); + assertEquals("l-interface child merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").both().has("interface-name", "l-interface1").has("special-prop", "value").hasNext()); + assertEquals("l-interface child cousin edge merged", true, g.V().has("hostname", "TEST1").both().has("interface-name", "p-interface2").both().has("interface-name", "l-interface1").both().has("link-name", "logical-link1").hasNext()); + assertEquals("one l-interface1 found", new Long(1), g.V().has("interface-name", "l-interface1").count().next()); + assertEquals("one p-interface2 found", new Long(1), g.V().has("interface-name", "p-interface2").count().next()); + + } +} diff --git a/src/test/java/org/onap/aai/migration/v12/ContainmentDeleteOtherVPropertyMigrationTest.java b/src/test/java/org/onap/aai/migration/v12/ContainmentDeleteOtherVPropertyMigrationTest.java new file mode 100644 index 0000000..d159ef8 --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/ContainmentDeleteOtherVPropertyMigrationTest.java @@ -0,0 +1,112 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.schema.JanusGraphManagement; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Edge; +import org.apache.tinkerpop.gremlin.structure.Graph; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.edges.enums.EdgeProperty; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.edges.enums.AAIDirection; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class ContainmentDeleteOtherVPropertyMigrationTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private ContainmentDeleteOtherVPropertyMigration migration; + private GraphTraversalSource g; + private Graph tx; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + JanusGraphManagement janusgraphManagement = graph.openManagement(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + Vertex v = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "delcontains-test-vnf") + .next(); + Vertex v2 = g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "delcontains-test-lint") + .next(); + Edge e = v.addEdge("hasLInterface", v2, EdgeProperty.CONTAINS.toString(), AAIDirection.OUT.toString(), + EdgeProperty.DELETE_OTHER_V.toString(), AAIDirection.NONE.toString()); + + Vertex v3 = g.addV().property("aai-node-type", "allotted-resource").next(); + + Edge e2 = v2.addEdge("uses", v3, EdgeProperty.CONTAINS.toString(), AAIDirection.NONE.toString(), + EdgeProperty.DELETE_OTHER_V.toString(), AAIDirection.NONE.toString()); + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + Mockito.doReturn(janusgraphManagement).when(adminSpy).getManagementSystem(); + migration = new ContainmentDeleteOtherVPropertyMigration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions, "/edgeMigrationTestRules.json"); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void run() { + assertEquals("del other now OUT", true, + g.E().hasLabel("hasLInterface").has(EdgeProperty.DELETE_OTHER_V.toString(), AAIDirection.OUT.toString()).hasNext()); + assertEquals("contains val still same", true, + g.E().hasLabel("hasLInterface").has(EdgeProperty.CONTAINS.toString(), AAIDirection.OUT.toString()).hasNext()); + assertEquals("non-containment unchanged", true, + g.E().hasLabel("uses").has(EdgeProperty.DELETE_OTHER_V.toString(), AAIDirection.NONE.toString()).hasNext()); + } + +} diff --git a/src/test/java/org/onap/aai/migration/v12/DeletePInterfaceTest.java b/src/test/java/org/onap/aai/migration/v12/DeletePInterfaceTest.java new file mode 100644 index 0000000..665000f --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/DeletePInterfaceTest.java @@ -0,0 +1,145 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.migration.Status; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class DeletePInterfaceTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private DeletePInterface migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + System.setProperty("AJSC_HOME", "."); + System.setProperty("BUNDLECONFIG_DIR", "src/test/resources"); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex pnf1 = g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf-name1") + .next(); + Vertex pInterface1 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "interface-name1") + .property("source-of-truth", "AAI-CSVP-INSTARAMS") + .next(); + edgeSerializer.addTreeEdge(g, pnf1, pInterface1); + + Vertex pnf2 = g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf-name2") + .next(); + Vertex pInterface2 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "interface-name2") + .property("source-of-truth", "AAI-CSVP-INSTARAMS") + .next(); + Vertex pLink = g.addV().property("aai-node-type", "physical-link") + .property("interface-name", "interface-name1") + .next(); + edgeSerializer.addTreeEdge(g, pnf2, pInterface2); + edgeSerializer.addEdge(g, pInterface2, pLink); + + Vertex pnf3 = g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf-name3") + .next(); + Vertex pInterface3 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "interface-name3") + .property("source-of-truth", "AAI-CSVP-INSTARAMS") + .next(); + Vertex lInterface = g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "interface-name3") + .next(); + edgeSerializer.addTreeEdge(g, pnf3, pInterface3); + edgeSerializer.addTreeEdge(g, pInterface3, lInterface); + + Vertex pnf4 = g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf-name4") + .next(); + Vertex pInterface4 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "interface-name4") + .next(); + edgeSerializer.addTreeEdge(g, pnf4, pInterface4); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new DeletePInterface(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void tearDown() throws Exception { + tx.rollback(); + graph.close(); + } + + @Test + public void test() { + assertEquals("pInterface1 deleted", false, g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf-name1") + .in("tosca.relationships.network.BindsTo").has("aai-node-type", "p-interface").has("interface-name", "interface-name1").hasNext()); + + assertEquals("pInterface2 skipped", true, g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf-name2") + .in("tosca.relationships.network.BindsTo").has("aai-node-type", "p-interface").hasNext()); + + assertEquals("pInterface3 skipped", true, g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf-name3") + .in("tosca.relationships.network.BindsTo").has("aai-node-type", "p-interface").hasNext()); + + assertEquals("pInterface4 should not be deleted", true, g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf-name4") + .in("tosca.relationships.network.BindsTo").has("aai-node-type", "p-interface").has("interface-name", "interface-name4").hasNext()); + + assertEquals("Status should be success", Status.SUCCESS, migration.getStatus()); + } + +} diff --git a/src/test/java/org/onap/aai/migration/v12/MigrateModelVerDistributionStatusPropertyTest.java b/src/test/java/org/onap/aai/migration/v12/MigrateModelVerDistributionStatusPropertyTest.java new file mode 100644 index 0000000..3571f5f --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/MigrateModelVerDistributionStatusPropertyTest.java @@ -0,0 +1,106 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + + +public class MigrateModelVerDistributionStatusPropertyTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateModelVerDistriubutionStatusProperty migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + Vertex modelVer1; + Vertex modelVer2; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + modelVer1 = g.addV().property("aai-node-type", "model-ver") + .property("model-version-id", "modelVer1") + .property("distribution-status", "test1") + .next(); + + modelVer2 = g.addV().property("aai-node-type", "model-ver") + .property("model-version-id", "modelVer1") + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new MigrateModelVerDistriubutionStatusProperty(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + + /*** + * checks if the Distribution Status value was changed + */ + + @Test + public void confirmDistributionStatusChanged() { + + assertEquals("DISTRIBUTION_COMPLETE_OK",modelVer1.property("distribution-status").value()); + assertEquals("DISTRIBUTION_COMPLETE_OK",modelVer2.property("distribution-status").value()); + + } + + +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTest.java b/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTest.java new file mode 100644 index 0000000..0bfdb41 --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTest.java @@ -0,0 +1,404 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + +public class MigrateServiceInstanceToConfigurationTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateServiceInstanceToConfiguration migration; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex customer1 = g.addV() + .property("aai-node-type", "customer") + .property("global-customer-id", "customer-id-1") + .property("subscriber-type", "CUST") + .next(); + + Vertex customer2 = g.addV() + .property("aai-node-type", "customer") + .property("global-customer-id", "customer-id-2") + .property("subscriber-type", "CUST") + .next(); + + Vertex customer3 = g.addV() + .property("aai-node-type", "customer") + .property("global-customer-id", "customer-id-3") + .property("subscriber-type", "CUST") + .next(); + + Vertex customer4 = g.addV() + .property("aai-node-type", "customer") + .property("global-customer-id", "customer-id-4") + .property("subscriber-type", "CUST") + .next(); + + Vertex servSub1 = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + + Vertex servSub2 = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "OTHER") + .next(); + + Vertex servSub3 = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + + Vertex servSub4 = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + + Vertex servSub5 = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + + Vertex servInstance1 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "service-inst-1") + .property("operational-status", "activated") + .property("bandwidth-total", "5") + .next(); + + Vertex servInstance2 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "service-inst-2") + .property("operational-status", "activated") + .property("bandwidth-total", "8") + .next(); + + Vertex servInstance3 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "service-inst-3") + .property("operational-status", "activated") + .property("bandwidth-total", "10") + .next(); + + Vertex servInstance4 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "service-inst-4") + .property("operational-status", "activated") + .property("bandwidth-total", "15") + .next(); + + Vertex config1 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "configuration-1") + .property("configuration-type", "DHV") + .property("tunnel-bandwidth", "7") + .next(); + + Vertex config2 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "configuration-2") + .property("configuration-type", "OTHER") + .property("tunnel-bandwidth", "3") + .next(); + + Vertex config3 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "configuration-3") + .property("configuration-type", "OTHER") + .property("tunnel-bandwidth", "2") + .next(); + + Vertex config4 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "configuration-4") + .property("configuration-type", "OTHER") + .property("tunnel-bandwidth", "4") + .next(); + + // graph 1 + edgeSerializer.addTreeEdge(g, customer1, servSub1); + edgeSerializer.addTreeEdge(g, customer1, servSub2); + edgeSerializer.addTreeEdge(g, servSub1, servInstance1); + edgeSerializer.addTreeEdge(g, servSub2, servInstance2); + + // graph 2 + edgeSerializer.addTreeEdge(g, customer2, servSub3); + + // graph 3 + edgeSerializer.addTreeEdge(g, customer3, servSub4); + edgeSerializer.addTreeEdge(g, servSub4, servInstance3); + edgeSerializer.addEdge(g, servInstance3, config1); + edgeSerializer.addEdge(g, servInstance3, config2); + + // graph 4 + edgeSerializer.addTreeEdge(g, customer4, servSub5); + edgeSerializer.addTreeEdge(g, servSub5, servInstance4); + edgeSerializer.addEdge(g, servInstance4, config3); + edgeSerializer.addEdge(g, servInstance4, config4); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateServiceInstanceToConfiguration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testRun_createConfigNode() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-id-1") + .hasNext()); + + assertEquals("service subscription node, service-type=DHV", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=5", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1").has("bandwidth-total", "5") + .hasNext()); + + // check if configuration node gets created + assertEquals("configuration node exists", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .hasNext()); + + // check configuration type + assertEquals("configuration node, configuration-type=DHV", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("configuration-type", "DHV") + .hasNext()); + + // check configuration tunnel-bandwidth + assertEquals("configuration node, tunnel-bandwidth=5", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "5") + .hasNext()); + } + + @Test + public void testRun_configNodeNotCreated() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-id-1") + .hasNext()); + + assertEquals("service subscription node, service-type=OTHER", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=8", true, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2").has("bandwidth-total", "8") + .hasNext()); + + // configuration node should not be created + assertEquals("configuration node does not exist", false, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .hasNext()); + + // edge between service instance and configuration should not be created + assertEquals("configuration node does not exist", false, + g.V().has("global-customer-id", "customer-id-1") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2") + .out("org.onap.relationships.inventory.Uses").hasNext()); + } + + @Test + public void testRun_noServiceInstance() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-id-2") + .hasNext()); + + assertEquals("service subscription node, service-type=DHV", true, + g.V().has("global-customer-id", "customer-id-2") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .hasNext()); + + // no service instance nodes + assertEquals("no service instance nodes", false, + g.V().has("global-customer-id", "customer-id-2") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "service-instance") + .hasNext()); + } + + @Test + public void testRun_existingConfig() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-id-3") + .hasNext()); + + assertEquals("service subscription node, service-type=DHV", true, + g.V().has("global-customer-id", "customer-id-3") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=10", true, + g.V().has("global-customer-id", "customer-id-3") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3").has("bandwidth-total", "10") + .hasNext()); + + assertEquals("configuration node with type DHV, tunnel-bandwidth changed to 10", true, + g.V().has("global-customer-id", "customer-id-3") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "10") + .hasNext()); + + assertEquals("configuration node with type OTHER, tunnel-bandwidth remains same", true, + g.V().has("global-customer-id", "customer-id-3") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "3") + .hasNext()); + } + + @Test + public void testRun_existingConfigNotDHV() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-id-4") + .hasNext()); + + assertEquals("service subscription node, service-type=DHV", true, + g.V().has("global-customer-id", "customer-id-4") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=15", true, + g.V().has("global-customer-id", "customer-id-4") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4").has("bandwidth-total", "15") + .hasNext()); + + assertEquals("first configuration node with type OTHER, tunnel-bandwidth remains same", true, + g.V().has("global-customer-id", "customer-id-4") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "2") + .hasNext()); + + assertEquals("second configuration node with type OTHER, tunnel-bandwidth remains same", true, + g.V().has("global-customer-id", "customer-id-4") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "4") + .hasNext()); + + assertEquals("new configuration node created with type DHV, tunnel-bandwidth=15", true, + g.V().has("global-customer-id", "customer-id-4") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "15") + .hasNext()); + } + + @Test + public void testGetAffectedNodeTypes() { + Optional types = migration.getAffectedNodeTypes(); + Optional expected = Optional.of(new String[]{"service-instance"}); + + assertNotNull(types); + assertArrayEquals(expected.get(), types.get()); + } + + @Test + public void testGetMigrationName() { + String migrationName = migration.getMigrationName(); + + assertNotNull(migrationName); + assertEquals("service-instance-to-configuration", migrationName); + } +} diff --git a/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTestPreMigrationMock.java b/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTestPreMigrationMock.java new file mode 100644 index 0000000..ccec10b --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/MigrateServiceInstanceToConfigurationTestPreMigrationMock.java @@ -0,0 +1,299 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + +public class MigrateServiceInstanceToConfigurationTestPreMigrationMock extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateServiceInstanceToConfiguration migration; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex customer = g.addV() + .property("aai-node-type", "customer") + .property("global-customer-id", "customer-9972-BandwidthMigration") + .property("subscriber-type", "CUST") + .next(); + + Vertex servSubSDNEI = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "SDN-ETHERNET-INTERNET") + .next(); + + Vertex servInstance22 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "servInstance-9972-22-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "bandwidth-total-22-BandwidthMigration") + .next(); + + Vertex servInstance11 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "servInstance-9972-11-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "bandwidth-total-11-BandwidthMigration") + .next(); + + Vertex servSubDHV = g.addV() + .property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + + Vertex servInstance4 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "servInstance-9972-4-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "bandwidth-total-4-BandwidthMigration") + .next(); + + Vertex servInstance1 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "ServInstance-9972-1-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "2380") + .next(); + + Vertex servInstance3 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "servInstance-9972-3-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "bandwidth-total-3-BandwidthMigration") + .next(); + + Vertex servInstance2 = g.addV() + .property("aai-node-type", "service-instance") + .property("service-instance-id", "servInstance-9972-2-BandwidthMigration") + .property("operational-status", "activated") + .property("bandwidth-total", "bandwidth-total-2-BandwidthMigration") + .next(); + + Vertex config1 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "9972-config-LB1113") + .property("configuration-type", "DHV") + .property("tunnel-bandwidth", "12") + .next(); + + Vertex config2 = g.addV() + .property("aai-node-type", "configuration") + .property("configuration-id", "9972-1config-LB1113") + .property("configuration-type", "configuration-type1-9972") + .next(); + + Vertex allottedResource = g.addV() + .property("aai-node-type", "allotted-resource") + .property("id", "allResource-9972-BandwidthMigration") + .next(); + + edgeSerializer.addTreeEdge(g, customer, servSubSDNEI); + edgeSerializer.addTreeEdge(g, customer, servSubDHV); + edgeSerializer.addTreeEdge(g, servSubSDNEI, servInstance22); + edgeSerializer.addTreeEdge(g, servSubSDNEI, servInstance11); + edgeSerializer.addTreeEdge(g, servSubDHV, servInstance4); + edgeSerializer.addTreeEdge(g, servSubDHV, servInstance1); + edgeSerializer.addTreeEdge(g, servSubDHV, servInstance3); + edgeSerializer.addTreeEdge(g, servSubDHV, servInstance2); + edgeSerializer.addEdge(g, servInstance1, allottedResource); + edgeSerializer.addEdge(g, servInstance1, config1); + edgeSerializer.addEdge(g, servInstance2, config2); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateServiceInstanceToConfiguration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testRun() throws Exception { + // check if graph nodes exist + assertEquals("customer node exists", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .hasNext()); + + assertEquals("service subscription node, service-type=SDN-ETHERNET-INTERNET", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=bandwidth-total-22-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-22-BandwidthMigration") + .has("bandwidth-total", "bandwidth-total-22-BandwidthMigration") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=bandwidth-total-11-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-11-BandwidthMigration") + .has("bandwidth-total", "bandwidth-total-11-BandwidthMigration") + .hasNext()); + + assertEquals("service subscription node, service-type=DHV", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=servInstance-9972-4-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration") + .has("bandwidth-total", "bandwidth-total-4-BandwidthMigration") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=ServInstance-9972-1-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration") + .has("bandwidth-total", "2380") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=servInstance-9972-3-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration") + .has("bandwidth-total", "bandwidth-total-3-BandwidthMigration") + .hasNext()); + + assertEquals("service instance node, bandwidth-total=servInstance-9972-2-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration") + .has("bandwidth-total", "bandwidth-total-2-BandwidthMigration") + .hasNext()); + + assertEquals("configuration node with type=configuration-type1-9972, tunnel-bandwidth does not exist", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .has("configuration-type", "configuration-type1-9972") + .hasNext()); + + // check if configuration node gets created for 2, 3, 4 + assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-4-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-4-BandwidthMigration") + .hasNext()); + + assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-3-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-3-BandwidthMigration") + .hasNext()); + + assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-2-BandwidthMigration", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-2-BandwidthMigration") + .hasNext()); + + // configuration modified for ServInstance-9972-1-BandwidthMigration + assertEquals("configuration node modified for ServInstance-9972-1-BandwidthMigration, tunnel-bandwidth=2380", true, + g.V().has("global-customer-id", "customer-9972-BandwidthMigration") + .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV") + .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration") + .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration") + .has("configuration-type", "DHV").has("tunnel-bandwidth", "2380") + .hasNext()); + } + + @Test + public void testGetAffectedNodeTypes() { + Optional types = migration.getAffectedNodeTypes(); + Optional expected = Optional.of(new String[]{"service-instance"}); + + assertNotNull(types); + assertArrayEquals(expected.get(), types.get()); + } + + @Test + public void testGetMigrationName() { + String migrationName = migration.getMigrationName(); + + assertNotNull(migrationName); + assertEquals("service-instance-to-configuration", migrationName); + } +} diff --git a/src/test/java/org/onap/aai/migration/v12/SDWANSpeedChangeMigrationTest.java b/src/test/java/org/onap/aai/migration/v12/SDWANSpeedChangeMigrationTest.java new file mode 100644 index 0000000..8041880 --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/SDWANSpeedChangeMigrationTest.java @@ -0,0 +1,375 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class SDWANSpeedChangeMigrationTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private SDWANSpeedChangeMigration migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + + Vertex pLinkWan1; + Vertex pLinkWan3; + Vertex pLinkWan5; + Vertex pLinkWan7; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex servSub1 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + Vertex servinst1 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "DHV") + .next(); + Vertex allotedRsrc1 = g.addV().property("aai-node-type", "allotted-resource") + .property("id", "rsrc1") + .next(); + Vertex servinst2 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "VVIG") + .next(); + Vertex servSub2 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .next(); + + Vertex genericvnf1 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId1") + .next(); + Vertex vServer1 = g.addV().property("aai-node-type", "vserver") + .next(); + Vertex pServer1 = g.addV().property("aai-node-type", "pserver") + .next(); + Vertex pInterfaceWan1 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "ge-0/0/10") + .next(); + Vertex tunnelXConnectAll_Wan1 = g.addV().property("aai-node-type", "tunnel-xconnect") + .property("id", "txc1") + .property("bandwidth-up-wan1", "300 Mbps") + .property("bandwidth-down-wan1", "400 Mbps") + .property("bandwidth-up-wan2", "500 Mbps") + .property("bandwidth-down-wan2", "600 Mbps") + .next(); + + pLinkWan1 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan1") + .property("service-provider-bandwidth-up-value", "empty") + .property("service-provider-bandwidth-up-units", "empty") + .property("service-provider-bandwidth-down-value", "empty") + .property("service-provider-bandwidth-down-units", "empty") + .next(); + Vertex servSub3 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + Vertex servinst3 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "DHV") + .next(); + Vertex allotedRsrc3 = g.addV().property("aai-node-type", "allotted-resource") + .property("id", "rsrc1") + .next(); + Vertex servinst4 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "VVIG") + .next(); + Vertex servSub4 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .next(); + + Vertex genericvnf3 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId1") + .next(); + Vertex vServer3 = g.addV().property("aai-node-type", "vserver") + .next(); + Vertex pServer3 = g.addV().property("aai-node-type", "pserver") + .next(); + Vertex pInterfaceWan3 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "ge-0/0/11") + .next(); + Vertex tunnelXConnectAll_Wan3 = g.addV().property("aai-node-type", "tunnel-xconnect") + .property("id", "txc3") + .property("bandwidth-up-wan1", "300 Mbps") + .property("bandwidth-down-wan1", "400 Mbps") + .property("bandwidth-up-wan2", "500 Mbps") + .property("bandwidth-down-wan2", "600 Mbps") + .next(); + + pLinkWan3 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan3") + .property("service-provider-bandwidth-up-value", "empty") + .property("service-provider-bandwidth-up-units", "empty") + .property("service-provider-bandwidth-down-value", "empty") + .property("service-provider-bandwidth-down-units", "empty") + .next(); + + + Vertex servSub5 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + Vertex servinst5 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "DHV") + .next(); + Vertex allotedRsrc5 = g.addV().property("aai-node-type", "allotted-resource") + .property("id", "rsrc1") + .next(); + Vertex servinst6 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "VVIG") + .next(); + Vertex servSub6 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .next(); + + Vertex genericvnf5 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId1") + .next(); + Vertex vServer5 = g.addV().property("aai-node-type", "vserver") + .next(); + Vertex pServer5 = g.addV().property("aai-node-type", "pserver") + .next(); + Vertex pInterfaceWan5 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "ge-0/0/10") + .next(); + Vertex tunnelXConnectAll_Wan5 = g.addV().property("aai-node-type", "tunnel-xconnect") + .property("id", "txc5") + .property("bandwidth-up-wan1", "") + .property("bandwidth-down-wan1", "") + .property("bandwidth-up-wan2", "500 Mbps") + .property("bandwidth-down-wan2", "600 Mbps") + .next(); + + pLinkWan5 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan5") + .property("service-provider-bandwidth-up-value", "") + .property("service-provider-bandwidth-up-units", "") + .property("service-provider-bandwidth-down-value", "") + .property("service-provider-bandwidth-down-units", "") + .next(); + + + Vertex servSub7 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "DHV") + .next(); + Vertex servinst7 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "DHV") + .next(); + Vertex allotedRsrc7 = g.addV().property("aai-node-type", "allotted-resource") + .property("id", "rsrc1") + .next(); + Vertex servinst9 = g.addV().property("aai-node-type", "service-instance") + .property("service-type", "VVIG") + .next(); + Vertex servSub9 = g.addV().property("aai-node-type", "service-subscription") + .property("service-type", "VVIG") + .next(); + + Vertex genericvnf7 = g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "vnfId1") + .next(); + Vertex vServer7 = g.addV().property("aai-node-type", "vserver") + .next(); + Vertex pServer7 = g.addV().property("aai-node-type", "pserver") + .next(); + Vertex pInterfaceWan7 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "ge-0/0/11") + .next(); + Vertex tunnelXConnectAll_Wan7 = g.addV().property("aai-node-type", "tunnel-xconnect") + .property("id", "txc7") + .property("bandwidth-up-wan1", "300 Mbps") + .property("bandwidth-down-wan1", "400 Mbps") + .property("bandwidth-up-wan2", "") + .property("bandwidth-down-wan2", "") + .next(); + + pLinkWan7 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "pLinkWan5") + .property("service-provider-bandwidth-up-value", "") + .property("service-provider-bandwidth-up-units", "") + .property("service-provider-bandwidth-down-value", "") + .property("service-provider-bandwidth-down-units", "") + .next(); + + + + edgeSerializer.addTreeEdge(g, servSub1, servinst1); + edgeSerializer.addEdge(g, servinst1, allotedRsrc1); + edgeSerializer.addTreeEdge(g, servinst2, servSub2); + edgeSerializer.addTreeEdge(g, allotedRsrc1, servinst2); + + edgeSerializer.addTreeEdge(g, allotedRsrc1, tunnelXConnectAll_Wan1); + + + edgeSerializer.addEdge(g, servinst1, genericvnf1); + edgeSerializer.addEdge(g, genericvnf1, vServer1); + edgeSerializer.addEdge(g, vServer1, pServer1); + edgeSerializer.addTreeEdge(g, pServer1, pInterfaceWan1); + edgeSerializer.addEdge(g, pInterfaceWan1, pLinkWan1); + + edgeSerializer.addTreeEdge(g, servSub3, servinst3); + edgeSerializer.addEdge(g, servinst3, allotedRsrc3); + edgeSerializer.addTreeEdge(g, servinst4, servSub4); + edgeSerializer.addTreeEdge(g, allotedRsrc3, servinst4); + + edgeSerializer.addTreeEdge(g, allotedRsrc3, tunnelXConnectAll_Wan3); + + + edgeSerializer.addEdge(g, servinst3, genericvnf3); + edgeSerializer.addEdge(g, genericvnf3, vServer3); + edgeSerializer.addEdge(g, vServer3, pServer3); + edgeSerializer.addTreeEdge(g, pServer3, pInterfaceWan3); + edgeSerializer.addEdge(g, pInterfaceWan3, pLinkWan3); + + + edgeSerializer.addTreeEdge(g, servSub5, servinst5); + edgeSerializer.addEdge(g, servinst5, allotedRsrc5); + edgeSerializer.addTreeEdge(g, servinst6, servSub6); + edgeSerializer.addTreeEdge(g, allotedRsrc5, servinst6); + + edgeSerializer.addTreeEdge(g, allotedRsrc5, tunnelXConnectAll_Wan5); + + + edgeSerializer.addEdge(g, servinst5, genericvnf5); + edgeSerializer.addEdge(g, genericvnf5, vServer5); + edgeSerializer.addEdge(g, vServer5, pServer5); + edgeSerializer.addTreeEdge(g, pServer5, pInterfaceWan5); + edgeSerializer.addEdge(g, pInterfaceWan5, pLinkWan5); + + edgeSerializer.addTreeEdge(g, servSub7, servinst7); + edgeSerializer.addEdge(g, servinst7, allotedRsrc7); + edgeSerializer.addTreeEdge(g, servinst9, servSub9); + edgeSerializer.addTreeEdge(g, allotedRsrc7, servinst9); + + edgeSerializer.addTreeEdge(g, allotedRsrc7, tunnelXConnectAll_Wan7); + + + edgeSerializer.addEdge(g, servinst7, genericvnf7); + edgeSerializer.addEdge(g, genericvnf7, vServer7); + edgeSerializer.addEdge(g, vServer7, pServer7); + edgeSerializer.addTreeEdge(g, pServer7, pInterfaceWan7); + edgeSerializer.addEdge(g, pInterfaceWan7, pLinkWan7); + + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new SDWANSpeedChangeMigration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + + /*** + * Checks to see if the Wan1 properties were updated in the physical link + */ + + @Test + public void ConfirmWan1Changes() { + + assertEquals("300", pLinkWan1.property("service-provider-bandwidth-up-value").value().toString()); + assertEquals("Mbps", pLinkWan1.property("service-provider-bandwidth-up-units").value().toString()); + assertEquals("400", pLinkWan1.property("service-provider-bandwidth-down-value").value().toString()); + assertEquals("Mbps", pLinkWan1.property("service-provider-bandwidth-down-units").value().toString()); + + } + + /*** + * Checks to see if the Wan2 properties were updated in the physical link + */ + @Test + public void ConfirmWan2Changes() { + + assertEquals("500", pLinkWan3.property("service-provider-bandwidth-up-value").value().toString()); + assertEquals("Mbps", pLinkWan3.property("service-provider-bandwidth-up-units").value().toString()); + assertEquals("600", pLinkWan3.property("service-provider-bandwidth-down-value").value().toString()); + assertEquals("Mbps", pLinkWan3.property("service-provider-bandwidth-down-units").value().toString()); + + } + + /*** + * if tunnel xconncets missing bandwidth up 1 value the plink should not be updated + */ + + @Test + public void Wan1EmptyNoChanges() { + + assertEquals("", pLinkWan5.property("service-provider-bandwidth-up-value").value().toString()); + assertEquals("", pLinkWan5.property("service-provider-bandwidth-up-units").value().toString()); + assertEquals("", pLinkWan5.property("service-provider-bandwidth-down-value").value().toString()); + assertEquals("", pLinkWan5.property("service-provider-bandwidth-down-units").value().toString()); + + } + + /*** + * if tunnel xconncets missing bandwidth up 2 value the plink should not be updated + */ + + @Test + public void Wan2EmptyNoChanges() { + + assertEquals("", pLinkWan7.property("service-provider-bandwidth-up-value").value().toString()); + assertEquals("", pLinkWan7.property("service-provider-bandwidth-up-units").value().toString()); + assertEquals("", pLinkWan7.property("service-provider-bandwidth-down-value").value().toString()); + assertEquals("", pLinkWan7.property("service-provider-bandwidth-down-units").value().toString()); + + } + + +} diff --git a/src/test/java/org/onap/aai/migration/v12/UriMigrationTest.java b/src/test/java/org/onap/aai/migration/v12/UriMigrationTest.java new file mode 100644 index 0000000..70ea20d --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v12/UriMigrationTest.java @@ -0,0 +1,169 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v12; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class UriMigrationTest extends AAISetup { + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private UriMigration migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + + private Vertex pnf3; + private Vertex pInterface3; + private Vertex pInterface4; + private Vertex lInterface3; + private Vertex plink3; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + pnf3 = g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf-name3") + .next(); + pInterface3 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface-name3") + .next(); + pInterface4 = g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface-name/4") + .next(); + lInterface3 = g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface-name3") + .next(); + plink3 = g.addV().property("aai-node-type", "physical-link") + .property("link-name", "link-name3") + .next(); + edgeSerializer.addTreeEdge(g, pnf3, pInterface3); + edgeSerializer.addTreeEdge(g, pnf3, pInterface4); + edgeSerializer.addTreeEdge(g, pInterface3, lInterface3); + edgeSerializer.addEdge(g, pInterface3, plink3); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new UriMigration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + + migration.run(); + + } + + @After + public void tearDown() throws Exception { + graph.close(); + } + + @Test + public void allVertexHasUri() throws InterruptedException { + assertEquals(Long.valueOf(0L), g.V().hasNot(AAIProperties.AAI_URI).count().next()); + } + + @Test + public void pnf() { + printVertex(pnf3); + assertEquals("/network/pnfs/pnf/pnf-name3", g.V().has("pnf-name", "pnf-name3").next().value("aai-uri")); + } + + protected void printVertex(Vertex v) { + final StringBuilder sb = new StringBuilder(); + v.properties().forEachRemaining(p -> sb.append("\t").append(p.key()).append(" : ").append(p.value()).append("\n")); + sb.append("\n"); + System.out.println(sb.toString()); + } + + @Test + public void plink3() { + printVertex(plink3); + assertEquals("/network/physical-links/physical-link/link-name3", g.V().has("link-name", "link-name3").next().value("aai-uri")); + } + + @Test + public void pinterface3() { + printVertex(pInterface3); + assertEquals("/network/pnfs/pnf/pnf-name3/p-interfaces/p-interface/p-interface-name3", g.V().has("interface-name", "p-interface-name3").next().value("aai-uri")); + } + + @Test + public void pInterface4() { + printVertex(pInterface4); + assertEquals("/network/pnfs/pnf/pnf-name3/p-interfaces/p-interface/p-interface-name%2F4", g.V().has("interface-name", "p-interface-name/4").next().value("aai-uri")); + } + + @Test + public void getChildrenTopTest() { + migration.seen = new HashSet<>(); + migration.seen.add(pnf3.id()); + assertEquals(new HashSet<>(Arrays.asList(pInterface3, pInterface4)), migration.getChildren(pnf3)); + } + + @Test + public void getChildrenOneDownTest() { + migration.seen = new HashSet<>(); + migration.seen.add(pnf3.id()); + assertEquals(new HashSet<>(Arrays.asList(lInterface3)), migration.getChildren(pInterface3)); + } + + @Test + public void getChildrenTwoDownTest() { + migration.seen = new HashSet<>(); + migration.seen.add(pInterface3.id()); + assertEquals(Collections.EMPTY_SET, migration.getChildren(lInterface3)); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateBooleanDefaultsToFalseTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateBooleanDefaultsToFalseTest.java new file mode 100644 index 0000000..f8434fc --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateBooleanDefaultsToFalseTest.java @@ -0,0 +1,384 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import org.onap.aai.AAISetup; +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + + +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.migration.Status; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import static org.junit.Assert.assertTrue; + +public class MigrateBooleanDefaultsToFalseTest extends AAISetup { + + public static class BooleanDefaultMigrator extends MigrateBooleanDefaultsToFalse { + public BooleanDefaultMigrator(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions){ + super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + } + @Override + public Status getStatus() { + return Status.SUCCESS; + } + @Override + public Optional getAffectedNodeTypes() { + return Optional.of(new String[]{VNF_NODE_TYPE,VSERVER_NODE_TYPE,VNFC_NODE_TYPE,L3NETWORK_NODE_TYPE,SUBNET_NODE_TYPE,LINTERFACE_NODE_TYPE,VFMODULE_NODE_TYPE}); + } + @Override + public String getMigrationName() { + return "MockBooleanDefaultMigrator"; + } + } + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private BooleanDefaultMigrator migration; + private GraphTraversalSource g; + + @Before + public void setup() throws Exception{ + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + //generic-vnf + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf0") + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf1") + .property("is-closed-loop-disabled", "") + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf2") + .property("is-closed-loop-disabled", true) + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf3") + .property("is-closed-loop-disabled", false) + .next(); + //vnfc + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc0") + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc1") + .property("is-closed-loop-disabled", "") + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc2") + .property("is-closed-loop-disabled", true) + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc3") + .property("is-closed-loop-disabled", false) + .next(); + //vserver + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver0") + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver1") + .property("is-closed-loop-disabled", "") + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver2") + .property("is-closed-loop-disabled", true) + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver3") + .property("is-closed-loop-disabled", false) + .next(); + //l3-network + g.addV().property("aai-node-type", "l3-network") + .property("network-id", "l3-network0") + .property("network-name", "l3-network-name0") + .next(); + g.addV().property("aai-node-type", "l3-network") + .property("network-id", "l3-network1") + .property("network-name", "l3-network-name1") + .property("is-bound-to-vpn", "") + .property("is-provider-network", "") + .property("is-shared-network", "") + .property("is-external-network", "") + .next(); + g.addV().property("aai-node-type", "l3-network") + .property("network-id", "l3-network2") + .property("network-name", "l3-network-name2") + .property("is-bound-to-vpn", true) + .property("is-provider-network", true) + .property("is-shared-network", true) + .property("is-external-network", true) + .next(); + g.addV().property("aai-node-type", "l3-network") + .property("network-id", "l3-network3") + .property("network-name", "l3-network-name3") + .property("is-bound-to-vpn", false) + .property("is-provider-network", false) + .property("is-shared-network", false) + .property("is-external-network", false) + .next(); + //subnet + g.addV().property("aai-node-type", "subnet") + .property("subnet-id", "subnet0") + .next(); + g.addV().property("aai-node-type", "subnet") + .property("subnet-id", "subnet1") + .property("dhcp-enabled", "") + .next(); + g.addV().property("aai-node-type", "subnet") + .property("subnet-id", "subnet2") + .property("dhcp-enabled", true) + .next(); + g.addV().property("aai-node-type", "subnet") + .property("subnet-id", "subnet3") + .property("dhcp-enabled", false) + .next(); + //l-interface + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface0") + .property("in-maint", false) + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface1") + .property("in-maint", false) + .property("is-port-mirrored", "") + .property("is-ip-unnumbered", "") + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface2") + .property("in-maint", false) + .property("is-port-mirrored", true) + .property("is-ip-unnumbered", true) + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface3") + .property("in-maint", false) + .property("is-port-mirrored", false) + .property("is-ip-unnumbered", false) + .next(); + //vf-module + g.addV().property("aai-node-type", "vf-module") + .property("vf-module-id", "vf-module0") + .next(); + g.addV().property("aai-node-type", "vf-module") + .property("vf-module-id", "vf-module1") + .property("is-base-vf-module", "") + .next(); + g.addV().property("aai-node-type", "vf-module") + .property("vf-module-id", "vf-module2") + .property("is-base-vf-module", true) + .next(); + g.addV().property("aai-node-type", "vf-module") + .property("vf-module-id", "vf-module3") + .property("is-base-vf-module", false) + .next(); + + //vlan + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan0") + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan1") + .property("is-ip-unnumbered", "") + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan2") + .property("is-ip-unnumbered", true) + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan3") + .property("is-ip-unnumbered", false) + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new BooleanDefaultMigrator(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + + } + + @Test + public void testMissingProperty(){ + //is-closed-loop-disabled + assertTrue("Value of generic-vnf should be updated since the property is-closed-loop-disabled doesn't exist", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf0").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vnfc should be updated since the property is-closed-loop-disabled doesn't exist", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc0").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vserver should be updated since the property is-closed-loop-disabled doesn't exist", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver0").has("is-closed-loop-disabled", false).hasNext()); + //dhcp-enabled + assertTrue("Value of subnet should be updated since the property dhcp-enabled doesn't exist", + g.V().has("aai-node-type", "subnet").has("subnet-id", "subnet0").has("dhcp-enabled", false).hasNext()); + //l3-network: is-bound-to-vpn, is-shared-network, is-external-network + assertTrue("Value of l3-network should be updated since the property is-bound-to-vpn doesn't exist", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network0").has("network-name", "l3-network-name0").has("is-bound-to-vpn", false).hasNext()); + assertTrue("Value of l3-network should be updated since the property is-provider-network doesn't exist", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network0").has("network-name", "l3-network-name0").has("is-provider-network", false).hasNext()); + assertTrue("Value of l3-network should be updated since the property is-shared-network doesn't exist", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network0").has("network-name", "l3-network-name0").has("is-shared-network", false).hasNext()); + assertTrue("Value of l3-network should be updated since the property is-external-network doesn't exist", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network0").has("network-name", "l3-network-name0").has("is-external-network", false).hasNext()); + //l-interface: is-port-mirrored, is-ip-unnumbered + assertTrue("Value of l-interface should be updated since the property is-port-mirrored doesn't exist", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface0").has("is-port-mirrored", false).hasNext()); + assertTrue("Value of l-interface should be updated since the property is-ip-unnumbered doesn't exist", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface0").has("is-ip-unnumbered", false).hasNext()); + //vf-module: is-base-vf-module + assertTrue("Value of vf-module should be updated since the property is-base-vf-module doesn't exist", + g.V().has("aai-node-type", "vf-module").has("vf-module-id", "vf-module0").has("is-base-vf-module", false).hasNext()); + //vlan: is-ip-unnumbered + assertTrue("Value of vlan should be updated since the property is-ip-unnumbered doesn't exist", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan0").has("is-ip-unnumbered", false).hasNext()); + } + + @Test + public void testEmptyValue() { + //is-closed-loop-disabled + assertTrue("Value of generic-vnf should be updated since the value for is-closed-loop-disabled is an empty string", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf1").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vnfc should be updated since the value for is-closed-loop-disabled is an empty string", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc1").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vserver should be updated since the value for is-closed-loop-disabled is an empty string", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver1").has("is-closed-loop-disabled", false).hasNext()); + //dhcp-enabled + assertTrue("Value of subnet should be updated since the value for dhcp-enabled is an empty string", + g.V().has("aai-node-type", "subnet").has("subnet-id", "subnet1").has("dhcp-enabled", false).hasNext()); + //l3-network: is-bound-to-vpn, is-shared-network, is-external-network + assertTrue("Value of l3-network should be updated since the value for is-bound-to-vpn is an empty string", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network1").has("network-name", "l3-network-name1").has("is-bound-to-vpn", false).hasNext()); + assertTrue("Value of l3-network should be updated since the value for is-provider-network is an empty string", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network1").has("network-name", "l3-network-name1").has("is-provider-network", false).hasNext()); + assertTrue("Value of l3-network should be updated since the value for is-shared-network is an empty string", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network1").has("network-name", "l3-network-name1").has("is-shared-network", false).hasNext()); + assertTrue("Value of l3-network should be updated since the value for is-external-network is an empty string", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network1").has("network-name", "l3-network-name1").has("is-external-network", false).hasNext()); + //l-interface: is-port-mirrored, is-ip-unnumbered + assertTrue("Value of l-interface should be updated since the property is-port-mirrored is an empty string", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface1").has("is-port-mirrored", false).hasNext()); + assertTrue("Value of l-interface should be updated since the property is-ip-unnumbered is an empty string", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface1").has("is-ip-unnumbered", false).hasNext()); + //vf-module: is-base-vf-module, is-ip-unnumbered + assertTrue("Value of vf-module should be updated since the property is-base-vf-module is an empty string", + g.V().has("aai-node-type", "vf-module").has("vf-module-id", "vf-module1").has("is-base-vf-module", false).hasNext()); + //vlan: is-ip-unnumbered + assertTrue("Value of vlan should be updated since the property is-ip-unnumbered is an empty string", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan1").has("is-ip-unnumbered", false).hasNext()); + } + + @Test + public void testExistingTrueValues() { + //is-closed-loop-disabled + assertTrue("Value of generic-vnf shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf2").has("is-closed-loop-disabled", true).hasNext()); + assertTrue("Value of vnfc shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc2").has("is-closed-loop-disabled", true).hasNext()); + assertTrue("Value of vserver shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver2").has("is-closed-loop-disabled", true).hasNext()); + //dhcp-enabled + assertTrue("Value of subnet shouldn't be update since dhcp-enabled already exists", + g.V().has("aai-node-type", "subnet").has("subnet-id", "subnet2").has("dhcp-enabled", true).hasNext()); + //l3-network: is-bound-to-vpn, is-shared-network, is-external-network + assertTrue("Value of l3-network shouldn't be updated since is-bound-to-vpn already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network2").has("network-name", "l3-network-name2").has("is-bound-to-vpn", true).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-provider-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network2").has("network-name", "l3-network-name2").has("is-provider-network", true).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-shared-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network2").has("network-name", "l3-network-name2").has("is-shared-network", true).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-external-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network2").has("network-name", "l3-network-name2").has("is-external-network", true).hasNext()); + //l-interface: is-port-mirrored, is-ip-unnumbered + assertTrue("Value of l-interface shouldn't be updated since is-port-mirrored already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface2").has("is-port-mirrored", true).hasNext()); + assertTrue("Value of ll-interface shouldn't be updated since is-ip-unnumbered already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface2").has("is-ip-unnumbered", true).hasNext()); + //vf-module: is-base-vf-module + assertTrue("Value of vf-module shouldn't be updated since is-base-vf-module already exists", + g.V().has("aai-node-type", "vf-module").has("vf-module-id", "vf-module2").has("is-base-vf-module", true).hasNext()); + //vlan: is-ip-unnumbered + assertTrue("Value of vlan shouldn't be updated since is-ip-unnumbered already exists", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan2").has("is-ip-unnumbered", true).hasNext()); + + } + + @Test + public void testExistingFalseValues() { + //is-closed-loop-disabled + assertTrue("Value of generic-vnf shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf3").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vnfc shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc3").has("is-closed-loop-disabled", false).hasNext()); + assertTrue("Value of vserver shouldn't be update since is-closed-loop-disabled already exists", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver3").has("is-closed-loop-disabled", false).hasNext()); + //dhcp-enabled + assertTrue("Value of subnet shouldn't be update since dhcp-enabled already exists", + g.V().has("aai-node-type", "subnet").has("subnet-id", "subnet3").has("dhcp-enabled", false).hasNext()); + //l3-network: is-bound-to-vpn, is-shared-network, is-external-network + assertTrue("Value of l3-network shouldn't be updated since is-bound-to-vpn already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network3").has("network-name", "l3-network-name3").has("is-bound-to-vpn", false).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-provider-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network3").has("network-name", "l3-network-name3").has("is-provider-network", false).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-shared-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network3").has("network-name", "l3-network-name3").has("is-shared-network", false).hasNext()); + assertTrue("Value of l3-network shouldn't be updated since is-external-network already exists", + g.V().has("aai-node-type", "l3-network").has("network-id", "l3-network3").has("network-name", "l3-network-name3").has("is-external-network", false).hasNext()); + //l-interface: is-port-mirrored, is-ip-unnumbered + assertTrue("Value of l-interface shouldn't be updated since is-port-mirrored already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface3").has("is-port-mirrored", false).hasNext()); + assertTrue("Value of ll-interface shouldn't be updated since is-ip-unnumbered already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface3").has("is-ip-unnumbered", false).hasNext()); + //vf-module: is-base-vf-module + assertTrue("Value of vf-module shouldn't be updated since is-base-vf-module already exists", + g.V().has("aai-node-type", "vf-module").has("vf-module-id", "vf-module3").has("is-base-vf-module", false).hasNext()); + //vlan: is-ip-unnumbered + assertTrue("Value of vlan shouldn't be updated since is-ip-unnumbered already exists", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan3").has("is-ip-unnumbered", false).hasNext()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateInMaintDefaultToFalseTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateInMaintDefaultToFalseTest.java new file mode 100644 index 0000000..9e7845f --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateInMaintDefaultToFalseTest.java @@ -0,0 +1,411 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import org.onap.aai.edges.EdgeIngestor; +import org.onap.aai.serialization.db.EdgeSerializer; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.janusgraph.core.schema.JanusGraphManagement; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.migration.Status; +import org.onap.aai.migration.v13.MigrateInMaintDefaultToFalse; +import org.onap.aai.migration.v13.MigrateInMaintDefaultToFalseTest.InMaintDefaultMigrator; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; + +public class MigrateInMaintDefaultToFalseTest extends + AAISetup { + protected static final String VNF_NODE_TYPE = "generic-vnf"; + protected static final String LINTERFACE_NODE_TYPE = "l-interface"; + protected static final String LAG_INTERFACE_NODE_TYPE = "lag-interface"; + protected static final String LOGICAL_LINK_NODE_TYPE = "logical-link"; + protected static final String PINTERFACE_NODE_TYPE = "p-interface"; + protected static final String VLAN_NODE_TYPE = "vlan"; + protected static final String VNFC_NODE_TYPE = "vnfc"; + protected static final String VSERVER_NODE_TYPE = "vserver"; + protected static final String PSERVER_NODE_TYPE = "pserver"; + protected static final String PNF_NODE_TYPE = "pnf"; + protected static final String NOS_SERVER_NODE_TYPE = "nos-server"; + + public static class InMaintDefaultMigrator extends MigrateInMaintDefaultToFalse { + public InMaintDefaultMigrator(TransactionalGraphEngine engine, LoaderFactory loaderFactory, EdgeIngestor edgeIngestor, EdgeSerializer edgeSerializer, SchemaVersions schemaVersions){ + super(engine, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + } + @Override + public Status getStatus() { + return Status.SUCCESS; + } + @Override + public Optional getAffectedNodeTypes() { + return Optional.of(new String[]{VNF_NODE_TYPE,LINTERFACE_NODE_TYPE,LAG_INTERFACE_NODE_TYPE,LOGICAL_LINK_NODE_TYPE,PINTERFACE_NODE_TYPE,VLAN_NODE_TYPE,VNFC_NODE_TYPE,VSERVER_NODE_TYPE,PSERVER_NODE_TYPE,PNF_NODE_TYPE,NOS_SERVER_NODE_TYPE}); + } + @Override + public String getMigrationName() { + return "MockInMaintDefaultMigrator"; + } + } + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private InMaintDefaultMigrator migration; + private GraphTraversalSource g; + + @Before + public void setup() throws Exception{ + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + //generic-vnf + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf0") + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "generic-vnf") + .property("vnf-id", "generic-vnf3") + .property("in-maint", false) + .next(); + //l-interface + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface0") + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "l-interface") + .property("interface-name", "l-interface3") + .property("in-maint", false) + .next(); + //lag-interface + g.addV().property("aai-node-type", "lag-interface") + .property("interface-name", "lag-interface0") + .next(); + g.addV().property("aai-node-type", "lag-interface") + .property("interface-name", "lag-interface1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "lag-interface") + .property("interface-name", "lag-interface2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "lag-interface") + .property("interface-name", "lag-interface3") + .property("in-maint", false) + .next(); + //logical-link + g.addV().property("aai-node-type", "logical-link") + .property("link-name", "logical-link0") + .next(); + g.addV().property("aai-node-type", "logical-link") + .property("link-name", "logical-link1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "logical-link") + .property("link-name", "logical-link2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "logical-link") + .property("link-name", "logical-link3") + .property("in-maint", false) + .next(); + //p-interface + g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface0") + .next(); + g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "p-interface") + .property("interface-name", "p-interface3") + .property("in-maint", false) + .next(); + //pnf + g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf0") + .next(); + g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "pnf") + .property("pnf-name", "pnf3") + .property("in-maint", false) + .next(); + //pserver + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver0") + .next(); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "pserver") + .property("pserver-id", "pserver3") + .property("in-maint", false) + .next(); + //vlan + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan0") + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "vlan") + .property("vlan-interface", "vlan3") + .property("in-maint", false) + .next(); + //vnfc + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc0") + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "vnfc") + .property("vnfc-name", "vnfc3") + .property("in-maint", false) + .next(); + //vserver + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver0") + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver2") + .property("in-maint", true) + .property("is-closed-loop-disabled", true) + .next(); + g.addV().property("aai-node-type", "vserver") + .property("vserver-id", "vserver3") + .property("in-maint", false) + .next(); + //nos-server + g.addV().property("aai-node-type", "nos-server") + .property("nos-server-id", "nos-server0") + .property("nos-server-name", "nos-server-name0") + .property("vendor", "vendor0") + .property("nos-server-selflink", "nos-server-selflink0") + .next(); + g.addV().property("aai-node-type", "nos-server") + .property("nos-server-id", "nos-server1") + .property("nos-server-name", "nos-server-name1") + .property("vendor", "vendor1") + .property("nos-server-selflink", "nos-server-selflink1") + .property("in-maint", "") + .next(); + g.addV().property("aai-node-type", "nos-server") + .property("nos-server-id", "nos-server2") + .property("nos-server-name", "nos-server-name2") + .property("vendor", "vendor2") + .property("nos-server-selflink", "nos-server-selflink2") + .property("in-maint", true) + .next(); + g.addV().property("aai-node-type", "nos-server") + .property("nos-server-id", "nos-server3") + .property("nos-server-name", "nos-server-name3") + .property("vendor", "vendor3") + .property("nos-server-selflink", "nos-server-selflink3") + .property("in-maint", false) + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new InMaintDefaultMigrator(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @Test + public void testMissingProperty(){ + assertTrue("Value of generic-vnf should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf0").has("in-maint", false).hasNext()); + assertTrue("Value of l-interface should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface0").has("in-maint", false).hasNext()); + assertTrue("Value of lag-interface should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "lag-interface").has("interface-name", "lag-interface0").has("in-maint", false).hasNext()); + assertTrue("Value of logical-link should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "logical-link").has("link-name", "logical-link0").has("in-maint", false).hasNext()); + assertTrue("Value of p-interface should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "p-interface").has("interface-name", "p-interface0").has("in-maint", false).hasNext()); + assertTrue("Value of pnf should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf0").has("in-maint", false).hasNext()); + assertTrue("Value of pserver should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver0").has("in-maint", false).hasNext()); + assertTrue("Value of vlan should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan0").has("in-maint", false).hasNext()); + assertTrue("Value of vnfc should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc0").has("in-maint", false).hasNext()); + assertTrue("Value of vserver should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver0").has("in-maint", false).hasNext()); + assertTrue("Value of nos-server should be updated since the property in-maint doesn't exist", + g.V().has("aai-node-type", "nos-server").has("nos-server-id", "nos-server0").has("in-maint", false).hasNext()); + } + + @Test + public void testEmptyValue() { + assertTrue("Value of generic-vnf should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf1").has("in-maint", false).hasNext()); + assertTrue("Value of l-interface should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface1").has("in-maint", false).hasNext()); + assertTrue("Value of lag-interface should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "lag-interface").has("interface-name", "lag-interface1").has("in-maint", false).hasNext()); + assertTrue("Value of logical-link should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "logical-link").has("link-name", "logical-link1").has("in-maint", false).hasNext()); + assertTrue("Value of p-interface should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "p-interface").has("interface-name", "p-interface1").has("in-maint", false).hasNext()); + assertTrue("Value of pnf should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf1").has("in-maint", false).hasNext()); + assertTrue("Value of pserver should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver1").has("in-maint", false).hasNext()); + assertTrue("Value of vlan should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan1").has("in-maint", false).hasNext()); + assertTrue("Value of vnfc should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc1").has("in-maint", false).hasNext()); + assertTrue("Value of vserver should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver1").has("in-maint", false).hasNext()); + assertTrue("Value of nos-server should be updated since the value for in-maint is an empty string", + g.V().has("aai-node-type", "nos-server").has("nos-server-id", "nos-server1").has("in-maint", false).hasNext()); + } + + @Test + public void testExistingTrueValues() { + assertTrue("Value of generic-vnf shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf2").has("in-maint", true).hasNext()); + assertTrue("Value of l-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface2").has("in-maint", true).hasNext()); + assertTrue("Value of lag-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "lag-interface").has("interface-name", "lag-interface2").has("in-maint", true).hasNext()); + assertTrue("Value of logical-link shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "logical-link").has("link-name", "logical-link2").has("in-maint", true).hasNext()); + assertTrue("Value of p-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "p-interface").has("interface-name", "p-interface2").has("in-maint", true).hasNext()); + assertTrue("Value of pnf shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf2").has("in-maint", true).hasNext()); + assertTrue("Value of pserver shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver2").has("in-maint", true).hasNext()); + assertTrue("Value of vlan shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan2").has("in-maint", true).hasNext()); + assertTrue("Value of vnfc shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc2").has("in-maint", true).hasNext()); + assertTrue("Value of vserver shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver2").has("in-maint", true).hasNext()); + assertTrue("Value of nos-server shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "nos-server").has("nos-server-id", "nos-server2").has("in-maint", true).hasNext()); + } + + @Test + public void testExistingFalseValues() { + assertTrue("Value of generic-vnf shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "generic-vnf3").has("in-maint", false).hasNext()); + assertTrue("Value of l-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "l-interface").has("interface-name", "l-interface3").has("in-maint", false).hasNext()); + assertTrue("Value of lag-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "lag-interface").has("interface-name", "lag-interface3").has("in-maint", false).hasNext()); + assertTrue("Value of logical-link shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "logical-link").has("link-name", "logical-link3").has("in-maint", false).hasNext()); + assertTrue("Value of p-interface shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "p-interface").has("interface-name", "p-interface3").has("in-maint", false).hasNext()); + assertTrue("Value of pnf shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnf3").has("in-maint", false).hasNext()); + assertTrue("Value of pserver shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "pserver").has("pserver-id", "pserver3").has("in-maint", false).hasNext()); + assertTrue("Value of vlan shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vlan").has("vlan-interface", "vlan3").has("in-maint", false).hasNext()); + assertTrue("Value of vnfc shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc3").has("in-maint", false).hasNext()); + assertTrue("Value of vserver shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "vserver").has("vserver-id", "vserver3").has("in-maint", false).hasNext()); + assertTrue("Value of nos-server shouldn't be updated since in-maint already exists", + g.V().has("aai-node-type", "nos-server").has("nos-server-id", "nos-server3").has("in-maint", false).hasNext()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelInvariantIdTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelInvariantIdTest.java new file mode 100644 index 0000000..3610fac --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelInvariantIdTest.java @@ -0,0 +1,108 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class MigrateInstanceGroupModelInvariantIdTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateInstanceGroupModelInvariantId migration; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex instancegroup1 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-1") + .property("description","instance-description-1").property("instanceGroupType","instance-type-1") + .property("model-invariant-id", "instance-invariant-id-1").next(); + + Vertex instancegroup2 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-2") + .property("description","instance-description-2").property("instanceGroupType","instance-type-1") + .property("model-invariant-id-local", "instance-invariant-id-2").next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateInstanceGroupModelInvariantId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testIdsUpdated() throws Exception { + assertEquals(true, + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").has("model-invariant-id-local").next().property("model-invariant-id-local").isPresent()); + assertEquals("model-invariant-id renamed to model-invariant-id-local for instance-group", "instance-invariant-id-1", + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").next().value("model-invariant-id-local").toString()); + } + + @Test + public void testIdsNotUpdated() throws Exception { + assertEquals("model-invariant-id-local remains the same for instance-group", "instance-invariant-id-2", + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-2").next().value("model-invariant-id-local").toString()); + } +} diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelVersionIdTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelVersionIdTest.java new file mode 100644 index 0000000..346d76a --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupModelVersionIdTest.java @@ -0,0 +1,111 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class MigrateInstanceGroupModelVersionIdTest extends AAISetup { + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private static Loader loader; + private static TransactionalGraphEngine dbEngine; + private static JanusGraph graph; + private static MigrateInstanceGroupModelVersionId migration; + private static JanusGraphTransaction tx; + private static GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex instancegroup1 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-1") + .property("description","instance-description-1").property("instanceGroupType","instance-type-1") + .property("model-version-id", "instance-version-id-1").next(); + + Vertex instancegroup2 = g.addV().property("aai-node-type", "instance-group").property("id", "instance-id-2") + .property("description","instance-description-2").property("instanceGroupType","instance-type-1") + .property("model-version-id-local", "instance-version-id-2").next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateInstanceGroupModelVersionId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @AfterClass + public static void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testIdsUpdated() throws Exception { + assertEquals(true, + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").has("model-version-id-local").next().property("model-version-id-local").isPresent()); + assertEquals("model-version-id renamed to model-version-id-local for instance-group", "instance-version-id-1", + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-1").next().value("model-version-id-local").toString()); + } + + @Test + public void testIdsNotUpdated() throws Exception { + assertEquals("model-version-id-local remains the same for instance-group", "instance-version-id-2", + g.V().has("aai-node-type", "instance-group").has("id", "instance-id-2").next().value("model-version-id-local").toString()); + } +} diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupSubTypeTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupSubTypeTest.java new file mode 100644 index 0000000..45a6cb8 --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupSubTypeTest.java @@ -0,0 +1,112 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + + +public class MigrateInstanceGroupSubTypeTest extends AAISetup{ + + private static final String SUB_TYPE_VALUE = "SubTypeValue"; + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateInstanceGroupSubType migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + Vertex instanceGroup; + Vertex instanceGroupWithoutTSubType; + + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + instanceGroup = g.addV().property("aai-node-type", MigrateInstanceGroupSubType.INSTANCE_GROUP_NODE_TYPE) + .property( MigrateInstanceGroupSubType.SUB_TYPE_PROPERTY, SUB_TYPE_VALUE) + .next(); + + instanceGroupWithoutTSubType = g.addV().property("aai-node-type", MigrateInstanceGroupSubType.INSTANCE_GROUP_NODE_TYPE) + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new MigrateInstanceGroupSubType(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + } + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + + /*** + * checks if the type/subtype property were renamed + */ + + @Test + public void confirmTypeAndSubTypeWereRenamed() { + migration.run(); + + //instance group with sub-type + assertEquals(SUB_TYPE_VALUE, instanceGroup.property(MigrateInstanceGroupSubType.INSTANCE_GROUP_ROLE_PROPERTY).value()); + assertFalse(instanceGroup.property(MigrateInstanceGroupSubType.SUB_TYPE_PROPERTY).isPresent()); + + //instance group without subtype + assertFalse(instanceGroupWithoutTSubType.property(MigrateInstanceGroupSubType.INSTANCE_GROUP_ROLE_PROPERTY).isPresent()); + assertFalse(instanceGroupWithoutTSubType.property(MigrateInstanceGroupSubType.SUB_TYPE_PROPERTY).isPresent()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupTypeTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupTypeTest.java new file mode 100644 index 0000000..210b905 --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateInstanceGroupTypeTest.java @@ -0,0 +1,112 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + + +public class MigrateInstanceGroupTypeTest extends AAISetup{ + + private static final String TYPE_VALUE = "TypeValue"; + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateInstanceGroupType migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + Vertex instanceGroup; + Vertex instanceGroupWithoutType; + + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + instanceGroup = g.addV().property("aai-node-type", MigrateInstanceGroupType.INSTANCE_GROUP_NODE_TYPE) + .property( MigrateInstanceGroupType.TYPE_PROPERTY, TYPE_VALUE) + .next(); + + instanceGroupWithoutType = g.addV().property("aai-node-type", MigrateInstanceGroupType.INSTANCE_GROUP_NODE_TYPE) + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new MigrateInstanceGroupType(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + } + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + + /*** + * checks if the type/subtype property were renamed + */ + + @Test + public void confirmTypeAndSubTypeWereRenamed() { + migration.run(); + + //instance group with type + assertEquals(TYPE_VALUE, instanceGroup.property(MigrateInstanceGroupType.INSTANCE_GROUP_TYPE_PROPERTY).value()); + assertFalse(instanceGroup.property(MigrateInstanceGroupType.TYPE_PROPERTY).isPresent()); + + //instance group without type + assertFalse(instanceGroupWithoutType.property(MigrateInstanceGroupType.INSTANCE_GROUP_TYPE_PROPERTY).isPresent()); + assertFalse(instanceGroupWithoutType.property(MigrateInstanceGroupType.TYPE_PROPERTY).isPresent()); + } +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateModelVerTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateModelVerTest.java new file mode 100644 index 0000000..00db1fa --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateModelVerTest.java @@ -0,0 +1,487 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.janusgraph.core.schema.JanusGraphManagement; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +public class MigrateModelVerTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + private MigrateModelVer migration; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + JanusGraphManagement janusgraphManagement = graph.openManagement(); + tx = graph.newTransaction(); + g = graph.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine(queryStyle, type, loader); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + Mockito.doReturn(janusgraphManagement).when(adminSpy).getManagementSystem(); + + + // Add model1/model-ver1 -- invalid model/model-ver + Vertex model1 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-1").property("model-type", "widget").next(); + Vertex modelVer1 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-1") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-1/model-vers/model-ver/model-version-id-1") + .property("model-name", "connector").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model1, modelVer1); + //connector + Vertex connector1= g.addV().property("aai-node-type", "connector").property("resource-instance-id", "connector1") + .property("model-invariant-id-local", "model-invariant-id-1").property("model-version-id-local", "model-version-id-1").next(); + Vertex connector2= g.addV().property("aai-node-type", "connector").property("resource-instance-id", "connector2") + .property("model-invariant-id-local", "model-invariant-id-x").property("model-version-id-local", "model-version-id-x").next(); + Vertex connector3= g.addV().property("aai-node-type", "connector").property("resource-instance-id", "connector3") + .property("model-invariant-id-local", "model-invariant-id-1").property("model-version-id-local", "model-version-id-1").next(); + edgeSerializer.addPrivateEdge(traversal, connector3, modelVer1, null); + + + // Add model1/model-ver1 -- invalid model/model-ver + Vertex model2 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-2").property("model-type", "widget").next(); + Vertex modelVer2 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-2") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-2/model-vers/model-ver/model-version-id-2") + .property("model-name", "service-instance").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model2, modelVer2); + //serivce-instance + Vertex serviceInstance1= g.addV().property("aai-node-type", "service-instance").property("service-instance-id", "serviceinstance1") + .property("model-invariant-id-local", "model-invariant-id-2").property("model-version-id-local", "model-version-id-2").next(); + Vertex serviceInstance2= g.addV().property("aai-node-type", "service-instance").property("service-instance-id", "serviceinstance2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex serviceInstance3= g.addV().property("aai-node-type", "service-instance").property("service-instance-id", "serviceinstance3") + .property("model-invariant-id-local", "model-invariant-id-2").property("model-version-id-local", "model-version-id-2").next(); + edgeSerializer.addPrivateEdge(traversal, serviceInstance3, modelVer2, null); + + // Add model3/model-ver3 + Vertex model3 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-3").property("model-type", "widget").next(); + Vertex modelVer3 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-3") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-3/model-vers/model-ver/model-version-id-3") + .property("model-name", "pnf").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model3, modelVer3); + //pnf + Vertex pnfName1= g.addV().property("aai-node-type", "pnf").property("pnf-name", "pnfName1") + .property("model-invariant-id-local", "model-invariant-id-3").property("model-version-id-local", "model-version-id-3").next(); + Vertex pnfName2= g.addV().property("aai-node-type", "pnf").property("pnf-name", "pnfName2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex pnfName3= g.addV().property("aai-node-type", "pnf").property("pnf-name", "pnfName3") + .property("model-invariant-id-local", "model-invariant-id-3").property("model-version-id-local", "model-version-id-3").next(); + edgeSerializer.addPrivateEdge(traversal, pnfName3, modelVer3, null); + + // Add model4/model-ver4 + Vertex model4 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-4").property("model-type", "widget").next(); + Vertex modelVer4 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-4") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-4/model-vers/model-ver/model-version-id-4") + .property("model-name", "logical-link").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model4, modelVer4); + //logical-link + Vertex linkName1= g.addV().property("aai-node-type", "logical-link").property("link-name", "linkName1") + .property("model-invariant-id-local", "model-invariant-id-4").property("model-version-id-local", "model-version-id-4").next(); + Vertex linkName2= g.addV().property("aai-node-type", "logical-link").property("link-name", "linkName2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex linkName3= g.addV().property("aai-node-type", "logical-link").property("link-name", "linkName3") + .property("model-invariant-id-local", "model-invariant-id-4").property("model-version-id-local", "model-version-id-4").next(); + edgeSerializer.addPrivateEdge(traversal, linkName3, modelVer4, null); + + + // Add model5/model-ver5 + Vertex model5 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-5").property("model-type", "widget").next(); + Vertex modelVer5 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-5") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-5/model-vers/model-ver/model-version-id-5") + .property("model-name", "vnfc").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model5, modelVer5); + //vnfc + Vertex vnfc1= g.addV().property("aai-node-type", "vnfc").property("vnfc-name", "vnfc1") + .property("model-invariant-id-local", "model-invariant-id-5").property("model-version-id-local", "model-version-id-5").next(); + Vertex vnfc2= g.addV().property("aai-node-type", "vnfc").property("vnfc-name", "vnfc2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex vnfc3= g.addV().property("aai-node-type", "vnfc").property("vnfc-name", "vnfc3") + .property("model-invariant-id-local", "model-invariant-id-5").property("model-version-id-local", "model-version-id-5").next(); + edgeSerializer.addPrivateEdge(traversal, vnfc3, modelVer5, null); + + // Add model6/model-ver6 + Vertex model6 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-6").property("model-type", "widget").next(); + Vertex modelVer6 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-6") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-6/model-vers/model-ver/model-version-id-6") + .property("model-name", "vnf").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model6, modelVer6); + //generic-vnf + Vertex vnf1= g.addV().property("aai-node-type", "generic-vnf").property("vnf-id", "vnf1") + .property("model-invariant-id-local", "model-invariant-id-6").property("model-version-id-local", "model-version-id-6").next(); + Vertex vnf2= g.addV().property("aai-node-type", "generic-vnf").property("vnf-id", "vnf2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex vnf3= g.addV().property("aai-node-type", "generic-vnf").property("vnf-id", "vnf3") + .property("model-invariant-id-local", "model-invariant-id-6").property("model-version-id-local", "model-version-id-6").next(); + edgeSerializer.addPrivateEdge(traversal, vnf3, modelVer6, null); + + // Add model7/model-ver7 + Vertex model7 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-7").property("model-type", "widget").next(); + Vertex modelVer7 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-7") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-7/model-vers/model-ver/model-version-id-7") + .property("model-name", "configuration").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model7, modelVer7); + //configuration + Vertex configuration1= g.addV().property("aai-node-type", "configuration").property("configuration-id", "configuration1") + .property("model-invariant-id-local", "model-invariant-id-7").property("model-version-id-local", "model-version-id-7").next(); + Vertex configuration2= g.addV().property("aai-node-type", "configuration").property("configuration-id", "configuration2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex configuration3= g.addV().property("aai-node-type", "configuration").property("configuration-id", "configuration3") + .property("model-invariant-id-local", "model-invariant-id-7").property("model-version-id-local", "model-version-id-7").next(); + edgeSerializer.addPrivateEdge(traversal, configuration3, modelVer7, null); + + // Add model8/model-ver8 + Vertex model8 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-8").property("model-type", "widget").next(); + Vertex modelVer8 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-8") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-8/model-vers/model-ver/model-version-id-8") + .property("model-name", "l3-network").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model8, modelVer8); + //l3-network + Vertex l3Network1= g.addV().property("aai-node-type", "l3-network").property("network-id", "l3Network1") + .property("model-invariant-id-local", "model-invariant-id-8").property("model-version-id-local", "model-version-id-8").next(); + Vertex l3Network2= g.addV().property("aai-node-type", "l3-network").property("network-id", "l3Network2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex l3Network3= g.addV().property("aai-node-type", "l3-network").property("network-id", "l3Network3") + .property("model-invariant-id-local", "model-invariant-id-8").property("model-version-id-local", "model-version-id-8").next(); + edgeSerializer.addPrivateEdge(traversal, l3Network3, modelVer8, null); + + // Add model9/model-ver9 + Vertex model9 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-9").property("model-type", "widget").next(); + Vertex modelVer9 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-9") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-9/model-vers/model-ver/model-version-id-9") + .property("model-name", "vf-module").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model9, modelVer9); + //vf-module + Vertex vfModule1= g.addV().property("aai-node-type", "vf-module").property("vf-module-id", "vfModule1") + .property("model-invariant-id-local", "model-invariant-id-9").property("model-version-id-local", "model-version-id-9").next(); + Vertex vfModule2= g.addV().property("aai-node-type", "vf-module").property("vf-module-id", "vfModule2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex vfModule3= g.addV().property("aai-node-type", "vf-module").property("vf-module-id", "vfModule3") + .property("model-invariant-id-local", "model-invariant-id-9").property("model-version-id-local", "model-version-id-9").next(); + edgeSerializer.addPrivateEdge(traversal, vfModule3, modelVer9, null); + + // Add model10/model-ver10 + Vertex model10 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-10").property("model-type", "widget").next(); + Vertex modelVer10 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-10") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-10/model-vers/model-ver/model-version-id-10") + .property("model-name", "collection").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model10, modelVer10); + //collection + Vertex collection1= g.addV().property("aai-node-type", "collection").property("collection-id", "collection1") + .property("model-invariant-id-local", "model-invariant-id-10").property("model-version-id-local", "model-version-id-10").next(); + Vertex collection2= g.addV().property("aai-node-type", "collection").property("collection-id", "collection2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex collection3= g.addV().property("aai-node-type", "collection").property("collection-id", "collection3") + .property("model-invariant-id-local", "model-invariant-id-10").property("model-version-id-local", "model-version-id-10").next(); + edgeSerializer.addPrivateEdge(traversal, collection3, modelVer10, null); + + // Add model11/model-ver11 + Vertex model11 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-11").property("model-type", "widget").next(); + Vertex modelVer11 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-11") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-11/model-vers/model-ver/model-version-id-11") + .property("model-name", "instance-group").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model11, modelVer11); + //instance-group + Vertex instanceGroup1= g.addV().property("aai-node-type", "instance-group").property("id", "instanceGroup1") + .property("model-invariant-id-local", "model-invariant-id-11").property("model-version-id-local", "model-version-id-11").next(); + Vertex instanceGroup2= g.addV().property("aai-node-type", "instance-group").property("id", "instanceGroup2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex instanceGroup3= g.addV().property("aai-node-type", "instance-group").property("id", "instanceGroup3") + .property("model-invariant-id-local", "model-invariant-id-11").property("model-version-id-local", "model-version-id-11").next(); + edgeSerializer.addPrivateEdge(traversal, instanceGroup3, modelVer11, null); + + // Add model12/model-ver12 + Vertex model12 = g.addV().property("aai-node-type", "model") + .property("model-invariant-id", "model-invariant-id-12").property("model-type", "widget").next(); + Vertex modelVer12 = g.addV().property("aai-node-type", "model-ver").property("model-version-id", "model-version-id-12") + .property("aai-uri", "/service-design-and-creation/models/model/model-invariant-id-12/model-vers/model-ver/model-version-id-12") + .property("model-name", "allotted-resource").property("model-version", "v1.0").next(); + edgeSerializer.addTreeEdge(g, model12, modelVer12); + //allotted-resource + Vertex allottedResource1= g.addV().property("aai-node-type", "allotted-resource").property("id", "allottedResource1") + .property("model-invariant-id-local", "model-invariant-id-12").property("model-version-id-local", "model-version-id-12").next(); + Vertex allottedResource2= g.addV().property("aai-node-type", "allotted-resource").property("id", "allottedResource2") + .property("model-invariant-id-local", "model-invariant-id-y").property("model-version-id-local", "model-version-id-y").next(); + Vertex allottedResource3= g.addV().property("aai-node-type", "allotted-resource").property("id", "allottedResource3") + .property("model-invariant-id-local", "model-invariant-id-12").property("model-version-id-local", "model-version-id-12").next(); + edgeSerializer.addPrivateEdge(traversal, allottedResource3, modelVer12, null); + + + migration = new MigrateModelVer(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + @Test + public void checkEdgeCreatedForConnector() { + assertEquals(true, + g.V().has("aai-node-type", "connector").has("resource-instance-id", "connector1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-1").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "connector").has("resource-instance-id", "connector2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-1").hasNext()); + + assertEquals("Edge exists to 2 connectors", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-1") + .in().count().next()); + } + + + @Test + public void checkEdgeCreatedForSerivceInstance() { + assertEquals(true, + g.V().has("aai-node-type", "service-instance").has("service-instance-id", "serviceinstance1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-2").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "service-instance").has("service-instance-id", "serviceinstance2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-2").hasNext()); + + assertEquals("Edge exists to only 2 service-instances", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-2") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForPnf() { + assertEquals(true, + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnfName1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-3").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "pnf").has("pnf-name", "pnfName2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-3").hasNext()); + + assertEquals("Edge exists to only 2 pnfs", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-3") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForLogicalLink() { + assertEquals(true, + g.V().has("aai-node-type", "logical-link").has("link-name", "linkName1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-4").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "logical-link").has("link-name", "linkName2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-4").hasNext()); + + assertEquals("Edge exists to only 2 logical-link", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-4") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForVnfc() { + assertEquals(true, + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-5").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "vnfc").has("vnfc-name", "vnfc2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-5").hasNext()); + + assertEquals("Edge exists to only 2 logical-link", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-5") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForGenericVnf() { + assertEquals(true, + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "vnf1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-6").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "generic-vnf").has("vnf-id", "vnf2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-6").hasNext()); + + assertEquals("Edge exists to only 2 generic-vnfs", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-6") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForConfiguration() { + assertEquals(true, + g.V().has("aai-node-type", "configuration").has("configuration-id", "configuration1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-7").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "configuration").has("configuration-id", "configuration2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-7").hasNext()); + + assertEquals("Edge exists to only 2 configurations", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-7") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForl3Network() { + assertEquals(true, + g.V().has("aai-node-type", "l3-network").has("network-id", "l3Network1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-8").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "l3-network").has("network-id", "l3Network2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-8").hasNext()); + + assertEquals("Edge exists to only 2 l3-networks", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-8") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForVfModule() { + assertEquals(true, + g.V().has("aai-node-type", "vf-module").has("vf-module-id", "vfModule1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-9").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "vf-module").has(" vf-module-id", "vfModule2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-9").hasNext()); + + assertEquals("Edge exists to only 2 vf-modules", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-9") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForCollection() { + assertEquals(true, + g.V().has("aai-node-type", "collection").has("collection-id", "collection1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-10").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "collection").has("collection-id", "collection2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-10").hasNext()); + + assertEquals("Edge exists to only 2 collections", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-10") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForInstanceGroup() { + assertEquals(true, + g.V().has("aai-node-type", "instance-group").has("id", "instanceGroup1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-11").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "instance-group").has("collection-id", "instanceGroup2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-11").hasNext()); + + assertEquals("Edge exists to only 2 instance-group2", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-11") + .in().count().next()); + } + + @Test + public void checkEdgeCreatedForAllottedResource() { + assertEquals(true, + g.V().has("aai-node-type", "allotted-resource").has("id", "allottedResource1") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-12").hasNext()); + + assertEquals( "Edge not created", false, + g.V().has("aai-node-type", "allotted-resource").has("id", "allottedResource2") + .out() + .has("aai-node-type", "model-ver").has("model-version-id","model-version-id-12").hasNext()); + + assertEquals("Edge exists to only 2 allotted-resource", new Long(2L), + g.V().has("aai-node-type", "model-ver").has("model-version-id", "model-version-id-12") + .in().count().next()); + } + +} diff --git a/src/test/java/org/onap/aai/migration/v13/MigratePServerAndPnfEquipTypeTest.java b/src/test/java/org/onap/aai/migration/v13/MigratePServerAndPnfEquipTypeTest.java new file mode 100644 index 0000000..adce73e --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigratePServerAndPnfEquipTypeTest.java @@ -0,0 +1,138 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + + +public class MigratePServerAndPnfEquipTypeTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigratePserverAndPnfEquipType migration; + private GraphTraversalSource g; + private JanusGraphTransaction tx; + Vertex pserver1; + Vertex pserver2; + Vertex pnf1; + Vertex pserver3; + Vertex pnf2; + Vertex pnf22; + + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + pserver1 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PSERVER_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "Server") + .next(); + + pserver2 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PSERVER_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "server") + .next(); + + pnf1 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PNF_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "Switch") + .next(); + pnf22 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PNF_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "switch") + .next(); + + pserver3 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PSERVER_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "server1") + .next(); + + pnf2 = g.addV().property("aai-node-type", MigratePserverAndPnfEquipType.PNF_NODE_TYPE) + .property( MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY, "Switch1") + .next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + GraphTraversalSource traversal = g; + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + migration = new MigratePserverAndPnfEquipType(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.rollback(); + graph.close(); + } + + + /*** + * checks if the Equip Type value was changed + */ + + @Test + public void confirmEquipTypeChanged() { + + assertEquals("SERVER",pserver1.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + assertEquals("SERVER",pserver2.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + assertEquals("SWITCH",pnf1.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + assertEquals("SWITCH",pnf22.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + } + + @Test + public void verifyEquipTypeIsNotChanged() { + assertEquals("server1",pserver3.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + assertEquals("Switch1",pnf2.property(MigratePserverAndPnfEquipType.EQUIP_TYPE_PROPERTY).value()); + } + + + + + +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelInvariantIdTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelInvariantIdTest.java new file mode 100644 index 0000000..d546eea --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelInvariantIdTest.java @@ -0,0 +1,110 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +public class MigrateVnfcModelInvariantIdTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateVnfcModelInvariantId migration; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex vnfc1 = g.addV().property("aai-node-type", "vnfc").property("model-invariant-id", "vnfc-invariant-id-1") + .property("vnfcName", "vnfc-name-1").property("nfcNamingCode", "naming-code-1") + .property("nfcFunction", "function-1") + .property("model-version-id", "vnfc-variant-id-1").next(); + + Vertex vnfc2 = g.addV().property("aai-node-type", "vnfc").property("model-invariant-id-local", "vnfc-invariant-id-2") + .property("vnfcName", "vnfc-name-2").property("nfcNamingCode", "naming-code-2") + .property("nfcFunction", "function-2") + .property("model-version-id-local", "vnfc-version-id-2").next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateVnfcModelInvariantId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testIdsUpdated() throws Exception { + assertEquals(true, + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").has("model-invariant-id-local").next().property("model-invariant-id-local").isPresent()); + assertEquals("model-invariant-id renamed to model-invariant-id-local for vnfc", "vnfc-invariant-id-1", + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").next().value("model-invariant-id-local").toString()); + } + + @Test + public void testIdsNotUpdated() throws Exception { + assertEquals("model-invariant-id-local should not be renamed for vnfc", "vnfc-invariant-id-2", + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-2").next().value("model-invariant-id-local").toString()); + } +} diff --git a/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelVersionIdTest.java b/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelVersionIdTest.java new file mode 100644 index 0000000..b74756e --- /dev/null +++ b/src/test/java/org/onap/aai/migration/v13/MigrateVnfcModelVersionIdTest.java @@ -0,0 +1,112 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.migration.v13; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.junit.*; +import org.onap.aai.AAISetup; +import org.onap.aai.dbmap.DBConnectionType; +import org.onap.aai.introspection.Loader; +import org.onap.aai.introspection.LoaderFactory; +import org.onap.aai.introspection.ModelType; +import org.onap.aai.setup.SchemaVersions; +import org.onap.aai.setup.SchemaVersion; +import org.onap.aai.serialization.engines.QueryStyle; +import org.onap.aai.serialization.engines.JanusGraphDBEngine; +import org.onap.aai.serialization.engines.TransactionalGraphEngine; + +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphTransaction; + +public class MigrateVnfcModelVersionIdTest extends AAISetup{ + + private final static ModelType introspectorFactoryType = ModelType.MOXY; + private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL; + private final static DBConnectionType type = DBConnectionType.REALTIME; + + private Loader loader; + private TransactionalGraphEngine dbEngine; + private JanusGraph graph; + private MigrateVnfcModelVersionId migration; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + @Before + public void setUp() throws Exception { + graph = JanusGraphFactory.build().set("storage.backend","inmemory").open(); + tx = graph.newTransaction(); + g = tx.traversal(); + loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion()); + dbEngine = new JanusGraphDBEngine( + queryStyle, + type, + loader); + + Vertex vnfc1 = g.addV().property("aai-node-type", "vnfc").property("model-version-id", "vnfc-version-id-1") + .property("vnfcName", "vnfc-name-1").property("nfcNamingCode", "naming-code-1") + .property("nfcFunction", "function-1") + .property("model-version-id", "vnfc-version-id-1").next(); + + Vertex vnfc2 = g.addV().property("aai-node-type", "vnfc").property("model-version-id-local", "vnfc-version-id-1") + .property("vnfcName", "vnfc-name-2").property("nfcNamingCode", "naming-code-1") + .property("nfcFunction", "function-2") + .property("model-version-id-local", "vnfc-version-id-2").next(); + + TransactionalGraphEngine spy = spy(dbEngine); + TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin()); + + GraphTraversalSource traversal = g; + GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance())); + when (spy.tx()).thenReturn(tx); + when(spy.asAdmin()).thenReturn(adminSpy); + when(adminSpy.getTraversalSource()).thenReturn(traversal); + when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly); + + migration = new MigrateVnfcModelVersionId(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions); + migration.run(); + } + + @After + public void cleanUp() { + tx.tx().rollback(); + graph.close(); + } + + @Test + public void testIdsUpdated() throws Exception { + assertEquals(true, + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").has("model-version-id-local").next().property("model-version-id-local").isPresent()); + assertEquals("model-version-id renamed to model-invariant-id-local for vnfc", "vnfc-version-id-1", + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-1").next().value("model-version-id-local").toString()); + } + + @Test + public void testIdsNotUpdated() throws Exception { + assertEquals("model-version-id-local should not be renamed for vnfc", "vnfc-version-id-2", + g.V().has("aai-node-type", "vnfc").has("vnfcName", "vnfc-name-2").next().value("model-version-id-local").toString()); + } +} diff --git a/src/test/java/org/onap/aai/schema/db/ManageSchemaTest.java b/src/test/java/org/onap/aai/schema/db/ManageSchemaTest.java new file mode 100644 index 0000000..ddaad21 --- /dev/null +++ b/src/test/java/org/onap/aai/schema/db/ManageSchemaTest.java @@ -0,0 +1,111 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.schema.db; + +import org.codehaus.jackson.JsonParseException; +import org.codehaus.jackson.map.JsonMappingException; +import org.codehaus.jackson.map.ObjectMapper; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.schema.JanusGraphManagement; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.onap.aai.AAISetup; +import org.onap.aai.db.schema.DBIndex; +import org.onap.aai.db.schema.ManageJanusGraphSchema; + +import java.io.IOException; +import java.util.Set; + +@Ignore("not ready yet") +public class ManageSchemaTest extends AAISetup { + + private JanusGraph graph = null; + + @Before + public void beforeTest() { + graph = JanusGraphFactory.open("bundleconfig-local/etc/appprops/aaiconfig.properties"); + } + + /* + @Test + public void populateEmptyGraph() { + ManageJanusGraphSchema schema = new ManageJanusGraphSchema(graph); + schema.buildSchema(); + } + + @Test + public void modifyIndex() { + ManageJanusGraphSchema schema = new ManageJanusGraphSchema(graph); + schema.buildSchema(); + Vertex v = graph.addVertex(); + v.setProperty("aai-node-type", "pserver"); + v.setProperty("hostname", "test1"); + v.setProperty("internet-topology", "test2"); + graph.commit(); + DBIndex index = new DBIndex(); + index.setName("internet-topology"); + index.setUnique(false); + schema.updateIndex(index); + + } + */ + @Test + public void closeRunningInstances() { + + JanusGraphManagement mgmt = graph.openManagement(); + Set instances = mgmt.getOpenInstances(); + + for (String instance : instances) { + + if (!instance.contains("(current)")) { + mgmt.forceCloseInstance(instance); + } + } + mgmt.commit(); + + graph.close(); + + } + @Test + public void addNewIndex() throws JsonParseException, JsonMappingException, IOException { + ObjectMapper mapper = new ObjectMapper(); + String content = " {\r\n" + + " \"name\" : \"equipment-name\",\r\n" + + " \"unique\" : false,\r\n" + + " \"properties\" : [ {\r\n" + + " \"name\" : \"equipment-name\",\r\n" + + " \"cardinality\" : \"SINGLE\",\r\n" + + " \"typeClass\" : \"java.lang.String\"\r\n" + + " } ]\r\n" + + " }"; + DBIndex index = mapper.readValue(content, DBIndex.class); + ManageJanusGraphSchema schema = new ManageJanusGraphSchema(graph, auditorFactory, schemaVersions); + JanusGraphManagement mgmt = graph.openManagement(); + Set instances = mgmt.getOpenInstances(); + System.out.println(instances); + schema.updateIndex(index); + + graph.close(); + + } + +} diff --git a/src/test/java/org/onap/aai/util/SendDeleteMigrationNotificationsTest.java b/src/test/java/org/onap/aai/util/SendDeleteMigrationNotificationsTest.java new file mode 100644 index 0000000..5df4a75 --- /dev/null +++ b/src/test/java/org/onap/aai/util/SendDeleteMigrationNotificationsTest.java @@ -0,0 +1,166 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.util; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.junit.runners.MethodSorters; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.introspection.Introspector; +import org.onap.aai.migration.EventAction; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SendDeleteMigrationNotificationsTest extends AAISetup { + + private final static String FILE = "./test.txt"; + + private static AtomicBoolean graphCreated = new AtomicBoolean(false); + + private JanusGraph graph; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + private static final String REALTIME_CONFIG = "./src/main/resources/etc/appprops/janusgraph-realtime.properties"; + + @Before + public void setUp() throws Exception { + System.setProperty("realtime.db.config", REALTIME_CONFIG); + AAIGraph.getInstance(); + graph = AAIGraph.getInstance().getGraph(); + tx = graph.newTransaction(); + g = tx.traversal(); + + createFile(); + } + + public void createFile() throws AAIException, IOException { + + /*String str = "pserver#@#/cloud-infrastructure/pservers/pserver/mtunj102sd9#@#{\"hostname\":\"mtunj102sd9\",\"ptnii-equip-name\":\"mtunj102sd9\",\"equip-type\":\"SERVER\",\"equip-vendor\":\"HP\",\"equip-model\":\"DL380p9-nd\",\"fqdn\":\"mtunjrsv102.mtunj.sbcglobal.net\",\"ipv4-oam-address\":\"10.64.220.7\",\"resource-version\":\"1523039038578\",\"purpose\":\"LCPA-3.0\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"complex\",\"relationship-label\":\"org.onap.relationships.inventory.LocatedIn\",\"related-link\":\"/aai/v14/cloud-infrastructure/complexes/complex/MDTWNJ21A5\",\"relationship-data\":[{\"relationship-key\":\"complex.physical-location-id\",\"relationship-value\":\"MDTWNJ21A5\"}]}]}}"; + Files.write(Paths.get(FILE), str.getBytes()); + graphCreated.compareAndSet(false, true); + */ + if(!graphCreated.get()){ + Vertex pserver1 = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1") + .property(AAIProperties.RESOURCE_VERSION, "333") + .next(); + + Vertex pserver2 = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2") + .property(AAIProperties.RESOURCE_VERSION, "334") + .next(); + + Vertex pserver3 = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3") + .property(AAIProperties.RESOURCE_VERSION, "335") + .next(); + + Vertex pserver4 = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4") + .property(AAIProperties.RESOURCE_VERSION, "336") + .next(); + + tx.commit(); + + try{ + Files.createFile(Paths.get(FILE)); + }catch(Exception e) { + e.printStackTrace(); + } + String finalStr = ""; + finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-1\",\"resource-version\":\"333\"}" + "\n"; + Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND); + finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-2\",\"resource-version\":\"334\"}" + "\n"; + Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND); + finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-3\",\"resource-version\":\"335\"}" + "\n"; + Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND); + finalStr = "pserver" + "#@#" + "/cloud-infrastructure/pservers/pserver/"+SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4" + "#@#" + "{\"hostname\":\""+ SendDeleteMigrationNotifications.class.getSimpleName()+"-pserver-4\",\"resource-version\":\"336\"}" + "\n"; + Files.write(Paths.get(FILE), finalStr.getBytes(),StandardOpenOption.APPEND); + graphCreated.compareAndSet(false, true); + } + } + @AfterClass + public static void cleanUp() throws IOException { + Files.delete(Paths.get(FILE)); + } + + @After + public void tearDown() throws IOException { + if (tx.isOpen()) { + tx.tx().rollback(); + } + } + + @Test + public void processEverything() throws Exception { + SendDeleteMigrationNotifications s = spy(new SendDeleteMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 0, "test", EventAction.DELETE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + assertEquals("1 events are created ", 4, s.notificationHelper.getNotifications().getEvents().size()); + + } + + @Test + public void processEverythingBatched2() throws Exception { + SendDeleteMigrationNotifications s = spy(new SendDeleteMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 2, "test", EventAction.DELETE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + Mockito.verify(s, times(2)).trigger(); + + } + + @Test + public void processEverythingBatched3() throws Exception { + SendDeleteMigrationNotifications s = spy(new SendDeleteMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 3, "test", EventAction.DELETE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + Mockito.verify(s, times(2)).trigger(); + + } + +} \ No newline at end of file diff --git a/src/test/java/org/onap/aai/util/SendMigrationNotificationsTest.java b/src/test/java/org/onap/aai/util/SendMigrationNotificationsTest.java new file mode 100644 index 0000000..6719d0f --- /dev/null +++ b/src/test/java/org/onap/aai/util/SendMigrationNotificationsTest.java @@ -0,0 +1,184 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 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.aai.util; + +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.janusgraph.core.JanusGraph; +import org.janusgraph.core.JanusGraphFactory; +import org.janusgraph.core.JanusGraphTransaction; +import org.junit.*; +import org.junit.runners.MethodSorters; +import org.mockito.Mockito; +import org.onap.aai.AAISetup; +import org.onap.aai.db.props.AAIProperties; +import org.onap.aai.dbmap.AAIGraph; +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.migration.EventAction; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.*; + +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class SendMigrationNotificationsTest extends AAISetup { + + private final static String FILE = "./test.txt"; + + private static AtomicBoolean graphCreated = new AtomicBoolean(false); + + private JanusGraph graph; + private JanusGraphTransaction tx; + private GraphTraversalSource g; + + private static final String REALTIME_CONFIG = "./src/main/resources/etc/appprops/janusgraph-realtime.properties"; + + @Before + public void setUp() throws Exception { + System.setProperty("realtime.db.config", REALTIME_CONFIG); + AAIGraph.getInstance(); + graph = AAIGraph.getInstance().getGraph(); + tx = graph.newTransaction(); + g = tx.traversal(); + + createGraph(); + } + + public void createGraph() throws AAIException, IOException { + if(!graphCreated.get()){ + Vertex pnf1 = g.addV() + .property("aai-node-type", "pnf") + .property("pnf-name", SendMigrationNotifications.class.getSimpleName()+"-pnf-1") + .property(AAIProperties.RESOURCE_VERSION, "123") + .next(); + + Vertex pnf2 = g.addV() + .property("aai-node-type", "pnf") + .property("pnf-name", SendMigrationNotifications.class.getSimpleName()+"-pnf-2") + .property(AAIProperties.RESOURCE_VERSION, "456") + .next(); + + Vertex pnf3 = g.addV() + .property("aai-node-type", "pnf") + .property("pnf-name", SendMigrationNotifications.class.getSimpleName()+"-pnf-3") + .property(AAIProperties.RESOURCE_VERSION, "111") + .next(); + + Vertex pinterface1 = g.addV() + .property("aai-node-type", "p-interface") + .property("interface-name", SendMigrationNotifications.class.getSimpleName()+"-pinterface-1") + .property(AAIProperties.RESOURCE_VERSION, "789") + .next(); + + Vertex pserver1 = g.addV() + .property("aai-node-type", "pserver") + .property("hostname", SendMigrationNotifications.class.getSimpleName()+"-pserver-1") + .property(AAIProperties.RESOURCE_VERSION, "333") + .next(); + + edgeSerializer.addTreeEdge(g, pnf1, pinterface1); + + tx.commit(); + + List list = new ArrayList<>(); + list.add(pnf1.id().toString() + "_123"); // valid + list.add(pnf2.id().toString() + "_345"); // invalid: no longer the current resource version + list.add(pnf2.id().toString() + "_456"); // valid: same as above but with the correct resource version + list.add(pinterface1.id().toString() + "_789"); // valid + list.add(pnf3.id().toString() + "_222"); // invalid: wrong resource version + list.add("345_345"); // invalid + list.add(pserver1.id().toString() + "_333"); // valid + Files.write(Paths.get(FILE), (Iterable)list.stream()::iterator); + graphCreated.compareAndSet(false, true); + } + } + @AfterClass + public static void cleanUp() throws IOException { + Files.delete(Paths.get(FILE)); + } + + @After + public void tearDown() throws IOException { + if (tx.isOpen()) { + tx.tx().rollback(); + } + } + + @Test + public void processEverything() throws Exception { + SendMigrationNotifications s = spy(new SendMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 0, "test", EventAction.UPDATE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + assertEquals("4 events are created ", 4, s.notificationHelper.getNotifications().getEvents().size()); + + } + + @Test + public void processOnlyPnfs() throws Exception { + SendMigrationNotifications s = spy(new SendMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, new HashSet<>(Arrays.asList("pnf")), 0, 0, "test", EventAction.UPDATE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + assertEquals("2 events are created ", 2, s.notificationHelper.getNotifications().getEvents().size()); + + } + + @Test + public void processOnlyPnfsAndPservers() throws Exception { + SendMigrationNotifications s = spy(new SendMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, new HashSet<>(Arrays.asList("pserver","pnf")), 0, 0, "test", EventAction.UPDATE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + assertEquals("3 events are created ", 3, s.notificationHelper.getNotifications().getEvents().size()); + + } + + @Test + public void processEverythingBatched2() throws Exception { + SendMigrationNotifications s = spy(new SendMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 2, "test", EventAction.UPDATE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + Mockito.verify(s, times(2)).trigger(); + + } + + @Test + public void processEverythingBatched3() throws Exception { + SendMigrationNotifications s = spy(new SendMigrationNotifications( + loaderFactory, schemaVersions, REALTIME_CONFIG, FILE, Collections.EMPTY_SET, 0, 3, "test", EventAction.UPDATE, "DMAAP-LOAD")); + doNothing().when(s).trigger(); + doNothing().when(s).cleanup(); + s.process("/aai/"); + Mockito.verify(s, times(2)).trigger(); + + } + +} \ No newline at end of file -- cgit 1.2.3-korg