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 #5032] add ut for package com.alibaba.nacos.client.config.filter #5046

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.filter.impl;

import org.junit.Assert;
import org.junit.Test;

public class ConfigContextTest {

@Test
public void testParameter() {
ConfigContext context = new ConfigContext();
String key = "key";
String v = "v";
context.setParameter(key, v);

String actual = (String) context.getParameter(key);

Assert.assertEquals(v, actual);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.filter.impl;

import com.alibaba.nacos.api.config.filter.IConfigContext;
import com.alibaba.nacos.api.config.filter.IConfigFilter;
import com.alibaba.nacos.api.config.filter.IConfigFilterChain;
import com.alibaba.nacos.api.config.filter.IConfigRequest;
import com.alibaba.nacos.api.config.filter.IConfigResponse;
import com.alibaba.nacos.api.config.filter.IFilterConfig;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ConfigFilterChainManagerTest {

private static class MyIConfigFilter implements IConfigFilter {

private String name;

private int order;

public MyIConfigFilter(String name, int order) {
this.name = name;
this.order = order;
}

@Override
public void init(IFilterConfig filterConfig) {
}

@Override
public void doFilter(IConfigRequest request, IConfigResponse response, IConfigFilterChain filterChain)
throws NacosException {
IConfigContext configContext = request.getConfigContext();
// save filter info
configContext.setParameter(name, order);

// save filter order
if (configContext.getParameter("orders") == null) {
configContext.setParameter("orders", new ArrayList<Integer>());
}
List<Integer> orders = (List<Integer>) configContext.getParameter("orders");
orders.add(order);

// save filter count
if (configContext.getParameter("filterCount") == null) {
configContext.setParameter("filterCount", 0);
}
Integer filterCount = (Integer) configContext.getParameter("filterCount");
filterCount = filterCount + 1;
configContext.setParameter("filterCount", filterCount);

// do next
filterChain.doFilter(request, response);
}

@Override
public void deploy() {
}

@Override
public int getOrder() {
return order;
}

@Override
public String getFilterName() {
return name;
}
}

@Test
public void testAddFilterOrder() throws NacosException {
final ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager();
MyIConfigFilter filter1 = new MyIConfigFilter("filter1", 1);
MyIConfigFilter filter2 = new MyIConfigFilter("filter2", 2);
MyIConfigFilter filter3 = new MyIConfigFilter("filter3", 3);

//random order
configFilterChainManager.addFilter(filter2);
configFilterChainManager.addFilter(filter1);
configFilterChainManager.addFilter(filter3);

ConfigRequest configRequest = new ConfigRequest();

configFilterChainManager.doFilter(configRequest, new ConfigResponse());

IConfigContext configContext = configRequest.getConfigContext();

// doFilter works
Assert.assertEquals(1, configContext.getParameter("filter1"));
Assert.assertEquals(2, configContext.getParameter("filter2"));
Assert.assertEquals(3, configContext.getParameter("filter3"));

//order
List<Integer> orders = (List<Integer>) configContext.getParameter("orders");
Assert.assertEquals(Arrays.asList(1, 2, 3), orders);
}

@Test
public void testAddFilterNotRepeat() throws NacosException {
final ConfigFilterChainManager configFilterChainManager = new ConfigFilterChainManager();
MyIConfigFilter filter1 = new MyIConfigFilter("filter1", 1);
MyIConfigFilter filter2 = new MyIConfigFilter("filter2", 2);
MyIConfigFilter repeatFilter = new MyIConfigFilter("filter1", 1);

configFilterChainManager.addFilter(filter2);
configFilterChainManager.addFilter(filter1);
configFilterChainManager.addFilter(repeatFilter);

ConfigRequest configRequest = new ConfigRequest();
configFilterChainManager.doFilter(configRequest, new ConfigResponse());

IConfigContext configContext = configRequest.getConfigContext();

Assert.assertEquals(2, configContext.getParameter("filterCount"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.filter.impl;

import com.alibaba.nacos.api.config.filter.IConfigContext;
import org.junit.Assert;
import org.junit.Test;

public class ConfigRequestTest {

@Test
public void testGetterAndSetter() {
ConfigRequest configRequest = new ConfigRequest();
String dataId = "id";
String group = "group";
String tenant = "n";
String content = "abc";
String type = "properties";

configRequest.setContent(content);
configRequest.setDataId(dataId);
configRequest.setGroup(group);
configRequest.setTenant(tenant);
configRequest.setType(type);

Assert.assertEquals(dataId, configRequest.getDataId());
Assert.assertEquals(group, configRequest.getGroup());
Assert.assertEquals(tenant, configRequest.getTenant());
Assert.assertEquals(content, configRequest.getContent());
//TODO fix getType
//Assert.assertEquals(type, configRequest.getType());

}

@Test
public void testGetParameter() {
ConfigRequest configRequest = new ConfigRequest();
String dataId = "id";
String group = "group";
String tenant = "n";
String content = "abc";

configRequest.setContent(content);
configRequest.setDataId(dataId);
configRequest.setGroup(group);
configRequest.setTenant(tenant);

Assert.assertEquals(dataId, configRequest.getParameter("dataId"));
Assert.assertEquals(group, configRequest.getParameter("group"));
Assert.assertEquals(tenant, configRequest.getParameter("tenant"));
Assert.assertEquals(content, configRequest.getParameter("content"));
}

@Test
public void testGetConfigContext() {
ConfigRequest configRequest = new ConfigRequest();
IConfigContext configContext = configRequest.getConfigContext();
Assert.assertNotNull(configContext);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.client.config.filter.impl;

import com.alibaba.nacos.api.config.filter.IConfigContext;
import org.junit.Assert;
import org.junit.Test;

public class ConfigResponseTest {

@Test
public void testGetterAndSetter() {
ConfigResponse configResponse = new ConfigResponse();
String dataId = "id";
String group = "group";
String tenant = "n";
String content = "abc";

configResponse.setContent(content);
configResponse.setDataId(dataId);
configResponse.setGroup(group);
configResponse.setTenant(tenant);

Assert.assertEquals(dataId, configResponse.getDataId());
Assert.assertEquals(group, configResponse.getGroup());
Assert.assertEquals(tenant, configResponse.getTenant());
Assert.assertEquals(content, configResponse.getContent());

}

@Test
public void getParameter() {
ConfigResponse configResponse = new ConfigResponse();
String dataId = "id";
String group = "group";
String tenant = "n";
String content = "abc";

configResponse.setContent(content);
configResponse.setDataId(dataId);
configResponse.setGroup(group);
configResponse.setTenant(tenant);

Assert.assertEquals(dataId, configResponse.getParameter("dataId"));
Assert.assertEquals(group, configResponse.getParameter("group"));
Assert.assertEquals(tenant, configResponse.getParameter("tenant"));
Assert.assertEquals(content, configResponse.getParameter("content"));
}

@Test
public void getConfigContext() {
ConfigResponse configResponse = new ConfigResponse();
IConfigContext configContext = configResponse.getConfigContext();
Assert.assertNotNull(configContext);
}
}