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 #8417]add some test cases for org.apache.rocketmq.common.AclConfig #8418

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions common/src/test/java/org/apache/rocketmq/common/AclConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class AclConfigTest {

/**
* Test to verify that getGlobalWhiteAddrs returns null when no addresses have been set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test classes do not require method comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to remove all method level comments?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's not necessary in test class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, why does my check item show "7 workflows awaiting approval"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, it's first contribution, need the approval of the committer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, I've modified it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to bother you again, is codecov-commenter triggered automatically or do you need to trigger it manually?
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci automatically triggered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci automatically triggered.

Thank you very much

*
*/
@Test
public void testGetGlobalWhiteAddrsWhenNull() {
AclConfig aclConfig = new AclConfig();
Assert.assertNull("The globalWhiteAddrs should return null", aclConfig.getGlobalWhiteAddrs());
}

/**
* Test to verify that getGlobalWhiteAddrs returns an empty list when an empty list is set.
*/
@Test
public void testGetGlobalWhiteAddrsWhenEmpty() {
AclConfig aclConfig = new AclConfig();
List<String> globalWhiteAddrs = new ArrayList<>();
aclConfig.setGlobalWhiteAddrs(globalWhiteAddrs);
assertNotNull("The globalWhiteAddrs should never return null", aclConfig.getGlobalWhiteAddrs());
assertEquals("The globalWhiteAddrs list should be empty", 0, aclConfig.getGlobalWhiteAddrs().size());
}

/**
* Test to verify that getGlobalWhiteAddrs correctly returns a list of set global white addresses.
*/
@Test
public void testGetGlobalWhiteAddrs() {
AclConfig aclConfig = new AclConfig();
List<String> expected = Arrays.asList("192.168.1.1", "192.168.1.2");
aclConfig.setGlobalWhiteAddrs(expected);
assertEquals("Global white addresses should match", expected, aclConfig.getGlobalWhiteAddrs());
assertEquals("The globalWhiteAddrs list should be equal to 2", 2, aclConfig.getGlobalWhiteAddrs().size());
}

/**
* Test to verify that getPlainAccessConfigs returns null when no configurations have been set.
*/
@Test
public void testGetPlainAccessConfigsWhenNull() {
AclConfig aclConfig = new AclConfig();
Assert.assertNull("The plainAccessConfigs should return null", aclConfig.getPlainAccessConfigs());
}

/**
* Test to verify that getPlainAccessConfigs returns an empty list when an empty list is set.
*/
@Test
public void testGetPlainAccessConfigsWhenEmpty() {
AclConfig aclConfig = new AclConfig();
List<PlainAccessConfig> plainAccessConfigs = new ArrayList<>();
aclConfig.setPlainAccessConfigs(plainAccessConfigs);
assertNotNull("The plainAccessConfigs should never return null", aclConfig.getPlainAccessConfigs());
assertEquals("The plainAccessConfigs list should be empty", 0, aclConfig.getPlainAccessConfigs().size());
}

/**
* Test to verify that getPlainAccessConfigs correctly returns a list of set plain access configurations.
*/
@Test
public void testGetPlainAccessConfigs() {
AclConfig aclConfig = new AclConfig();
List<PlainAccessConfig> expected = Arrays.asList(new PlainAccessConfig(), new PlainAccessConfig());
aclConfig.setPlainAccessConfigs(expected);
assertEquals("Plain access configs should match", expected, aclConfig.getPlainAccessConfigs());
assertEquals("The plainAccessConfigs list should be equal to 2", 2, aclConfig.getPlainAccessConfigs().size());
}

/**
* Test the toString method when global white addresses and plain access configurations are null.
*/
@Test
public void testToStringWithNullValues() {
AclConfig aclConfig = new AclConfig();
String result = aclConfig.toString();
assertNotNull("toString should not be null", result);
assertEquals("toString should match", "AclConfig{globalWhiteAddrs=null, plainAccessConfigs=null}", result);
}

@Test
public void testToStringWithEmptyGlobalWhiteAddrsAndPlainAccessConfigs() {
AclConfig aclConfig = new AclConfig();
aclConfig.setGlobalWhiteAddrs(Collections.emptyList());
aclConfig.setPlainAccessConfigs(Collections.emptyList());
String expected = "AclConfig{globalWhiteAddrs=[], plainAccessConfigs=[]}";
assertEquals(expected, aclConfig.toString());
}

/**
* Test the toString method to ensure it correctly represents the state of the AclConfig instance.
*/
@Test
public void testToStringWithNonEmptyGlobalWhiteAddrsAndPlainAccessConfigs() {
AclConfig aclConfig = new AclConfig();
List<String> globalWhiteAddrs = Collections.singletonList("192.168.1.1");
aclConfig.setGlobalWhiteAddrs(globalWhiteAddrs);
PlainAccessConfig plainAccessConfig = new PlainAccessConfig();
List<PlainAccessConfig> plainAccessConfigs = Collections.singletonList(plainAccessConfig);
aclConfig.setPlainAccessConfigs(plainAccessConfigs);
String expected = "AclConfig{globalWhiteAddrs=[192.168.1.1], plainAccessConfigs=[" + plainAccessConfig + "]}";
assertEquals("toString should match", expected, aclConfig.toString());
}
}