use of org.structr.core.property.StringProperty in project structr by structr.
the class ValidationTest method testEmptyStringPropertyValidationWithEmptyStrings.
// ----- string property validation tests -----
@Test
public void testEmptyStringPropertyValidationWithEmptyStrings() {
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) {
String uuid = null;
try (final Tx tx = app.tx()) {
// for string properties, null equals the empty string
uuid = app.create(testType, new NodeAttribute<>(key, "")).getUuid();
app.create(testType, new NodeAttribute<>(key, ""));
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());
assertEquals("Invalid uniqueness validation result", uuid, token.getDetail());
}
}
}
}
use of org.structr.core.property.StringProperty in project structr by structr.
the class ValidationTest method testEmptyStringPropertyValidationWithNulls.
@Test
public void testEmptyStringPropertyValidationWithNulls() {
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) {
String uuid = null;
try (final Tx tx = app.tx()) {
// for string properties, null equals the empty string
uuid = app.create(testType, new NodeAttribute<>(key, null)).getUuid();
app.create(testType, new NodeAttribute<>(key, null));
tx.success();
} catch (FrameworkException fex) {
fail("Invalid uniqueness constraint validation for null values!");
}
}
}
}
use of org.structr.core.property.StringProperty 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.StringProperty 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.StringProperty 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()));
}
}
Aggregations