Skip to content

Commit

Permalink
refactor: removed used of some deprecated API, generalized server tes…
Browse files Browse the repository at this point in the history
…ts setup/teardown
  • Loading branch information
tglman committed Dec 7, 2023
1 parent 4c65198 commit 732c59f
Show file tree
Hide file tree
Showing 14 changed files with 452 additions and 629 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentAbstract;
import com.orientechnologies.orient.core.db.record.OAutoConvertToRecord;
import com.orientechnologies.orient.core.db.record.ODetachable;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
Expand Down Expand Up @@ -3366,7 +3366,7 @@ protected void setup(ODatabaseDocumentInternal db) {

if (recordFormat == null)
// GET THE DEFAULT ONE
recordFormat = ODatabaseDocumentTx.getDefaultSerializer();
recordFormat = ODatabaseDocumentAbstract.getDefaultSerializer();
}

protected String checkFieldName(final String iFieldName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.orientechnologies.orient.server;

import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;

public class BaseMemoryInternalDatabase {

protected ODatabaseDocumentInternal db;
protected OrientDB context;
@Rule public TestName name = new TestName();

@Before
public void beforeTest() {
context = new OrientDB("embedded:", OrientDBConfig.defaultConfig());
context
.execute(
"create database "
+ name.getMethodName()
+ " memory users(admin identified by 'adminpwd' role admin) ")
.close();
db = (ODatabaseDocumentInternal) context.open(name.getMethodName(), "admin", "adminpwd");
}

@After
public void afterTest() {
db.close();
context.drop(name.getMethodName());
context.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.orientechnologies.orient.server;

import com.orientechnologies.common.io.OFileUtils;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.OrientDB;
import com.orientechnologies.orient.core.db.OrientDBConfig;
import java.io.File;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestName;

public class BaseServerMemoryDatabase {

protected ODatabaseDocumentInternal db;
protected OrientDB context;
@Rule public TestName name = new TestName();
protected OServer server;

@Before
public void beforeTest() {
server = new OServer(false);
try {
server.startup(getClass().getResourceAsStream("orientdb-server-config.xml"));
server.activate();
} catch (Exception e) {
throw new RuntimeException(e);
}

context = new OrientDB("remote:localhost", "root", "root", OrientDBConfig.defaultConfig());
context
.execute(
"create database "
+ name.getMethodName()
+ " memory users(admin identified by 'adminpwd' role admin) ")
.close();
db = (ODatabaseDocumentInternal) context.open(name.getMethodName(), "admin", "adminpwd");
}

@After
public void afterTest() {
db.close();
context.drop(name.getMethodName());
context.close();
String directory = server.getDatabaseDirectory();
server.shutdown();
OFileUtils.deleteRecursively(new File(directory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@

import com.orientechnologies.orient.core.config.OContextConfiguration;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException;
import com.orientechnologies.orient.server.network.protocol.binary.ONetworkProtocolBinary;
import com.orientechnologies.orient.server.token.OTokenHandlerImpl;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/** Created by tglman on 27/12/15. */
public class OClientConnectionTest {
public class OClientConnectionTest extends BaseMemoryInternalDatabase {

private ODatabaseDocumentInternal db;
@Mock private ONetworkProtocolBinary protocol;

@Mock private ONetworkProtocolBinary protocol1;
Expand All @@ -32,19 +27,12 @@ public class OClientConnectionTest {

@Mock private OServer server;

@Before
public void before() {
public void beforeTest() {
super.beforeTest();
MockitoAnnotations.initMocks(this);
Mockito.when(protocol.getServer()).thenReturn(server);
Mockito.when(server.getClientConnectionManager()).thenReturn(manager);
Mockito.when(server.getContextConfiguration()).thenReturn(new OContextConfiguration());
db = new ODatabaseDocumentTx("memory:" + OClientConnectionTest.class.getSimpleName());
db.create();
}

@After
public void after() {
db.drop();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class OLiveQueryRemoteTest {

private OServer server;
private OrientDB orientDB;
private ODatabaseDocument database;
private ODatabaseDocument db;

@Before
public void before() throws Exception {
Expand All @@ -56,12 +56,12 @@ public void before() throws Exception {
orientDB.execute(
"create database ? memory users (admin identified by 'admin' role admin)",
OLiveQueryRemoteTest.class.getSimpleName());
database = orientDB.open(OLiveQueryRemoteTest.class.getSimpleName(), "admin", "admin");
db = orientDB.open(OLiveQueryRemoteTest.class.getSimpleName(), "admin", "admin");
}

@After
public void after() {
database.close();
db.close();
orientDB.close();
server.shutdown();

Expand Down Expand Up @@ -111,9 +111,9 @@ public void onEnd(ODatabaseDocument database) {
@Test
public void testRidSelect() throws InterruptedException {
MyLiveQueryListener listener = new MyLiveQueryListener(new CountDownLatch(1));
OVertex item = database.newVertex();
OVertex item = db.newVertex();
item.save();
OLiveQueryMonitor live = database.live("LIVE SELECT FROM " + item.getIdentity(), listener);
OLiveQueryMonitor live = db.live("LIVE SELECT FROM " + item.getIdentity(), listener);
item.setProperty("x", "z");
item.save();
Assert.assertTrue(listener.latch.await(10, TimeUnit.SECONDS));
Expand All @@ -122,25 +122,25 @@ public void testRidSelect() throws InterruptedException {
@Test
public void testLiveInsert() throws InterruptedException {

database.getMetadata().getSchema().createClass("test");
database.getMetadata().getSchema().createClass("test2");
db.getMetadata().getSchema().createClass("test");
db.getMetadata().getSchema().createClass("test2");
MyLiveQueryListener listener = new MyLiveQueryListener(new CountDownLatch(2));

OLiveQueryMonitor monitor = database.live("select from test", listener);
OLiveQueryMonitor monitor = db.live("select from test", listener);
Assert.assertNotNull(monitor);

database.command("insert into test set name = 'foo', surname = 'bar'").close();
database.command("insert into test set name = 'foo', surname = 'baz'").close();
database.command("insert into test2 set name = 'foo'").close();
db.command("insert into test set name = 'foo', surname = 'bar'").close();
db.command("insert into test set name = 'foo', surname = 'baz'").close();
db.command("insert into test2 set name = 'foo'").close();

Assert.assertTrue(listener.latch.await(1, TimeUnit.MINUTES));

monitor.unSubscribe();
Assert.assertTrue(listener.ended.await(1, TimeUnit.MINUTES));

database.command("insert into test set name = 'foo', surname = 'bax'");
database.command("insert into test2 set name = 'foo'");
database.command("insert into test set name = 'foo', surname = 'baz'");
db.command("insert into test set name = 'foo', surname = 'bax'");
db.command("insert into test2 set name = 'foo'");
db.command("insert into test set name = 'foo', surname = 'baz'");

Assert.assertEquals(listener.ops.size(), 2);
for (OResult doc : listener.ops) {
Expand All @@ -154,15 +154,15 @@ public void testLiveInsert() throws InterruptedException {
@Test
@Ignore
public void testRestrictedLiveInsert() throws ExecutionException, InterruptedException {
OSchema schema = database.getMetadata().getSchema();
OSchema schema = db.getMetadata().getSchema();
OClass oRestricted = schema.getClass("ORestricted");
schema.createClass("test", oRestricted);

int liveMatch = 1;
OResultSet query = database.query("select from OUSer where name = 'reader'");
OResultSet query = db.query("select from OUSer where name = 'reader'");

final OIdentifiable reader = query.next().getIdentity().orElse(null);
final OIdentifiable current = database.getUser().getIdentity();
final OIdentifiable current = db.getUser().getIdentity();

ExecutorService executorService = Executors.newSingleThreadExecutor();

Expand Down Expand Up @@ -216,9 +216,9 @@ public void onEnd(ODatabaseDocument database) {}
latch.await();

query.close();
database.command("insert into test set name = 'foo', surname = 'bar'");
db.command("insert into test set name = 'foo', surname = 'bar'");

database.command(
db.command(
"insert into test set name = 'foo', surname = 'bar', _allow=?",
new ArrayList<OIdentifiable>() {
{
Expand All @@ -234,24 +234,24 @@ public void onEnd(ODatabaseDocument database) {}
@Test
public void testBatchWithTx() throws InterruptedException {

database.getMetadata().getSchema().createClass("test");
database.getMetadata().getSchema().createClass("test2");
db.getMetadata().getSchema().createClass("test");
db.getMetadata().getSchema().createClass("test2");

int txSize = 100;

MyLiveQueryListener listener = new MyLiveQueryListener(new CountDownLatch(txSize));

OLiveQueryMonitor monitor = database.live("select from test", listener);
OLiveQueryMonitor monitor = db.live("select from test", listener);
Assert.assertNotNull(monitor);

database.begin();
db.begin();
for (int i = 0; i < txSize; i++) {
OElement elem = database.newElement("test");
OElement elem = db.newElement("test");
elem.setProperty("name", "foo");
elem.setProperty("surname", "bar" + i);
elem.save();
}
database.commit();
db.commit();

Assert.assertTrue(listener.latch.await(1, TimeUnit.MINUTES));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,33 @@

import static org.junit.Assert.assertTrue;

import com.orientechnologies.common.io.OFileUtils;
import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.client.remote.OStorageRemote;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.command.OCommandOutputListener;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.server.OServer;
import com.orientechnologies.orient.server.BaseServerMemoryDatabase;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.UnsupportedEncodingException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/** Created by tglman on 19/07/16. */
public class ORemoteImportTest {

private static final String SERVER_DIRECTORY = "./target/db";
private OServer server;

@Before
public void before() throws Exception {
server = new OServer(false);
server.setServerRootDirectory(SERVER_DIRECTORY);
server.startup(getClass().getResourceAsStream("orientdb-server-config.xml"));
server.activate();

OServerAdmin server = new OServerAdmin("remote:localhost");
server.connect("root", "root");
server.createDatabase(ORemoteImportTest.class.getSimpleName(), "graph", "memory");
server.close();
}

public class ORemoteImportTest extends BaseServerMemoryDatabase {
@Test
public void testImport() throws UnsupportedEncodingException {

ODatabaseDocumentInternal db =
new ODatabaseDocumentTx("remote:localhost/" + ORemoteImportTest.class.getSimpleName());
db.open("admin", "admin");
try {
String content =
"{\"records\": [{\"@type\": \"d\", \"@rid\": \"#9:0\",\"@version\": 1,\"@class\": \"V\"}]}";

OStorageRemote storage = (OStorageRemote) db.getStorage();
final StringBuffer buff = new StringBuffer();
storage.importDatabase(
"-merge=true",
new ByteArrayInputStream(content.getBytes("UTF8")),
"data.json",
new OCommandOutputListener() {
@Override
public void onMessage(String iText) {
buff.append(iText);
}
});
assertTrue(buff.toString().contains("Database import completed"));
} finally {
db.close();
}
}

@After
public void after() {
server.shutdown();
Orient.instance().shutdown();
OFileUtils.deleteRecursively(new File(SERVER_DIRECTORY));
Orient.instance().startup();
String content =
"{\"records\": [{\"@type\": \"d\", \"@rid\": \"#9:0\",\"@version\": 1,\"@class\": \"V\"}]}";

OStorageRemote storage = (OStorageRemote) db.getStorage();
final StringBuffer buff = new StringBuffer();
storage.importDatabase(
"-merge=true",
new ByteArrayInputStream(content.getBytes("UTF8")),
"data.json",
new OCommandOutputListener() {
@Override
public void onMessage(String iText) {
buff.append(iText);
}
});
assertTrue(buff.toString().contains("Database import completed"));
}
}
Loading

0 comments on commit 732c59f

Please sign in to comment.