use of org.structr.core.property.PropertyKey in project structr by structr.
the class ValidationTest method testStringPropertyUniqueness.
@Test
public void testStringPropertyUniqueness() {
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class testType = StructrApp.getConfiguration().getNodeEntityClass("Test");
if (testType != null) {
final PropertyKey key = StructrApp.key(testType, "testUnique");
if (key != null) {
try (final Tx tx = app.tx()) {
// key must be unique, but can empty
app.create(testType, new NodeAttribute<>(key, "unique"));
app.create(testType, new NodeAttribute<>(key, ""));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
for (int i = 0; i < 5; i++) {
try (final Tx tx = app.tx()) {
app.create(testType, new NodeAttribute<>(key, "unique"));
tx.success();
fail("Uniqueness constraint violated!");
} catch (FrameworkException fex) {
final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
final ErrorToken token = tokens.get(0);
assertEquals("Invalid uniqueness validation result", 1, tokens.size());
assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
assertEquals("Invalid uniqueness validation result", "testUnique", token.getProperty());
assertEquals("Invalid uniqueness validation result", "Test", token.getType());
assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
}
}
}
}
}
use of org.structr.core.property.PropertyKey in project structr by structr.
the class ValidationTest method testConcurrentValidation.
@Test
public void testConcurrentValidation() {
final int count = 100;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_name"), "+String!"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
assertNotNull(type);
final PropertyKey name = StructrApp.key(type, "name");
assertNotNull(name);
final Runnable tester = new Runnable() {
@Override
public void run() {
for (int i = 0; i < count; i++) {
// testing must be done in an isolated transaction
try (final Tx tx = app.tx()) {
app.create(type, "Item" + i);
tx.success();
} catch (FrameworkException ignore) {
}
}
}
};
// submit three test instances
final ExecutorService executor = Executors.newCachedThreadPool();
final Future f1 = executor.submit(tester);
final Future f2 = executor.submit(tester);
final Future f3 = executor.submit(tester);
try {
f1.get();
f2.get();
f3.get();
} catch (Throwable ex) {
}
List<GraphObject> result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(type).getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// verify that only count entities have been created.
assertEquals("Invalid concurrent validation result", count, result.size());
executor.shutdownNow();
}
use of org.structr.core.property.PropertyKey in project structr by structr.
the class ValidationTest method testDatePropertyUniquenessValidation.
// ----- date property validation tests -----
@Test
public void testDatePropertyUniquenessValidation() {
final String keyName = "unique";
final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "Date!");
final PropertyKey key = StructrApp.key(testType, keyName);
final Date date = new Date();
String uuid = null;
if (key != null) {
// test failure
try (final Tx tx = app.tx()) {
uuid = app.create(testType, new NodeAttribute<>(key, date)).getUuid();
app.create(testType, new NodeAttribute<>(key, date));
tx.success();
fail("Date property uniqueness constraint violated!");
} catch (FrameworkException fex) {
final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
final ErrorToken token = tokens.get(0);
assertEquals("Invalid date validation result", 1, tokens.size());
assertEquals("Invalid date validation result", 422, fex.getStatus());
assertEquals("Invalid date validation result", keyName, token.getProperty());
assertEquals("Invalid date validation result", "Test", token.getType());
assertEquals("Invalid date validation result", "already_taken", token.getToken());
assertEquals("Invalid date validation result", uuid, token.getDetail());
}
}
}
use of org.structr.core.property.PropertyKey in project structr by structr.
the class ValidationTest method testCompoundUniqueness.
@Test
public void testCompoundUniqueness() {
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestType"), new NodeAttribute<>(new StringProperty("_key1"), "String!!"), new NodeAttribute<>(new StringProperty("_key2"), "String!!"), new NodeAttribute<>(new StringProperty("_key3"), "String!!"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("TestType");
final PropertyKey key1 = StructrApp.key(type, "key1");
final PropertyKey key2 = StructrApp.key(type, "key2");
final PropertyKey key3 = StructrApp.key(type, "key3");
final PropertyKey[] keys = new PropertyKey[] { key1, key2, key3 };
// test success
try (final Tx tx = app.tx()) {
app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three"));
app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "one"), new NodeAttribute<>(key3, "three"));
tx.success();
} catch (FrameworkException fex) {
fail("Invalid compound indexing validation result.");
}
// test success
try (final Tx tx = app.tx()) {
app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key3, "three"));
app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key3, "four"));
tx.success();
} catch (FrameworkException fex) {
fail("Invalid compound indexing validation result.");
}
String uuid = null;
// test failure
try (final Tx tx = app.tx()) {
uuid = app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three")).getUuid();
app.create(type, new NodeAttribute<>(key1, "one"), new NodeAttribute<>(key2, "two"), new NodeAttribute<>(key3, "three"));
tx.success();
fail("Invalid compound indexing validation result.");
} catch (FrameworkException fex) {
System.out.println(fex.toJSON());
final ErrorToken token = fex.getErrorBuffer().getErrorTokens().get(0);
assertEquals("Invalid validation status code", fex.getStatus(), 422);
assertEquals("Invalid validation error token", "already_taken", token.getToken());
assertEquals("Invalid validation error type", "TestType", token.getType());
assertEquals("Invalid validation error type", uuid, token.getDetail());
assertTrue("Invalid validation error type", Arrays.equals(keys, (PropertyKey[]) token.getValue()));
}
}
use of org.structr.core.property.PropertyKey in project structr by structr.
the class ValidationTest method testArrayPropertyUniquenessValidation.
@Test
public void testArrayPropertyUniquenessValidation() {
final String keyName = "stringArray";
final Class<NodeInterface> testType = createTypeWithProperty("Test", keyName, "String[]!");
final PropertyKey key = StructrApp.key(testType, keyName);
String uuid1 = null;
String uuid2 = null;
if (key != null) {
// test failure
try (final Tx tx = app.tx()) {
uuid1 = app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" })).getUuid();
uuid2 = app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" })).getUuid();
tx.success();
fail("Array property validation failed!");
} catch (FrameworkException fex) {
final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
final ErrorToken token = tokens.get(0);
assertEquals("Invalid uniqueness validation result", 1, tokens.size());
assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
assertEquals("Invalid uniqueness validation result", keyName, token.getProperty());
assertEquals("Invalid uniqueness validation result", "Test", token.getType());
assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
assertEquals("Invalid uniqueness validation result", uuid1, token.getDetail());
}
removeInstances(testType);
// test success
try (final Tx tx = app.tx()) {
app.create(testType, new NodeAttribute<>(key, new String[] { "one" }));
app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two" }));
app.create(testType, new NodeAttribute<>(key, new String[] { "one", "two", "three" }));
tx.success();
} catch (FrameworkException fex) {
fail("Array property validation error!");
}
}
}
Aggregations