Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #8476] Add test cases for org.apache.rocketmq.common.attribute #8477

Merged
merged 8 commits into from
Aug 5, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> result = AttributeParser.parseToMap(attributesModification);
assertTrue(result.isEmpty());
}

@Test
public void parseToMap_NullString_ReturnsEmptyMap() {
String attributesModification = null;
Map<String, String> result = AttributeParser.parseToMap(attributesModification);
assertTrue(result.isEmpty());
}

@Test
public void parseToMap_ValidAttributesModification_ReturnsExpectedMap() {
String attributesModification = "+key1=value1,+key2=value2,-key3,+key4=value4";
Map<String, String> result = AttributeParser.parseToMap(attributesModification);

Map<String, String> 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<String, String> attributes = new HashMap<>();
String result = AttributeParser.parseToString(attributes);
assertEquals("", result);
}

@Test
public void parseToString_ValidAttributes_ReturnsExpectedString() {
Map<String, String> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Attribute> allAttributes;
private ImmutableMap<String, String> 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<String, String> newAttributes = ImmutableMap.of("+attr1", "new_value1", "+attr2", "value2", "+attr3", "value3");

Map<String, String> 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<String, String> newAttributes = ImmutableMap.of("attr1", "value1");
AttributeUtil.alterCurrentAttributes(true, allAttributes, currentAttributes, newAttributes);
}

@Test
public void alterCurrentAttributes_UpdateMode_ShouldReturnUpdatedAndAddedAttributes() {
ImmutableMap<String, String> newAttributes = ImmutableMap.of("+attr1", "new_value1", "-attr2", "value2", "+attr3", "value3");

Map<String, String> 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<String, String> newAttributes = ImmutableMap.of("-attr4", "value4");
AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes);
}

@Test(expected = RuntimeException.class)
public void alterCurrentAttributes_UpdateMode_WrongFormatKey_ShouldThrowException() {
ImmutableMap<String, String> newAttributes = ImmutableMap.of("attr1", "+value1");
AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes);
}

@Test(expected = RuntimeException.class)
public void alterCurrentAttributes_UnsupportedKey_ShouldThrowException() {
ImmutableMap<String, String> newAttributes = ImmutableMap.of("unsupported_attr", "value");
AttributeUtil.alterCurrentAttributes(false, allAttributes, currentAttributes, newAttributes);
}

@Test(expected = RuntimeException.class)
public void alterCurrentAttributes_AttemptToUpdateUnchangeableAttribute_ShouldThrowException() {
ImmutableMap<String, String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading
Loading