diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeParserTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeParserTest.java index 12398100bec..a89587354b9 100644 --- a/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeParserTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeParserTest.java @@ -17,18 +17,84 @@ package org.apache.rocketmq.common.attribute; import com.google.common.collect.Maps; -import org.junit.Assert; -import org.junit.Test; - import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; +import org.junit.Test; import static com.google.common.collect.Maps.newHashMap; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class AttributeParserTest { + + @Test + public void parseToMap_EmptyString_ReturnsEmptyMap() { + String attributesModification = ""; + Map result = AttributeParser.parseToMap(attributesModification); + assertTrue(result.isEmpty()); + } + + @Test + public void parseToMap_NullString_ReturnsEmptyMap() { + String attributesModification = null; + Map result = AttributeParser.parseToMap(attributesModification); + assertTrue(result.isEmpty()); + } + + @Test + public void parseToMap_ValidAttributesModification_ReturnsExpectedMap() { + String attributesModification = "+key1=value1,+key2=value2,-key3,+key4=value4"; + Map result = AttributeParser.parseToMap(attributesModification); + + Map expectedMap = new HashMap<>(); + expectedMap.put("+key1", "value1"); + expectedMap.put("+key2", "value2"); + expectedMap.put("-key3", ""); + expectedMap.put("+key4", "value4"); + + assertEquals(expectedMap, result); + } + + @Test(expected = RuntimeException.class) + public void parseToMap_InvalidAddAttributeFormat_ThrowsRuntimeException() { + String attributesModification = "+key1=value1,key2=value2,-key3,+key4=value4"; + AttributeParser.parseToMap(attributesModification); + } + + @Test(expected = RuntimeException.class) + public void parseToMap_InvalidDeleteAttributeFormat_ThrowsRuntimeException() { + String attributesModification = "+key1=value1,+key2=value2,key3,+key4=value4"; + AttributeParser.parseToMap(attributesModification); + } + + @Test(expected = RuntimeException.class) + public void parseToMap_DuplicateKey_ThrowsRuntimeException() { + String attributesModification = "+key1=value1,+key1=value2"; + AttributeParser.parseToMap(attributesModification); + } + + @Test + public void parseToString_EmptyMap_ReturnsEmptyString() { + Map attributes = new HashMap<>(); + String result = AttributeParser.parseToString(attributes); + assertEquals("", result); + } + + @Test + public void parseToString_ValidAttributes_ReturnsExpectedString() { + Map attributes = new HashMap<>(); + attributes.put("key1", "value1"); + attributes.put("key2", "value2"); + attributes.put("key3", ""); + + String result = AttributeParser.parseToString(attributes); + String expectedString = "key1=value1,key2=value2,key3"; + assertEquals(expectedString, result); + } + @Test public void testParseToMap() { Assert.assertEquals(0, AttributeParser.parseToMap(null).size()); diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeTest.java index 39a12b97ef4..9be0f31f06a 100644 --- a/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeTest.java @@ -17,12 +17,55 @@ package org.apache.rocketmq.common.attribute; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import static com.google.common.collect.Sets.newHashSet; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class AttributeTest { + private Attribute attribute; + + @Before + public void setUp() { + attribute = new Attribute("testAttribute", true) { + @Override + public void verify(String value) { + throw new UnsupportedOperationException(); + } + }; + } + + @Test + public void testGetName_ShouldReturnCorrectName() { + assertEquals("testAttribute", attribute.getName()); + } + + @Test + public void testSetName_ShouldSetCorrectName() { + attribute.setName("newTestAttribute"); + assertEquals("newTestAttribute", attribute.getName()); + } + + @Test + public void testIsChangeable_ShouldReturnCorrectChangeableStatus() { + assertTrue(attribute.isChangeable()); + } + + @Test + public void testSetChangeable_ShouldSetCorrectChangeableStatus() { + attribute.setChangeable(false); + assertFalse(attribute.isChangeable()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testVerify_ShouldThrowUnsupportedOperationException() { + attribute.verify("testValue"); + } + @Test public void testEnumAttribute() { EnumAttribute enumAttribute = new EnumAttribute("enum.key", true, newHashSet("enum-1", "enum-2", "enum-3"), "enum-1"); diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeUtilTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeUtilTest.java new file mode 100644 index 00000000000..aef46c16680 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/AttributeUtilTest.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class AttributeUtilTest { + + private Map allAttributes; + private ImmutableMap currentAttributes; + + @Before + public void setUp() { + allAttributes = new HashMap<>(); + allAttributes.put("attr1", new TestAttribute("value1", true, value -> true)); + allAttributes.put("attr2", new TestAttribute("value2", true, value -> true)); + allAttributes.put("attr3", new TestAttribute("value3", true, value -> value.equals("valid"))); + + currentAttributes = ImmutableMap.of("attr1", "value1", "attr2", "value2"); + } + + @Test + public void alterCurrentAttributes_CreateMode_ShouldReturnOnlyAddedAttributes() { + ImmutableMap newAttributes = ImmutableMap.of("+attr1", "new_value1", "+attr2", "value2", "+attr3", "value3"); + + Map result = AttributeUtil.alterCurrentAttributes(true, allAttributes, currentAttributes, newAttributes); + + assertEquals(3, result.size()); + assertTrue(result.containsKey("attr1")); + assertEquals("new_value1", result.get("attr1")); + assertTrue(result.containsKey("attr3")); + assertEquals("value3", result.get("attr3")); + assertTrue(result.containsKey("attr2")); + } + + @Test(expected = RuntimeException.class) + public void alterCurrentAttributes_CreateMode_AddNonAddableAttribute_ShouldThrowException() { + ImmutableMap newAttributes = ImmutableMap.of("attr1", "value1"); + AttributeUtil.alterCurrentAttributes(true, allAttributes, currentAttributes, newAttributes); + } + + @Test + public void alterCurrentAttributes_UpdateMode_ShouldReturnUpdatedAndAddedAttributes() { + ImmutableMap newAttributes = ImmutableMap.of("+attr1", "new_value1", "-attr2", "value2", "+attr3", "value3"); + + Map result = AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes); + + assertEquals(2, result.size()); + assertTrue(result.containsKey("attr1")); + assertEquals("new_value1", result.get("attr1")); + assertTrue(result.containsKey("attr3")); + assertEquals("value3", result.get("attr3")); + assertFalse(result.containsKey("attr2")); + } + + @Test(expected = RuntimeException.class) + public void alterCurrentAttributes_UpdateMode_DeleteNonExistentAttribute_ShouldThrowException() { + ImmutableMap newAttributes = ImmutableMap.of("-attr4", "value4"); + AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes); + } + + @Test(expected = RuntimeException.class) + public void alterCurrentAttributes_UpdateMode_WrongFormatKey_ShouldThrowException() { + ImmutableMap newAttributes = ImmutableMap.of("attr1", "+value1"); + AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes); + } + + @Test(expected = RuntimeException.class) + public void alterCurrentAttributes_UnsupportedKey_ShouldThrowException() { + ImmutableMap newAttributes = ImmutableMap.of("unsupported_attr", "value"); + AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes); + } + + @Test(expected = RuntimeException.class) + public void alterCurrentAttributes_AttemptToUpdateUnchangeableAttribute_ShouldThrowException() { + ImmutableMap newAttributes = ImmutableMap.of("attr2", "new_value2"); + AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes); + } + + private static class TestAttribute extends Attribute { + private final AttributeValidator validator; + + public TestAttribute(String name, boolean changeable, AttributeValidator validator) { + super(name, changeable); + this.validator = validator; + } + + @Override + public void verify(String value) { + validator.validate(value); + } + } + + private interface AttributeValidator { + boolean validate(String value); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/BooleanAttributeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/BooleanAttributeTest.java new file mode 100644 index 00000000000..6bed6ffac69 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/BooleanAttributeTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; + +public class BooleanAttributeTest { + + private BooleanAttribute booleanAttribute; + + @Before + public void setUp() { + booleanAttribute = new BooleanAttribute("testAttribute", true, false); + } + + @Test + public void testVerify_ValidValue_NoExceptionThrown() { + booleanAttribute.verify("true"); + booleanAttribute.verify("false"); + } + + @Test + public void testVerify_InvalidValue_ExceptionThrown() { + assertThrows(RuntimeException.class, () -> booleanAttribute.verify("invalid")); + assertThrows(RuntimeException.class, () -> booleanAttribute.verify("1")); + assertThrows(RuntimeException.class, () -> booleanAttribute.verify("0")); + assertThrows(RuntimeException.class, () -> booleanAttribute.verify("")); + } + + @Test + public void testGetDefaultValue() { + assertFalse(booleanAttribute.getDefaultValue()); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/CQTypeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/CQTypeTest.java new file mode 100644 index 00000000000..41aa98ba864 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/CQTypeTest.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CQTypeTest { + + @Test + public void testValues() { + CQType[] values = CQType.values(); + assertEquals(3, values.length); + assertEquals(CQType.SimpleCQ, values[0]); + assertEquals(CQType.BatchCQ, values[1]); + assertEquals(CQType.RocksDBCQ, values[2]); + } + + @Test + public void testValueOf() { + assertEquals(CQType.SimpleCQ, CQType.valueOf("SimpleCQ")); + assertEquals(CQType.BatchCQ, CQType.valueOf("BatchCQ")); + assertEquals(CQType.RocksDBCQ, CQType.valueOf("RocksDBCQ")); + } + + @Test(expected = IllegalArgumentException.class) + public void testValueOf_InvalidName() { + CQType.valueOf("InvalidCQ"); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/CleanupPolicyTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/CleanupPolicyTest.java new file mode 100644 index 00000000000..584de2b7e42 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/CleanupPolicyTest.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class CleanupPolicyTest { + + @Test + public void testCleanupPolicy_Delete() { + CleanupPolicy cleanupPolicy = CleanupPolicy.DELETE; + assertEquals("DELETE", cleanupPolicy.toString()); + } + + @Test + public void testCleanupPolicy_Compaction() { + CleanupPolicy cleanupPolicy = CleanupPolicy.COMPACTION; + assertEquals("COMPACTION", cleanupPolicy.toString()); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/EnumAttributeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/EnumAttributeTest.java new file mode 100644 index 00000000000..637dc302f52 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/EnumAttributeTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import org.junit.Before; +import org.junit.Test; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class EnumAttributeTest { + + private EnumAttribute enumAttribute; + + @Before + public void setUp() { + Set universe = new HashSet<>(); + universe.add("value1"); + universe.add("value2"); + universe.add("value3"); + + enumAttribute = new EnumAttribute("testAttribute", true, universe, "value1"); + } + + @Test + public void verify_ValidValue_NoExceptionThrown() { + enumAttribute.verify("value1"); + enumAttribute.verify("value2"); + enumAttribute.verify("value3"); + } + + @Test + public void verify_InvalidValue_ExceptionThrown() { + RuntimeException exception = assertThrows(RuntimeException.class, () -> { + enumAttribute.verify("invalidValue"); + }); + + assertTrue(exception.getMessage().startsWith("value is not in set:")); + } + + @Test + public void getDefaultValue_ReturnsDefaultValue() { + assertEquals("value1", enumAttribute.getDefaultValue()); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/LongRangeAttributeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/LongRangeAttributeTest.java new file mode 100644 index 00000000000..222f9092d54 --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/LongRangeAttributeTest.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.rocketmq.common.attribute; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class LongRangeAttributeTest { + + private LongRangeAttribute longRangeAttribute; + + @Before + public void setUp() { + longRangeAttribute = new LongRangeAttribute("testAttribute", true, 0, 100, 50); + } + + @Test + public void verify_ValidValue_NoExceptionThrown() { + longRangeAttribute.verify("50"); + } + + @Test + public void verify_MinValue_NoExceptionThrown() { + longRangeAttribute.verify("0"); + } + + @Test + public void verify_MaxValue_NoExceptionThrown() { + longRangeAttribute.verify("100"); + } + + @Test + public void verify_ValueLessThanMin_ThrowsRuntimeException() { + RuntimeException exception = assertThrows(RuntimeException.class, () -> longRangeAttribute.verify("-1")); + assertEquals("value is not in range(0, 100)", exception.getMessage()); + } + + @Test + public void verify_ValueGreaterThanMax_ThrowsRuntimeException() { + RuntimeException exception = assertThrows(RuntimeException.class, () -> longRangeAttribute.verify("101")); + assertEquals("value is not in range(0, 100)", exception.getMessage()); + } + + @Test + public void getDefaultValue_ReturnsDefaultValue() { + assertEquals(50, longRangeAttribute.getDefaultValue()); + } +} diff --git a/common/src/test/java/org/apache/rocketmq/common/attribute/TopicMessageTypeTest.java b/common/src/test/java/org/apache/rocketmq/common/attribute/TopicMessageTypeTest.java index 67525ae8087..0321679ccc0 100644 --- a/common/src/test/java/org/apache/rocketmq/common/attribute/TopicMessageTypeTest.java +++ b/common/src/test/java/org/apache/rocketmq/common/attribute/TopicMessageTypeTest.java @@ -16,13 +16,76 @@ */ package org.apache.rocketmq.common.attribute; +import com.google.common.collect.Sets; import java.util.HashMap; import java.util.Map; +import java.util.Set; import org.apache.rocketmq.common.message.MessageConst; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class TopicMessageTypeTest { + + private Map normalMessageProperty; + private Map transactionMessageProperty; + private Map delayMessageProperty; + private Map fifoMessageProperty; + + @Before + public void setUp() { + normalMessageProperty = new HashMap<>(); + transactionMessageProperty = new HashMap<>(); + delayMessageProperty = new HashMap<>(); + fifoMessageProperty = new HashMap<>(); + + transactionMessageProperty.put(MessageConst.PROPERTY_TRANSACTION_PREPARED, "true"); + delayMessageProperty.put(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "1"); + fifoMessageProperty.put(MessageConst.PROPERTY_SHARDING_KEY, "shardingKey"); + } + + @Test + public void testTopicMessageTypeSet() { + Set expectedSet = Sets.newHashSet("UNSPECIFIED", "NORMAL", "FIFO", "DELAY", "TRANSACTION", "MIXED"); + Set actualSet = TopicMessageType.topicMessageTypeSet(); + assertEquals(expectedSet, actualSet); + } + + @Test + public void testParseFromMessageProperty_Normal() { + TopicMessageType actual = TopicMessageType.parseFromMessageProperty(normalMessageProperty); + assertEquals(TopicMessageType.NORMAL, actual); + } + + @Test + public void testParseFromMessageProperty_Transaction() { + TopicMessageType actual = TopicMessageType.parseFromMessageProperty(transactionMessageProperty); + assertEquals(TopicMessageType.TRANSACTION, actual); + } + + @Test + public void testParseFromMessageProperty_Delay() { + TopicMessageType actual = TopicMessageType.parseFromMessageProperty(delayMessageProperty); + assertEquals(TopicMessageType.DELAY, actual); + } + + @Test + public void testParseFromMessageProperty_Fifo() { + TopicMessageType actual = TopicMessageType.parseFromMessageProperty(fifoMessageProperty); + assertEquals(TopicMessageType.FIFO, actual); + } + + @Test + public void testGetMetricsValue() { + for (TopicMessageType type : TopicMessageType.values()) { + String expected = type.getValue().toLowerCase(); + String actual = type.getMetricsValue(); + assertEquals(expected, actual); + } + } + @Test public void testParseFromMessageProperty() { Map properties = new HashMap<>();