Skip to content

Commit

Permalink
Fixed #fixed incorrect use list.remove method. (#10935)
Browse files Browse the repository at this point in the history
* Fixed #fixed incorrect use list.remove method.

* Fixed add License to test file.
  • Loading branch information
mattisonchao committed Jun 19, 2021
1 parent fec6c58 commit 98d2c54
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.admin.cli.utils.NameValueParameterSplitter;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.PulsarAdminException;
import org.apache.pulsar.common.policies.data.AutoFailoverPolicyData;
import org.apache.pulsar.common.policies.data.AutoFailoverPolicyDataImpl;
import org.apache.pulsar.common.policies.data.AutoFailoverPolicyType;
import org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl;
import org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData;
Expand Down Expand Up @@ -152,12 +153,9 @@ void run() throws PulsarAdminException {
}

private List<String> validateList(List<String> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).isEmpty()) {
list.remove(i);
}
}
return list;
return list.stream()
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
}

private NamespaceIsolationData createNamespaceIsolationData(List<String> namespaces, List<String> primary,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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.pulsar.admin.cli;

import com.google.common.collect.Lists;
import org.junit.Test;
import org.testng.Assert;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;


public class TestCmdNamespaceIsolationPolicy {
@Test
public void testValidateListMethodToReturnNonNullStringList() {
List<String> mockList = Lists.newArrayList("", "1", "", "", "1", "2", "3", "4", "", "", "", "1", "");
List<String> resultList = Lists.newArrayList("1", "1", "2", "3", "4", "1");
CmdNamespaceIsolationPolicy cmdNamespaceIsolationPolicy = new CmdNamespaceIsolationPolicy(() -> null);
Class<? extends CmdNamespaceIsolationPolicy> klass = cmdNamespaceIsolationPolicy.getClass();
Arrays.stream(klass.getDeclaredMethods())
.filter((innerMethod) -> innerMethod.getName().contains("validateList"))
.findFirst().ifPresent(innerMethod -> {
try {
innerMethod.setAccessible(true);
List<String> calculatedList = (List<String>) innerMethod.invoke(cmdNamespaceIsolationPolicy, mockList);
Assert.assertEquals(calculatedList.size(), resultList.size());
for (int i = 0; i < resultList.size(); i++)
Assert.assertEquals(resultList.get(i), calculatedList.get(i));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
});
}
}

0 comments on commit 98d2c54

Please sign in to comment.