Search in sources :

Example 36 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class PlainOperationsTest method indexedBackedConstraintCreateMustThrowOnFulltextSchemas.

@Test
void indexedBackedConstraintCreateMustThrowOnFulltextSchemas() throws Exception {
    // given
    when(tokenHolders.labelTokens().getTokenById(anyInt())).thenReturn(new NamedToken("Label", 123));
    when(tokenHolders.propertyKeyTokens().getTokenById(anyInt())).thenReturn(new NamedToken("prop", 456));
    SchemaDescriptor schema = SchemaDescriptor.fulltext(NODE, this.schema.getEntityTokenIds(), this.schema.getPropertyIds());
    IndexPrototype prototype = IndexPrototype.uniqueForSchema(schema).withName("constraint name").withIndexProvider(GenericNativeIndexProvider.DESCRIPTOR);
    IndexDescriptor constraintIndex = prototype.materialise(42);
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), eq(prototype))).thenReturn(constraintIndex);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexProxy.getDescriptor()).thenReturn(constraintIndex);
    when(indexingService.getIndexProxy(constraintIndex)).thenReturn(indexProxy);
    when(storageReader.constraintsGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    when(storageReader.indexGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    // when
    var e = assertThrows(KernelException.class, () -> operations.uniquePropertyConstraintCreate(prototype));
    assertThat(e.getUserMessage(tokenHolders)).contains("full-text schema");
}
Also used : SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 37 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfConstraintCreationFails() throws Exception {
    // given
    UniquenessConstraintDescriptor constraint = uniqueForSchema(schema);
    storageReaderWithConstraints(constraint);
    int labelId = schema.getLabelId();
    int propertyId = schema.getPropertyId();
    when(tokenHolders.labelTokens().getTokenById(labelId)).thenReturn(new NamedToken("Label", labelId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", labelId));
    // when
    try {
        operations.uniquePropertyConstraintCreate(IndexPrototype.uniqueForSchema(schema).withName("constraint name"));
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.LABEL, labelId);
    order.verify(storageReader).constraintsGetForSchema(schema);
    order.verify(locks).releaseExclusive(ResourceTypes.LABEL, labelId);
}
Also used : AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) UniquenessConstraintDescriptor(org.neo4j.internal.schema.constraints.UniquenessConstraintDescriptor) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 38 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfRelationshipPropertyExistenceConstraintCreationFails() throws Exception {
    // given
    RelationTypeSchemaDescriptor descriptor = SchemaDescriptor.forRelType(11, 13);
    RelExistenceConstraintDescriptor constraint = existsForSchema(descriptor);
    storageReaderWithConstraints(constraint);
    int relTypeId = descriptor.getRelTypeId();
    int propertyId = descriptor.getPropertyId();
    when(tokenHolders.relationshipTypeTokens().getTokenById(relTypeId)).thenReturn(new NamedToken("Label", relTypeId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", relTypeId));
    // when
    try {
        operations.relationshipPropertyExistenceConstraintCreate(descriptor, "constraint name");
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
    order.verify(storageReader).constraintsGetForSchema(descriptor);
    order.verify(locks).releaseExclusive(ResourceTypes.RELATIONSHIP_TYPE, relTypeId);
}
Also used : RelExistenceConstraintDescriptor(org.neo4j.internal.schema.constraints.RelExistenceConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) RelationTypeSchemaDescriptor(org.neo4j.internal.schema.RelationTypeSchemaDescriptor) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Example 39 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class PlainOperationsTest method indexedBackedConstraintCreateMustThrowOnNonUniqueIndexPrototypes.

@Test
void indexedBackedConstraintCreateMustThrowOnNonUniqueIndexPrototypes() throws Exception {
    // given
    when(tokenHolders.labelTokens().getTokenById(anyInt())).thenReturn(new NamedToken("Label", 123));
    when(tokenHolders.propertyKeyTokens().getTokenById(anyInt())).thenReturn(new NamedToken("prop", 456));
    IndexPrototype prototype = IndexPrototype.forSchema(schema).withName("constraint name").withIndexProvider(GenericNativeIndexProvider.DESCRIPTOR);
    IndexDescriptor constraintIndex = prototype.materialise(42);
    when(constraintIndexCreator.createUniquenessConstraintIndex(any(), any(), eq(prototype))).thenReturn(constraintIndex);
    IndexProxy indexProxy = mock(IndexProxy.class);
    when(indexProxy.getDescriptor()).thenReturn(constraintIndex);
    when(indexingService.getIndexProxy(constraintIndex)).thenReturn(indexProxy);
    when(storageReader.constraintsGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    when(storageReader.indexGetForSchema(schema)).thenReturn(Collections.emptyIterator());
    // when
    var e = assertThrows(KernelException.class, () -> operations.uniquePropertyConstraintCreate(prototype));
    assertThat(e.getUserMessage(tokenHolders)).containsIgnoringCase("index prototype").containsIgnoringCase("not unique");
}
Also used : IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexProxy(org.neo4j.kernel.impl.api.index.IndexProxy) NamedToken(org.neo4j.token.api.NamedToken) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) Test(org.junit.jupiter.api.Test)

Example 40 with NamedToken

use of org.neo4j.token.api.NamedToken in project neo4j by neo4j.

the class PlainOperationsTest method shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails.

@Test
void shouldReleaseAcquiredSchemaWriteLockIfNodeKeyConstraintCreationFails() throws Exception {
    // given
    NodeKeyConstraintDescriptor constraint = nodeKeyForSchema(schema);
    storageReaderWithConstraints(constraint);
    int labelId = schema.getLabelId();
    int propertyId = schema.getPropertyId();
    when(tokenHolders.labelTokens().getTokenById(labelId)).thenReturn(new NamedToken("Label", labelId));
    when(tokenHolders.propertyKeyTokens().getTokenById(propertyId)).thenReturn(new NamedToken("prop", labelId));
    // when
    try {
        operations.nodeKeyConstraintCreate(IndexPrototype.uniqueForSchema(schema).withName("constraint name"));
        fail("Expected an exception because this schema should already be constrained.");
    } catch (AlreadyConstrainedException ignore) {
    // Good.
    }
    // then
    order.verify(locks).acquireExclusive(LockTracer.NONE, ResourceTypes.LABEL, labelId);
    order.verify(storageReader).constraintsGetForSchema(schema);
    order.verify(locks).releaseExclusive(ResourceTypes.LABEL, labelId);
}
Also used : NodeKeyConstraintDescriptor(org.neo4j.internal.schema.constraints.NodeKeyConstraintDescriptor) AlreadyConstrainedException(org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException) NamedToken(org.neo4j.token.api.NamedToken) Test(org.junit.jupiter.api.Test)

Aggregations

NamedToken (org.neo4j.token.api.NamedToken)63 Test (org.junit.jupiter.api.Test)41 ArrayList (java.util.ArrayList)6 TokenNotFoundException (org.neo4j.token.api.TokenNotFoundException)6 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)5 SchemaRule (org.neo4j.internal.schema.SchemaRule)5 NonUniqueTokenException (org.neo4j.token.api.NonUniqueTokenException)5 TokenHolder (org.neo4j.token.api.TokenHolder)5 AlreadyConstrainedException (org.neo4j.kernel.api.exceptions.schema.AlreadyConstrainedException)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3 IntSupplier (java.util.function.IntSupplier)3 KernelException (org.neo4j.exceptions.KernelException)3 Iterators (org.neo4j.internal.helpers.collection.Iterators)3 IdCapacityExceededException (org.neo4j.internal.id.IdCapacityExceededException)3 Token (org.neo4j.internal.kernel.api.Token)3 TokenWrite.checkValidTokenName (org.neo4j.internal.kernel.api.TokenWrite.checkValidTokenName)3 LabelNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.LabelNotFoundKernelException)3 PropertyKeyIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.PropertyKeyIdNotFoundKernelException)3 RelationshipTypeIdNotFoundKernelException (org.neo4j.internal.kernel.api.exceptions.RelationshipTypeIdNotFoundKernelException)3