Package com.orientechnologies.orient.test.database.auto

Source Code of com.orientechnologies.orient.test.database.auto.SchemaIndexTest

package com.orientechnologies.orient.test.database.auto;

import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import com.orientechnologies.orient.core.storage.OStorage;
import org.testng.Assert;
import org.testng.annotations.*;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.exception.OSchemaException;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.sql.OCommandSQL;

import java.util.List;

@Test(groups = { "index" })
public class SchemaIndexTest extends DocumentDBBaseTest {

  @Parameters(value = "url")
  public SchemaIndexTest(@Optional final String iURL) {
    super(iURL);
  }

  @BeforeMethod
  public void beforeMethod() throws Exception {
    super.beforeMethod();

    final OSchema schema = database.getMetadata().getSchema();
    final OClass superTest = schema.createClass("SchemaSharedIndexSuperTest");
    final OClass test = schema.createClass("SchemaIndexTest", superTest);
    test.createProperty("prop1", OType.DOUBLE);
    test.createProperty("prop2", OType.DOUBLE);

    schema.save();
  }

  @AfterMethod
  public void tearDown() throws Exception {
    database.command(new OCommandSQL("drop class SchemaIndexTest")).execute();
    database.command(new OCommandSQL("drop class SchemaSharedIndexSuperTest")).execute();
    database.getMetadata().getSchema().reload();
  }

  @Test
  public void testDropClass() throws Exception {
    database.command(new OCommandSQL("CREATE INDEX SchemaSharedIndexCompositeIndex ON SchemaIndexTest (prop1, prop2) UNIQUE"))
        .execute();
    database.getMetadata().getIndexManager().reload();
    Assert.assertNotNull(database.getMetadata().getIndexManager().getIndex("SchemaSharedIndexCompositeIndex"));

    database.getMetadata().getSchema().dropClass("SchemaIndexTest");
    database.getMetadata().getSchema().reload();
    database.getMetadata().getIndexManager().reload();

    Assert.assertNull(database.getMetadata().getSchema().getClass("SchemaIndexTest"));
    Assert.assertNotNull(database.getMetadata().getSchema().getClass("SchemaSharedIndexSuperTest"));

    Assert.assertNull(database.getMetadata().getIndexManager().getIndex("SchemaSharedIndexCompositeIndex"));
  }

  @Test
  public void testDropSuperClass() throws Exception {
    database.command(new OCommandSQL("CREATE INDEX SchemaSharedIndexCompositeIndex ON SchemaIndexTest (prop1, prop2) UNIQUE"))
        .execute();
    database.getMetadata().getIndexManager().reload();

    try {
      database.getMetadata().getSchema().dropClass("SchemaSharedIndexSuperTest");
      Assert.fail();
    } catch (OSchemaException e) {
      Assert
          .assertEquals(
              e.getMessage(),
              "Class SchemaSharedIndexSuperTest cannot be dropped because it has sub classes. Remove the dependencies before trying to drop it again");
    }

    database.getMetadata().getSchema().reload();

    Assert.assertNotNull(database.getMetadata().getSchema().getClass("SchemaIndexTest"));
    Assert.assertNotNull(database.getMetadata().getSchema().getClass("SchemaSharedIndexSuperTest"));

    Assert.assertNotNull(database.getMetadata().getIndexManager().getIndex("SchemaSharedIndexCompositeIndex"));
  }

  public void testPolymorphicIdsPropagationAfterClusterAddRemove() {
    final OSchema schema = database.getMetadata().getSchema();

    OClass polymorpicIdsPropagationSuperSuper = schema.getClass("polymorpicIdsPropagationSuperSuper");

    if (polymorpicIdsPropagationSuperSuper == null)
      polymorpicIdsPropagationSuperSuper = schema.createClass("polymorpicIdsPropagationSuperSuper");

    OClass polymorpicIdsPropagationSuper = schema.getClass("polymorpicIdsPropagationSuper");
    if (polymorpicIdsPropagationSuper == null)
      polymorpicIdsPropagationSuper = schema.createClass("polymorpicIdsPropagationSuper");

    OClass polymorpicIdsPropagation = schema.getClass("polymorpicIdsPropagation");
    if (polymorpicIdsPropagation == null)
      polymorpicIdsPropagation = schema.createClass("polymorpicIdsPropagation");

    polymorpicIdsPropagation.setSuperClass(polymorpicIdsPropagationSuper);
    polymorpicIdsPropagationSuper.setSuperClass(polymorpicIdsPropagationSuperSuper);

    polymorpicIdsPropagationSuperSuper.createProperty("value", OType.STRING);
    polymorpicIdsPropagationSuperSuper.createIndex("PolymorpicIdsPropagationSuperSuperIndex", OClass.INDEX_TYPE.UNIQUE, "value");

    int counter = 0;

    for (int i = 0; i < 10; i++) {
      ODocument document = new ODocument("polymorpicIdsPropagation");
      document.field("value", "val" + counter);
      document.save();

      counter++;
    }

    final int clusterId2 = database.addCluster("polymorpicIdsPropagationSuperSuper2");

    for (int i = 0; i < 10; i++) {
      ODocument document = new ODocument();
      document.field("value", "val" + counter);
      document.save("polymorpicIdsPropagationSuperSuper2");

      counter++;
    }

    polymorpicIdsPropagation.addCluster("polymorpicIdsPropagationSuperSuper2");

    assertContains(polymorpicIdsPropagationSuperSuper.getPolymorphicClusterIds(), clusterId2);
    assertContains(polymorpicIdsPropagationSuper.getPolymorphicClusterIds(), clusterId2);

    List<ODocument> result = database.query(new OSQLSynchQuery<ODocument>(
        "select from polymorpicIdsPropagationSuperSuper where value = 'val12'"));

    Assert.assertEquals(result.size(), 1);

    ODocument doc = result.get(0);
    Assert.assertEquals(doc.getSchemaClass().getName(), "polymorpicIdsPropagation");

    polymorpicIdsPropagation.removeClusterId(clusterId2);

    assertDoesNotContain(polymorpicIdsPropagationSuperSuper.getPolymorphicClusterIds(), clusterId2);
    assertDoesNotContain(polymorpicIdsPropagationSuper.getPolymorphicClusterIds(), clusterId2);

    result = database.query(new OSQLSynchQuery<ODocument>("select from polymorpicIdsPropagationSuperSuper  where value = 'val12'"));
    Assert.assertTrue(result.isEmpty());
  }

  private void assertContains(int[] clusterIds, int clusterId) {
    boolean contains = false;
    for (int cluster : clusterIds) {
      if (cluster == clusterId) {
        contains = true;
        break;
      }
    }

    Assert.assertTrue(contains);
  }

  private void assertDoesNotContain(int[] clusterIds, int clusterId) {
    boolean contains = false;
    for (int cluster : clusterIds) {
      if (cluster == clusterId) {
        contains = true;
        break;
      }
    }

    Assert.assertTrue(!contains);
  }
}
TOP

Related Classes of com.orientechnologies.orient.test.database.auto.SchemaIndexTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.