//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void nodeKeyConstraintRuleNameMustNotBeTheEmptyString()
        public virtual void NodeKeyConstraintRuleNameMustNotBeTheEmptyString()
        {
            //noinspection RedundantStringConstructorCall
            string name = "";

            ConstraintRule.ConstraintRuleConflict(RULE_ID, ConstraintDescriptorFactory.nodeKeyForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID_2, name);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void constraintRuleNameMustNotBeTheEmptyString()
        public virtual void ConstraintRuleNameMustNotBeTheEmptyString()
        {
            //noinspection RedundantStringConstructorCall
            string name = "";

            ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1), name);
        }
        // READ CONSTRAINT

//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private static ConstraintRule readConstraintRule(long id, ByteBuffer source) throws org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException
        private static ConstraintRule ReadConstraintRule(long id, ByteBuffer source)
        {
            SchemaDescriptor schema;
            sbyte            constraintRuleType = source.get();
            string           name;

            switch (constraintRuleType)
            {
            case EXISTS_CONSTRAINT:
                schema = ReadSchema(source);
                name   = ReadRuleName(source).orElse(null);
                return(ConstraintRule.ConstraintRuleConflict(id, ConstraintDescriptorFactory.existsForSchema(schema), name));

            case UNIQUE_CONSTRAINT:
                long ownedUniqueIndex = source.Long;
                schema = ReadSchema(source);
                UniquenessConstraintDescriptor descriptor = ConstraintDescriptorFactory.uniqueForSchema(schema);
                name = ReadRuleName(source).orElse(null);
                return(ConstraintRule.ConstraintRuleConflict(id, descriptor, ownedUniqueIndex, name));

            case UNIQUE_EXISTS_CONSTRAINT:
                long ownedNodeKeyIndex = source.Long;
                schema = ReadSchema(source);
                NodeKeyConstraintDescriptor nodeKeyConstraintDescriptor = ConstraintDescriptorFactory.nodeKeyForSchema(schema);
                name = ReadRuleName(source).orElse(null);
                return(ConstraintRule.ConstraintRuleConflict(id, nodeKeyConstraintDescriptor, ownedNodeKeyIndex, name));

            default:
                throw new MalformedSchemaRuleException(format("Got unknown constraint rule type '%d'.", constraintRuleType));
            }
        }
        /// <summary>
        /// Serialize the provided ConstraintRule onto the target buffer </summary>
        /// <param name="constraintRule"> the ConstraintRule to serialize </param>
        /// <exception cref="IllegalStateException"> if the ConstraintRule is of type unique, but the owned index has not been set </exception>
        public static sbyte[] Serialize(ConstraintRule constraintRule)
        {
            ByteBuffer target = ByteBuffer.allocate(LengthOf(constraintRule));

            target.putInt(LEGACY_LABEL_OR_REL_TYPE_ID);
            target.put(CONSTRAINT_RULE);

            ConstraintDescriptor constraintDescriptor = constraintRule.ConstraintDescriptor;

            switch (constraintDescriptor.Type())
            {
            case EXISTS:
                target.put(EXISTS_CONSTRAINT);
                break;

            case UNIQUE:
                target.put(UNIQUE_CONSTRAINT);
                target.putLong(constraintRule.OwnedIndex);
                break;

            case UNIQUE_EXISTS:
                target.put(UNIQUE_EXISTS_CONSTRAINT);
                target.putLong(constraintRule.OwnedIndex);
                break;

            default:
                throw new System.NotSupportedException(format("Got unknown index descriptor type '%s'.", constraintDescriptor.Type()));
            }

            constraintDescriptor.Schema().processWith(new SchemaDescriptorSerializer(target));
            UTF8.putEncodedStringInto(constraintRule.Name, target);
            return(target.array());
        }
        private void AssertCorrectLength(ConstraintRule constraintRule)
        {
            // GIVEN
            ByteBuffer buffer = ByteBuffer.wrap(SchemaRuleSerialization.Serialize(constraintRule));

            // THEN
            assertThat(SchemaRuleSerialization.LengthOf(constraintRule), equalTo(buffer.capacity()));
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertSerializeAndDeserializeConstraintRule(ConstraintRule constraintRule) throws org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException
        private void AssertSerializeAndDeserializeConstraintRule(ConstraintRule constraintRule)
        {
            ConstraintRule deserialized = AssertConstraintRule(SerialiseAndDeserialise(constraintRule));

            assertThat(deserialized.Id, equalTo(constraintRule.Id));
            assertThat(deserialized.ConstraintDescriptor, equalTo(constraintRule.ConstraintDescriptor));
            assertThat(deserialized.Schema(), equalTo(constraintRule.Schema()));
        }
Exemple #7
0
 public override bool Equals(object o)
 {
     if (o is ConstraintRule)
     {
         ConstraintRule that = ( ConstraintRule )o;
         return(this._descriptor.Equals(that._descriptor));
     }
     return(false);
 }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void rulesCreatedWithNameMustRetainGivenNameAfterDeserialisation() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void RulesCreatedWithNameMustRetainGivenNameAfterDeserialisation()
        {
            string name = "custom_rule";

            assertThat(SerialiseAndDeserialise(NamedForLabel(name, LABEL_ID, PROPERTY_ID_1).withId(RULE_ID)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(NamedUniqueForLabel(name, LABEL_ID, PROPERTY_ID_1).withIds(RULE_ID_2, RULE_ID)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(NamedForLabel(name, LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2).withId(RULE_ID)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(NamedUniqueForLabel(name, LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2).withIds(RULE_ID_2, RULE_ID)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(NamedForLabel(name, LABEL_ID, IntStream.range(1, 200).toArray()).withId(RULE_ID)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1), name)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.ConstraintRuleConflict(RULE_ID_2, ConstraintDescriptorFactory.uniqueForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID, name)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.ConstraintRuleConflict(RULE_ID_2, ConstraintDescriptorFactory.nodeKeyForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID, name)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID_2, ConstraintDescriptorFactory.existsForRelType(REL_TYPE_ID, PROPERTY_ID_1), name)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2), name)).Name, @is(name));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID_2, ConstraintDescriptorFactory.existsForRelType(REL_TYPE_ID, PROPERTY_ID_1, PROPERTY_ID_2), name)).Name, @is(name));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void rulesCreatedWithNullNameMustRetainComputedNameAfterDeserialisation() throws Exception
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void RulesCreatedWithNullNameMustRetainComputedNameAfterDeserialisation()
        {
            assertThat(SerialiseAndDeserialise(ForLabel(LABEL_ID, PROPERTY_ID_1).withId(RULE_ID)).Name, @is("index_1"));
            assertThat(SerialiseAndDeserialise(UniqueForLabel(LABEL_ID, PROPERTY_ID_1).withIds(RULE_ID_2, RULE_ID)).Name, @is("index_2"));
            assertThat(SerialiseAndDeserialise(ForLabel(LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2).withId(RULE_ID)).Name, @is("index_1"));
            assertThat(SerialiseAndDeserialise(UniqueForLabel(LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2).withIds(RULE_ID_2, RULE_ID)).Name, @is("index_2"));
            assertThat(SerialiseAndDeserialise(ForLabel(LABEL_ID, IntStream.range(1, 200).toArray()).withId(RULE_ID)).Name, @is("index_1"));

            string name = null;

            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1), name)).Name, @is("constraint_1"));
            assertThat(SerialiseAndDeserialise(ConstraintRule.ConstraintRuleConflict(RULE_ID_2, ConstraintDescriptorFactory.uniqueForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID, name)).Name, @is("constraint_2"));
            assertThat(SerialiseAndDeserialise(ConstraintRule.ConstraintRuleConflict(RULE_ID_2, ConstraintDescriptorFactory.nodeKeyForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID, name)).Name, @is("constraint_2"));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID_2, ConstraintDescriptorFactory.existsForRelType(REL_TYPE_ID, PROPERTY_ID_1), name)).Name, @is("constraint_2"));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1, PROPERTY_ID_2), name)).Name, @is("constraint_1"));
            assertThat(SerialiseAndDeserialise(ConstraintRule.constraintRule(RULE_ID_2, ConstraintDescriptorFactory.existsForRelType(REL_TYPE_ID, PROPERTY_ID_1, PROPERTY_ID_2), name)).Name, @is("constraint_2"));
        }
        /// <summary>
        /// Compute the byte size needed to serialize the provided ConstraintRule using serialize. </summary>
        /// <param name="constraintRule"> the ConstraintRule </param>
        /// <returns> the byte size of ConstraintRule </returns>
        internal static int LengthOf(ConstraintRule constraintRule)
        {
            int length = 4;            // legacy label or relType id

            length += 1;               // schema rule type

            length += 1;               // constraint type
            ConstraintDescriptor constraintDescriptor = constraintRule.ConstraintDescriptor;

            if (constraintDescriptor.EnforcesUniqueness())
            {
                length += 8;                         // owned index id
            }

            length += constraintDescriptor.Schema().computeWith(schemaSizeComputer);
            length += UTF8.computeRequiredByteBufferSize(constraintRule.Name);
            return(length);
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertParseRelationshipPropertyExistsRule(String serialized, String name) throws Exception
        private void AssertParseRelationshipPropertyExistsRule(string serialized, string name)
        {
            // GIVEN
            long ruleId      = 51;
            int  propertyKey = 6119;
            int  relTypeId   = 8512;
            ConstraintDescriptor constraint = ConstraintDescriptorFactory.existsForRelType(relTypeId, propertyKey);

            sbyte[] bytes = DecodeBase64(serialized);

            // WHEN
            ConstraintRule deserialized = AssertConstraintRule(SchemaRuleSerialization.Deserialize(ruleId, ByteBuffer.wrap(bytes)));

            // THEN
            assertThat(deserialized.Id, equalTo(ruleId));
            assertThat(deserialized.ConstraintDescriptor, equalTo(constraint));
            assertThat(deserialized.Schema(), equalTo(constraint.Schema()));
            assertException(deserialized.getOwnedIndex, typeof(System.InvalidOperationException));
            assertThat(deserialized.Name, @is(name));
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private void assertParseNodeKeyConstraintRule(String serialized, String name) throws org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException
        private void AssertParseNodeKeyConstraintRule(string serialized, string name)
        {
            // GIVEN
            long ruleId       = 1;
            int  propertyKey  = 3;
            int  labelId      = 55;
            long ownedIndexId = 2;
            NodeKeyConstraintDescriptor constraint = ConstraintDescriptorFactory.nodeKeyForLabel(labelId, propertyKey);

            sbyte[] bytes = DecodeBase64(serialized);

            // WHEN
            ConstraintRule deserialized = AssertConstraintRule(SchemaRuleSerialization.Deserialize(ruleId, ByteBuffer.wrap(bytes)));

            // THEN
            assertThat(deserialized.Id, equalTo(ruleId));
            assertThat(deserialized.ConstraintDescriptor, equalTo(constraint));
            assertThat(deserialized.Schema(), equalTo(constraint.Schema()));
            assertThat(deserialized.OwnedIndex, equalTo(ownedIndexId));
            assertThat(deserialized.Name, @is(name));
        }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
//ORIGINAL LINE: private org.neo4j.storageengine.api.schema.SchemaRule serialiseAndDeserialise(ConstraintRule constraintRule) throws org.neo4j.internal.kernel.api.exceptions.schema.MalformedSchemaRuleException
        private SchemaRule SerialiseAndDeserialise(ConstraintRule constraintRule)
        {
            ByteBuffer buffer = ByteBuffer.wrap(SchemaRuleSerialization.Serialize(constraintRule));

            return(SchemaRuleSerialization.Deserialize(constraintRule.Id, buffer));
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void nodeKeyConstraintRuleNameMustNotContainNullCharacter()
        public virtual void NodeKeyConstraintRuleNameMustNotContainNullCharacter()
        {
            string name = "a\0b";

            ConstraintRule.ConstraintRuleConflict(RULE_ID, ConstraintDescriptorFactory.nodeKeyForLabel(LABEL_ID, PROPERTY_ID_1), RULE_ID_2, name);
        }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test(expected = IllegalArgumentException.class) public void constraintRuleNameMustNotContainNullCharacter()
        public virtual void ConstraintRuleNameMustNotContainNullCharacter()
        {
            string name = "a\0b";

            ConstraintRule.constraintRule(RULE_ID, ConstraintDescriptorFactory.existsForLabel(LABEL_ID, PROPERTY_ID_1), name);
        }