public Error convert(final Exception exception) { Class errorClass = exception.getClass(); Class causeClass; if (errorClass.equals(ConstraintViolationException.class)) { causeClass = exception.getCause().getClass(); /*if (causeClass.equals(UniqueConstraintViolationKernelException.class)) { return analyseUniqueConstraintViolation((ConstraintViolationException) exception); }*/ } else if (errorClass.equals(NotFoundException.class)) { return analyseNotFound((NotFoundException) exception); } return new Error( Error.EXCEPTION, exception.getMessage() ); }
protected Error analyseUniqueConstraintViolation(final ConstraintViolationException constraintViolationException) { String errorMessage = constraintViolationException.getMessage(); final String part1 = "^Node "; final String part2 = " already exists with label "; final String part3 = " and property "; final String part4 = "\"=["; final String part5 = "]"; errorMessage = errorMessage.replaceFirst(part1, ""); int i = errorMessage.indexOf(part2); Long nodeId = Long.parseLong(errorMessage.substring(0, i)); errorMessage = errorMessage.substring(i + part2.length()); i = errorMessage.indexOf(part3); String label = errorMessage.substring(0, i); errorMessage = errorMessage.substring(i + part3.length()); i = errorMessage.indexOf(part4); String propertyName = errorMessage.substring(1, i); String propertyValue = errorMessage.substring(i + part4.length(), errorMessage.lastIndexOf(part5)); ObjectNode detailsNode = jsonObjectMapper.getObjectMapper().createObjectNode(); detailsNode.put("NodeId", nodeId); detailsNode.put("Label", label); detailsNode.put("Property", propertyName); detailsNode.put("Value", propertyValue); return new Error( Error.UNIQUE_CONSTRAINT_VIOLATION, constraintViolationException.getMessage(), detailsNode ); }
@Test public void shouldFailToValidateConstraintsIfUnderlyingIndexIsFailed() throws Exception { // given dbWithConstraint(); storeIndexFailure( "Injected failure" ); // when GraphDatabaseService db = startDatabase(); try { try ( Transaction tx = db.beginTx() ) { db.createNode( label( "Label1" ) ).setProperty( "key1", "value1" ); fail( "expected exception" ); } // then catch ( ConstraintViolationException e ) { assertThat( e.getCause(), instanceOf( UnableToValidateConstraintKernelException.class ) ); assertThat( e.getCause().getCause().getMessage(), equalTo( "The index is in a failed state: 'Injected failure'.") ); } } finally { db.shutdown(); } }
@Test public void shouldNotLeaveLuceneIndexFilesHangingAroundIfConstraintCreationFails() { // given GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI(); try ( Transaction tx = db.beginTx() ) { for ( int i = 0; i < 2; i++ ) { Node node1 = db.createNode( LABEL ); node1.setProperty( "prop", true ); } tx.success(); } // when try ( Transaction tx = db.beginTx() ) { db.schema().constraintFor( LABEL ).assertPropertyIsUnique( "prop" ).create(); fail("Should have failed with ConstraintViolationException"); tx.success(); } catch ( ConstraintViolationException ignored ) { } // then try(Transaction ignore = db.beginTx()) { assertEquals(0, Iterables.count(db.schema().getIndexes() )); } File schemaStorePath = SchemaIndexProvider .getRootDirectory( new File( db.getStoreDir() ), LuceneSchemaIndexProviderFactory.KEY ); String indexId = "1"; File[] files = new File(schemaStorePath, indexId ).listFiles(); assertNotNull( files ); assertEquals(0, files.length); }
@Test(expected = ConstraintViolationException.class) public void denyDuplicates() { XOManager xoManager = getXOManager(); xoManager.currentTransaction().begin(); B a1 = xoManager.create(B.class); a1.setValue("A1"); B a2_1 = xoManager.create(B.class); a2_1.setValue("A2"); B a2_2 = xoManager.create(B.class); a2_2.setValue("A2"); xoManager.currentTransaction().commit(); }