Add unit test for api module (#10241)
* Add more situation UT for NetUtils * Add UT for api module selector package * Add UT for api module exception package * Add UT for api module cmdb package
This commit is contained in:
parent
2bca057137
commit
ced2f1d9ed
@ -71,8 +71,4 @@ public class NacosApiException extends NacosException {
|
||||
}
|
||||
return Constants.NULL;
|
||||
}
|
||||
|
||||
public void setErrAbstract(String errAbstract) {
|
||||
this.errAbstract = errAbstract;
|
||||
}
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ public class NacosRuntimeException extends RuntimeException {
|
||||
|
||||
public static final String ERROR_MESSAGE_FORMAT = "errCode: %d, errMsg: %s ";
|
||||
|
||||
private int errCode;
|
||||
private final int errCode;
|
||||
|
||||
public NacosRuntimeException(int errCode) {
|
||||
super();
|
||||
@ -52,8 +52,4 @@ public class NacosRuntimeException extends RuntimeException {
|
||||
public int getErrCode() {
|
||||
return errCode;
|
||||
}
|
||||
|
||||
public void setErrCode(int errCode) {
|
||||
this.errCode = errCode;
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,8 @@ import static com.alibaba.nacos.api.common.Constants.Naming.CMDB_CONTEXT_TYPE;
|
||||
*/
|
||||
public abstract class AbstractCmdbSelector<T extends Instance> implements Selector<List<T>, CmdbContext<T>, String> {
|
||||
|
||||
private static final long serialVersionUID = 56587385358330901L;
|
||||
|
||||
/**
|
||||
* the labels expression.
|
||||
*/
|
||||
|
@ -16,7 +16,6 @@
|
||||
|
||||
package com.alibaba.nacos.api.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
@ -93,8 +92,7 @@ public class NetUtils {
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
//ignore
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
|
||||
if (result != null) {
|
||||
@ -103,8 +101,7 @@ public class NetUtils {
|
||||
|
||||
try {
|
||||
return InetAddress.getLocalHost();
|
||||
} catch (UnknownHostException e) {
|
||||
//ignore
|
||||
} catch (UnknownHostException ignore) {
|
||||
}
|
||||
|
||||
return null;
|
||||
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.cmdb.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class EntityEventTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
EntityEvent entity = new EntityEvent();
|
||||
entity.setEntityName("test-entity");
|
||||
entity.setEntityType("CMDB");
|
||||
entity.setType(EntityEventType.ENTITY_ADD_OR_UPDATE);
|
||||
String actual = mapper.writeValueAsString(entity);
|
||||
System.out.println(actual);
|
||||
assertTrue(actual.contains("\"entityName\":\"test-entity\""));
|
||||
assertTrue(actual.contains("\"entityType\":\"CMDB\""));
|
||||
assertTrue(actual.contains("\"type\":\"ENTITY_ADD_OR_UPDATE\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialization() throws JsonProcessingException {
|
||||
String json = "{\"type\":\"ENTITY_REMOVE\",\"entityName\":\"test-entity\",\"entityType\":\"CMDB\"}";
|
||||
EntityEvent entity = mapper.readValue(json, EntityEvent.class);
|
||||
assertEquals("test-entity", entity.getEntityName());
|
||||
assertEquals("CMDB", entity.getEntityType());
|
||||
assertEquals(EntityEventType.ENTITY_REMOVE, entity.getType());
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.cmdb.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class EntityTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
Entity entity = new Entity();
|
||||
entity.setName("test-entity");
|
||||
entity.setType(PreservedEntityTypes.ip.name());
|
||||
entity.setLabels(Collections.singletonMap("test-label-key", "test-label-value"));
|
||||
String actual = mapper.writeValueAsString(entity);
|
||||
assertTrue(actual.contains("\"type\":\"ip\""));
|
||||
assertTrue(actual.contains("\"name\":\"test-entity\""));
|
||||
assertTrue(actual.contains("\"labels\":{\"test-label-key\":\"test-label-value\"}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialization() throws JsonProcessingException {
|
||||
String json = "{\"type\":\"service\",\"name\":\"test-entity\",\"labels\":{\"test-label-key\":\"test-label-value\"}}";
|
||||
Entity entity = mapper.readValue(json, Entity.class);
|
||||
assertEquals("test-entity", entity.getName());
|
||||
assertEquals(PreservedEntityTypes.service.name(), entity.getType());
|
||||
assertEquals(1, entity.getLabels().size());
|
||||
assertTrue(entity.getLabels().containsKey("test-label-key"));
|
||||
assertEquals("test-label-value", entity.getLabels().get("test-label-key"));
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.cmdb.pojo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class LabelTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
Label label = new Label();
|
||||
label.setName("test-label");
|
||||
label.setDescription("CMDB description");
|
||||
label.setValues(Collections.singletonMap("test-value", "test-value").keySet());
|
||||
String actual = mapper.writeValueAsString(label);
|
||||
System.out.println(actual);
|
||||
assertTrue(actual.contains("\"name\":\"test-label\""));
|
||||
assertTrue(actual.contains("\"description\":\"CMDB description\""));
|
||||
assertTrue(actual.contains("\"values\":[\"test-value\"]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialization() throws JsonProcessingException {
|
||||
String json = "{\"values\":[\"test-value\"],\"name\":\"test-label\",\"description\":\"CMDB description\"}";
|
||||
Label label = mapper.readValue(json, Label.class);
|
||||
assertEquals("test-label", label.getName());
|
||||
assertEquals("CMDB description", label.getDescription());
|
||||
assertEquals(1, label.getValues().size());
|
||||
assertEquals("test-value", label.getValues().iterator().next());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class NacosExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyConstructor() {
|
||||
NacosException exception = new NacosException();
|
||||
assertEquals(0, exception.getErrCode());
|
||||
assertEquals(Constants.NULL, exception.getErrMsg());
|
||||
assertEquals("ErrCode:0, ErrMsg:", exception.toString());
|
||||
exception.setErrCode(NacosException.INVALID_PARAM);
|
||||
exception.setErrMsg("test");
|
||||
assertEquals("ErrCode:400, ErrMsg:test", exception.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithErrMsg() {
|
||||
NacosException exception = new NacosException(NacosException.SERVER_ERROR, "test");
|
||||
assertEquals(NacosException.SERVER_ERROR, exception.getErrCode());
|
||||
assertEquals("test", exception.getErrMsg());
|
||||
assertEquals("ErrCode:500, ErrMsg:test", exception.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithCause() {
|
||||
NacosException exception = new NacosException(NacosException.SERVER_ERROR, new RuntimeException("cause test"));
|
||||
assertEquals(NacosException.SERVER_ERROR, exception.getErrCode());
|
||||
assertEquals("cause test", exception.getErrMsg());
|
||||
assertEquals("ErrCode:500, ErrMsg:cause test", exception.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithMultiCauses() {
|
||||
NacosException exception = new NacosException(NacosException.SERVER_ERROR,
|
||||
new RuntimeException("cause test", new RuntimeException("multi")));
|
||||
assertEquals(NacosException.SERVER_ERROR, exception.getErrCode());
|
||||
assertEquals("multi", exception.getErrMsg());
|
||||
assertEquals("ErrCode:500, ErrMsg:multi", exception.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithFull() {
|
||||
NacosException exception = new NacosException(NacosException.SERVER_ERROR, "test",
|
||||
new RuntimeException("cause test"));
|
||||
assertEquals(NacosException.SERVER_ERROR, exception.getErrCode());
|
||||
assertEquals("test", exception.getErrMsg());
|
||||
assertEquals("ErrCode:500, ErrMsg:test", exception.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception.api;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.alibaba.nacos.api.model.v2.ErrorCode;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class NacosApiExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyConstructor() {
|
||||
NacosApiException exception = new NacosApiException();
|
||||
assertEquals(0, exception.getErrCode());
|
||||
assertEquals(0, exception.getDetailErrCode());
|
||||
assertEquals(Constants.NULL, exception.getErrMsg());
|
||||
assertEquals(Constants.NULL, exception.getErrAbstract());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithoutCause() {
|
||||
NacosApiException exception = new NacosApiException(500, ErrorCode.SERVER_ERROR, "test");
|
||||
assertEquals(500, exception.getErrCode());
|
||||
assertEquals(ErrorCode.SERVER_ERROR.getCode().intValue(), exception.getDetailErrCode());
|
||||
assertEquals("test", exception.getErrMsg());
|
||||
assertEquals(ErrorCode.SERVER_ERROR.getMsg(), exception.getErrAbstract());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithCause() {
|
||||
NacosApiException exception = new NacosApiException(500, ErrorCode.SERVER_ERROR,
|
||||
new RuntimeException("cause test"), "test");
|
||||
assertEquals(500, exception.getErrCode());
|
||||
assertEquals(ErrorCode.SERVER_ERROR.getCode().intValue(), exception.getDetailErrCode());
|
||||
assertEquals("test", exception.getErrMsg());
|
||||
assertEquals(ErrorCode.SERVER_ERROR.getMsg(), exception.getErrAbstract());
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception.runtime;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import com.fasterxml.jackson.databind.type.SimpleType;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class NacosDeserializationExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyConstructor() {
|
||||
NacosDeserializationException exception = new NacosDeserializationException();
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertNull(exception.getMessage());
|
||||
assertNull(exception.getTargetClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithTargetClass() {
|
||||
NacosDeserializationException exception = new NacosDeserializationException(
|
||||
NacosDeserializationExceptionTest.class);
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(String.format("errCode: 101, errMsg: Nacos deserialize for class [%s] failed. ",
|
||||
NacosDeserializationExceptionTest.class.getName()), exception.getMessage());
|
||||
assertEquals(NacosDeserializationExceptionTest.class, exception.getTargetClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithTargetType() {
|
||||
Type type = SimpleType.constructUnsafe(NacosDeserializationExceptionTest.class);
|
||||
NacosDeserializationException exception = new NacosDeserializationException(type);
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(
|
||||
String.format("errCode: 101, errMsg: Nacos deserialize for class [%s] failed. ", type.getTypeName()),
|
||||
exception.getMessage());
|
||||
assertNull(exception.getTargetClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithCause() {
|
||||
NacosDeserializationException exception = new NacosDeserializationException(new RuntimeException("test"));
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals("errCode: 101, errMsg: Nacos deserialize failed. ", exception.getMessage());
|
||||
assertNull(exception.getTargetClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithTargetClassAndCause() {
|
||||
NacosDeserializationException exception = new NacosDeserializationException(
|
||||
NacosDeserializationExceptionTest.class, new RuntimeException("test"));
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(String.format("errCode: 101, errMsg: Nacos deserialize for class [%s] failed, cause error[%s]. ",
|
||||
NacosDeserializationExceptionTest.class.getName(), "test"), exception.getMessage());
|
||||
assertEquals(NacosDeserializationExceptionTest.class, exception.getTargetClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithTargetTypeAndCause() {
|
||||
Type type = SimpleType.constructUnsafe(NacosDeserializationExceptionTest.class);
|
||||
NacosDeserializationException exception = new NacosDeserializationException(type, new RuntimeException("test"));
|
||||
assertEquals(Constants.Exception.DESERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(String.format("errCode: 101, errMsg: Nacos deserialize for class [%s] failed, cause error[%s]. ",
|
||||
type.getTypeName(), "test"), exception.getMessage());
|
||||
assertNull(exception.getTargetClass());
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception.runtime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class NacosLoadExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testConstructor() {
|
||||
NacosLoadException exception = new NacosLoadException("test");
|
||||
assertEquals("test", exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception.runtime;
|
||||
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class NacosRuntimeExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testConstructorWithErrorCode() {
|
||||
NacosRuntimeException exception = new NacosRuntimeException(NacosException.INVALID_PARAM);
|
||||
assertEquals(NacosException.INVALID_PARAM, exception.getErrCode());
|
||||
assertNull(exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithErrorCodeAndMsg() {
|
||||
NacosRuntimeException exception = new NacosRuntimeException(NacosException.INVALID_PARAM, "test");
|
||||
assertEquals(NacosException.INVALID_PARAM, exception.getErrCode());
|
||||
assertEquals("errCode: 400, errMsg: test ", exception.getMessage());
|
||||
assertNull(exception.getCause());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithErrorCodeAndCause() {
|
||||
NacosRuntimeException exception = new NacosRuntimeException(NacosException.INVALID_PARAM,
|
||||
new RuntimeException("test"));
|
||||
assertEquals(NacosException.INVALID_PARAM, exception.getErrCode());
|
||||
assertEquals("java.lang.RuntimeException: test", exception.getMessage());
|
||||
assertTrue(exception.getCause() instanceof RuntimeException);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithFull() {
|
||||
NacosRuntimeException exception = new NacosRuntimeException(NacosException.INVALID_PARAM,
|
||||
"test", new RuntimeException("cause test"));
|
||||
assertEquals(NacosException.INVALID_PARAM, exception.getErrCode());
|
||||
assertEquals("errCode: 400, errMsg: test ", exception.getMessage());
|
||||
assertTrue(exception.getCause() instanceof RuntimeException);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.exception.runtime;
|
||||
|
||||
import com.alibaba.nacos.api.common.Constants;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class NacosSerializationExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testEmptyConstructor() {
|
||||
NacosSerializationException exception = new NacosSerializationException();
|
||||
assertEquals(Constants.Exception.SERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertNull(exception.getMessage());
|
||||
assertNull(exception.getSerializedClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithSerializedClass() {
|
||||
NacosSerializationException exception = new NacosSerializationException(NacosSerializationExceptionTest.class);
|
||||
assertEquals(Constants.Exception.SERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(String.format("errCode: 100, errMsg: Nacos serialize for class [%s] failed. ",
|
||||
NacosSerializationExceptionTest.class.getName()), exception.getMessage());
|
||||
assertEquals(NacosSerializationExceptionTest.class, exception.getSerializedClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithCause() {
|
||||
NacosSerializationException exception = new NacosSerializationException(new RuntimeException("test"));
|
||||
assertEquals(Constants.Exception.SERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals("errCode: 100, errMsg: Nacos serialize failed. ", exception.getMessage());
|
||||
assertNull(exception.getSerializedClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructorWithSerializedClassAndCause() {
|
||||
NacosSerializationException exception = new NacosSerializationException(NacosSerializationExceptionTest.class,
|
||||
new RuntimeException("test"));
|
||||
assertEquals(Constants.Exception.SERIALIZE_ERROR_CODE, exception.getErrCode());
|
||||
assertEquals(String.format("errCode: 100, errMsg: Nacos serialize for class [%s] failed. ",
|
||||
NacosSerializationExceptionTest.class.getName(), "test"), exception.getMessage());
|
||||
assertEquals(NacosSerializationExceptionTest.class, exception.getSerializedClass());
|
||||
}
|
||||
}
|
@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.selector;
|
||||
|
||||
import com.alibaba.nacos.api.cmdb.pojo.Entity;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import com.alibaba.nacos.api.selector.context.CmdbContext;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static com.alibaba.nacos.api.common.Constants.Naming.CMDB_CONTEXT_TYPE;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class AbstractCmdbSelectorTest {
|
||||
|
||||
private AtomicInteger counter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
counter = new AtomicInteger();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetExpression() {
|
||||
MockCmdbSelector cmdbSelector = new MockCmdbSelector();
|
||||
assertNull(cmdbSelector.getExpression());
|
||||
cmdbSelector.setExpression("test");
|
||||
assertEquals("test", cmdbSelector.getExpression());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParse() throws NacosException {
|
||||
MockCmdbSelector cmdbSelector = new MockCmdbSelector();
|
||||
cmdbSelector.parse("test");
|
||||
assertEquals("test", cmdbSelector.getExpression());
|
||||
assertEquals(1, counter.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelect() {
|
||||
CmdbContext<Instance> context = new CmdbContext<>();
|
||||
CmdbContext.CmdbInstance<Instance> provider = new CmdbContext.CmdbInstance<>();
|
||||
provider.setInstance(new Instance());
|
||||
provider.setEntity(new Entity());
|
||||
context.setProviders(Collections.singletonList(provider));
|
||||
CmdbContext.CmdbInstance<Instance> consumer = new CmdbContext.CmdbInstance<>();
|
||||
consumer.setInstance(new Instance());
|
||||
consumer.setEntity(new Entity());
|
||||
context.setConsumer(consumer);
|
||||
List<Instance> actual = new MockCmdbSelector().select(context);
|
||||
assertNull(actual.get(0).getIp());
|
||||
assertTrue(actual.get(0).getMetadata().isEmpty());
|
||||
assertEquals("true", provider.getInstance().getMetadata().get("afterSelect"));
|
||||
assertEquals("true", provider.getEntity().getLabels().get("afterSelect"));
|
||||
assertEquals("true", consumer.getInstance().getMetadata().get("afterSelect"));
|
||||
assertEquals("true", consumer.getEntity().getLabels().get("afterSelect"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetContextType() {
|
||||
assertEquals(CMDB_CONTEXT_TYPE, new MockCmdbSelector().getContextType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetType() {
|
||||
assertEquals("mock", new MockCmdbSelector().getType());
|
||||
}
|
||||
|
||||
private class MockCmdbSelector extends AbstractCmdbSelector<Instance> {
|
||||
|
||||
@Override
|
||||
protected void doParse(String expression) throws NacosException {
|
||||
counter.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Instance> doSelect(CmdbContext<Instance> context) {
|
||||
for (CmdbContext.CmdbInstance<Instance> each : context.getProviders()) {
|
||||
each.getInstance().getMetadata().put("afterSelect", "true");
|
||||
each.getEntity().setLabels(Collections.singletonMap("afterSelect", "true"));
|
||||
}
|
||||
context.getConsumer().getInstance().getMetadata().put("afterSelect", "true");
|
||||
context.getConsumer().getEntity().setLabels(Collections.singletonMap("afterSelect", "true"));
|
||||
return Collections.singletonList(new Instance());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "mock";
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.selector;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.NamedType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class ExpressionSelectorTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.registerSubtypes(new NamedType(ExpressionSelector.class, SelectorType.label.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
ExpressionSelector selector = new ExpressionSelector();
|
||||
selector.setExpression("test expression");
|
||||
String actual = mapper.writeValueAsString(selector);
|
||||
assertTrue(actual.contains("\"type\":\"" + SelectorType.label.name() + "\""));
|
||||
assertTrue(actual.contains("\"expression\":\"test expression\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialization() throws JsonProcessingException {
|
||||
String json = "{\"type\":\"label\",\"expression\":\"test expression\"}";
|
||||
AbstractSelector actual = mapper.readValue(json, AbstractSelector.class);
|
||||
assertEquals(SelectorType.label.name(), actual.getType());
|
||||
ExpressionSelector selector = (ExpressionSelector) actual;
|
||||
assertEquals("test expression", selector.getExpression());
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.selector;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.jsontype.NamedType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class NoneSelectorTest {
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
mapper.registerSubtypes(new NamedType(NoneSelector.class, SelectorType.none.name()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerialization() throws JsonProcessingException {
|
||||
NoneSelector selector = new NoneSelector();
|
||||
String actual = mapper.writeValueAsString(selector);
|
||||
assertTrue(actual.contains("\"type\":\"" + SelectorType.none.name() + "\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeserialization() throws JsonProcessingException {
|
||||
String json = "{\"type\":\"none\"}";
|
||||
AbstractSelector actual = mapper.readValue(json, AbstractSelector.class);
|
||||
assertEquals(SelectorType.none.name(), actual.getType());
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 1999-2023 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.api.selector.context;
|
||||
|
||||
import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class CmdbContextTest {
|
||||
|
||||
@Test
|
||||
public void testToString() {
|
||||
CmdbContext<Instance> cmdbContext = new CmdbContext<>();
|
||||
cmdbContext.setProviders(Collections.singletonList(new CmdbContext.CmdbInstance<>()));
|
||||
cmdbContext.setConsumer(new CmdbContext.CmdbInstance<>());
|
||||
System.out.println(cmdbContext.toString());
|
||||
assertEquals(
|
||||
"CmdbContext{consumer=CmdbInstance{entity=null, instance=null}, providers=[CmdbInstance{entity=null, instance=null}]}",
|
||||
cmdbContext.toString());
|
||||
}
|
||||
}
|
@ -22,11 +22,14 @@ import org.junit.Test;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetAddress;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class NetUtilsTest {
|
||||
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
Class<?> clazz = Class.forName("com.alibaba.nacos.api.utils.NetUtils");
|
||||
@ -35,26 +38,54 @@ public class NetUtilsTest {
|
||||
field.set(null, "");
|
||||
System.clearProperty("com.alibaba.nacos.client.local.ip");
|
||||
System.clearProperty("com.alibaba.nacos.client.local.preferHostname");
|
||||
System.clearProperty("java.net.preferIPv6Addresses");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testLocalIP() {
|
||||
System.setProperty("com.alibaba.nacos.client.naming.local.ip", "10.2.7.8");
|
||||
public void testLocalIpWithSpecifiedIp() {
|
||||
System.setProperty("com.alibaba.nacos.client.local.ip", "10.2.8.8");
|
||||
assertEquals("10.2.8.8", NetUtils.localIP());
|
||||
System.setProperty("com.alibaba.nacos.client.local.ip", "10.2.8.9");
|
||||
assertEquals("10.2.8.8", NetUtils.localIP());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testPreferHostname() throws Exception {
|
||||
public void testLocalIpWithPreferHostname() throws Exception {
|
||||
InetAddress inetAddress = invokeGetInetAddress();
|
||||
String hostname = inetAddress.getHostName();
|
||||
System.setProperty("com.alibaba.nacos.client.local.preferHostname", "true");
|
||||
assertEquals(hostname, NetUtils.localIP());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalIpWithoutPreferHostname() throws Exception {
|
||||
InetAddress inetAddress = invokeGetInetAddress();
|
||||
String ip = inetAddress.getHostAddress();
|
||||
assertEquals(ip, NetUtils.localIP());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalIpWithException() throws Exception {
|
||||
Field field = System.class.getDeclaredField("props");
|
||||
field.setAccessible(true);
|
||||
Properties properties = (Properties) field.get(null);
|
||||
Properties mockProperties = mock(Properties.class);
|
||||
when(mockProperties.getProperty("java.net.preferIPv6Addresses")).thenThrow(new RuntimeException("test"));
|
||||
field.set(null, mockProperties);
|
||||
try {
|
||||
System.setProperty("java.net.preferIPv6Addresses", "aaa");
|
||||
InetAddress expect = InetAddress.getLocalHost();
|
||||
assertEquals(expect.getHostAddress(), NetUtils.localIP());
|
||||
} finally {
|
||||
field.set(null, properties);
|
||||
}
|
||||
}
|
||||
|
||||
private InetAddress invokeGetInetAddress() throws Exception {
|
||||
Class<?> clazz = Class.forName("com.alibaba.nacos.api.utils.NetUtils");
|
||||
Method method = clazz.getDeclaredMethod("findFirstNonLoopbackAddress");
|
||||
method.setAccessible(true);
|
||||
InetAddress inetAddress = (InetAddress) method.invoke(null);
|
||||
String hostname = inetAddress.getHostName();
|
||||
|
||||
System.setProperty("com.alibaba.nacos.client.local.preferHostname", "true");
|
||||
assertEquals(hostname, NetUtils.localIP());
|
||||
assertEquals(hostname, NetUtils.localIP());
|
||||
return (InetAddress) method.invoke(null);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ public class JacksonSerializerTest {
|
||||
@Test
|
||||
public void testSerialize() {
|
||||
String actual = new String(serializer.serialize(switchDomain));
|
||||
System.out.println(actual);
|
||||
assertTrue(actual.contains("\"defaultPushCacheMillis\":10000"));
|
||||
assertTrue(actual.contains("\"clientBeatInterval\":5000"));
|
||||
assertTrue(actual.contains("\"defaultCacheMillis\":3000"));
|
||||
|
Loading…
Reference in New Issue
Block a user