From ade480b317fe7bcf9ce94ef34e6c42b71a0ea1a3 Mon Sep 17 00:00:00 2001 From: Guangrong Fu Date: Mon, 4 Sep 2023 19:03:50 +0800 Subject: Removed MSB Dependencies Issue-ID: HOLMES-629 Signed-off-by: Guangrong Fu Change-Id: Ia74c8456822ba71f465a7142431327a884c50a70 --- .../onap/holmes/common/msb/MsbRegisterTest.java | 157 +++++++++++++++++++++ .../msb/entity/CustomDateSerializerTest.java | 32 +++++ .../holmes/common/msb/entity/KeyValuePairTest.java | 43 ++++++ .../msb/entity/MicroServiceFullInfoTest.java | 114 +++++++++++++++ .../common/msb/entity/MicroServiceInfoTest.java | 64 +++++++++ .../holmes/common/msb/entity/NodeInfoTest.java | 47 ++++++ .../onap/holmes/common/msb/entity/NodeTest.java | 56 ++++++++ .../onap/holmes/common/msb/entity/ServiceTest.java | 59 ++++++++ .../onap/holmes/common/utils/MsbRegisterTest.java | 156 -------------------- 9 files changed, 572 insertions(+), 156 deletions(-) create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/MsbRegisterTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/CustomDateSerializerTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/KeyValuePairTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceFullInfoTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceInfoTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeInfoTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeTest.java create mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/ServiceTest.java delete mode 100644 holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java (limited to 'holmes-actions/src/test/java') diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/MsbRegisterTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/MsbRegisterTest.java new file mode 100644 index 0000000..997d1cc --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/MsbRegisterTest.java @@ -0,0 +1,157 @@ +/** + * Copyright 2017-2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb; + +import jakarta.ws.rs.client.Entity; +import jakarta.ws.rs.core.MediaType; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.holmes.common.config.MicroServiceConfig; +import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.msb.entity.MicroServiceFullInfo; +import org.onap.holmes.common.msb.entity.MicroServiceInfo; +import org.onap.holmes.common.utils.GsonUtil; +import org.onap.holmes.common.utils.JerseyClient; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.expect; +import static org.powermock.api.easymock.PowerMock.createMock; +import static org.powermock.api.easymock.PowerMock.expectNew; + +@PrepareForTest({MicroServiceConfig.class, JerseyClient.class}) +@RunWith(PowerMockRunner.class) +@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) +public class MsbRegisterTest { + + private JerseyClient mockedJerseyClient; + private MicroServiceInfo msi; + + @Before + public void before() throws Exception { + msi = new MicroServiceInfo(); + String[] msbAddrInfo = {"127.0.0.1", "80"}; + + PowerMock.mockStatic(MicroServiceConfig.class); + expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo); + + mockedJerseyClient = createMock(JerseyClient.class); + expectNew(JerseyClient.class).andReturn(mockedJerseyClient); + } + + @Test + public void test_register2Msb_normal() { + expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient); + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + + "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + + "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + + "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + + "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + + "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", + MicroServiceFullInfo.class)); + + PowerMock.replayAll(); + + MsbRegister msbRegister = new MsbRegister(); + try { + msbRegister.register2Msb(msi); + } catch (CorrelationException e) { + // Do nothing + } + + PowerMock.verifyAll(); + } + + @Test + public void test_register2Msb_fail_n_times() { + int requestTimes = 3; + expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes); + expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes); + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(null).times(requestTimes - 1); + + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + + "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + + "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + + "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + + "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + + "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", + MicroServiceFullInfo.class)); + + PowerMock.replayAll(); + + MsbRegister msbRegister = new MsbRegister(); + msbRegister.setInterval(1); + try { + msbRegister.register2Msb(msi); + } catch (CorrelationException e) { + // Do nothing + } + + PowerMock.verifyAll(); + } + + @Test + public void test_register2Msb_fail_n_times_due_to_exception() { + int requestTimes = 3; + expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes); + expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes); + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andThrow(new RuntimeException("Failure!")).times(requestTimes - 1); + + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + + "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + + "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + + "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + + "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + + "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", + MicroServiceFullInfo.class)); + + PowerMock.replayAll(); + + MsbRegister msbRegister = new MsbRegister(); + msbRegister.setInterval(1); + try { + msbRegister.register2Msb(msi); + } catch (CorrelationException e) { + // Do nothing + } + + PowerMock.verifyAll(); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/CustomDateSerializerTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/CustomDateSerializerTest.java new file mode 100644 index 0000000..fb105e8 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/CustomDateSerializerTest.java @@ -0,0 +1,32 @@ +package org.onap.holmes.common.msb.entity; + +import com.fasterxml.jackson.core.JsonFactory; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import org.junit.Test; + +import java.io.IOException; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.junit.Assert.*; + +public class CustomDateSerializerTest { + @Test + public void testSerialize() throws IOException { + Date date = new Date(); + + StringWriter writer = new StringWriter(); + JsonGenerator jsonGenerator = new JsonFactory().createGenerator(writer); + SerializerProvider provider = new ObjectMapper().getSerializerProvider(); + + CustomDateSerializer serializer = new CustomDateSerializer(); + serializer.serialize(date, jsonGenerator, provider); + jsonGenerator.flush(); + + String expectedOutput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX").format(date); + assertEquals("\"" + expectedOutput + "\"", writer.toString()); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/KeyValuePairTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/KeyValuePairTest.java new file mode 100644 index 0000000..9c1bad2 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/KeyValuePairTest.java @@ -0,0 +1,43 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class KeyValuePairTest { + + @Test + public void testGettersAndSetters() { + KeyValuePair pair = new KeyValuePair(); + pair.setKey("myKey"); + pair.setValue("myValue"); + + assertEquals("myKey", pair.getKey()); + assertEquals("myValue", pair.getValue()); + } + + @Test + public void testConstructor() { + KeyValuePair pair = new KeyValuePair("key1", "value1"); + + assertEquals("key1", pair.getKey()); + assertEquals("value1", pair.getValue()); + } + +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceFullInfoTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceFullInfoTest.java new file mode 100644 index 0000000..8e63b59 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceFullInfoTest.java @@ -0,0 +1,114 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import java.util.*; + +import static org.junit.Assert.*; + +public class MicroServiceFullInfoTest { + @Test + public void testGettersAndSetters() { + MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo(); + microServiceFullInfo.setServiceName("service-name"); + microServiceFullInfo.setVersion("1.0"); + microServiceFullInfo.setUrl("http://example.com"); + microServiceFullInfo.setProtocol("http"); + microServiceFullInfo.setVisualRange("local"); + microServiceFullInfo.setStatus("active"); + + Set nodes = new HashSet<>(); + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setNodeId("node-1"); + nodeInfo.setIp("127.0.0.1"); + nodeInfo.setPort("8080"); + nodeInfo.setTtl("300"); + nodeInfo.setCreated_at(new Date()); + nodeInfo.setUpdated_at(new Date()); + nodeInfo.setExpiration(new Date()); + nodes.add(nodeInfo); + microServiceFullInfo.setNodes(nodes); + + List metadata = new ArrayList<>(); + metadata.add(new KeyValuePair("key1", "value1")); + metadata.add(new KeyValuePair("key2", "value2")); + microServiceFullInfo.setMetadata(metadata); + + assertEquals("service-name", microServiceFullInfo.getServiceName()); + assertEquals("1.0", microServiceFullInfo.getVersion()); + assertEquals("http://example.com", microServiceFullInfo.getUrl()); + assertEquals("http", microServiceFullInfo.getProtocol()); + assertEquals("local", microServiceFullInfo.getVisualRange()); + assertEquals("active", microServiceFullInfo.getStatus()); + assertEquals(nodes, microServiceFullInfo.getNodes()); + assertEquals(metadata, microServiceFullInfo.getMetadata()); + } + + @Test + public void testToString() { + MicroServiceFullInfo microServiceFullInfo = new MicroServiceFullInfo(); + microServiceFullInfo.setServiceName("service-name"); + microServiceFullInfo.setVersion("1.0"); + microServiceFullInfo.setUrl("http://example.com"); + microServiceFullInfo.setProtocol("http"); + microServiceFullInfo.setVisualRange("local"); + microServiceFullInfo.setStatus("active"); + + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setNodeId("node-1"); + nodeInfo.setIp("127.0.0.1"); + nodeInfo.setPort("8080"); + nodeInfo.setTtl("300"); + nodeInfo.setCreated_at(new Date()); + nodeInfo.setUpdated_at(new Date()); + nodeInfo.setExpiration(new Date()); + + Set nodes = new HashSet<>(); + nodes.add(nodeInfo); + microServiceFullInfo.setNodes(nodes); + + List metadata = new ArrayList<>(); + metadata.add(new KeyValuePair("key1", "value1")); + metadata.add(new KeyValuePair("key2", "value2")); + microServiceFullInfo.setMetadata(metadata); + + // Test the toString method + String expectedOutput = "MicroService List:\r\n" + + "serviceName:service-name\r\n" + + "version:1.0\r\n" + + "url:http://example.com\r\n" + + "protocol:http\r\n" + + "visualRange:local\r\n" + + "nodes:\r\n" + + " nodeId-node-1\r\n" + + " ip-127.0.0.1\r\n" + + " port-8080\r\n" + + " ttl-300\r\n" + + " Created_at-" + nodeInfo.getCreated_at() + "\r\n" + + " Updated_at-" + nodeInfo.getUpdated_at() + "\r\n" + + " Expiration-" + nodeInfo.getExpiration() + "\r\n" + + "metadata:\r\n" + + " key-key1\r\n" + + " value-value1\r\n" + + " key-key2\r\n" + + " value-value2\r\n"; + + assertEquals(expectedOutput, microServiceFullInfo.toString()); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceInfoTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceInfoTest.java new file mode 100644 index 0000000..aa89f19 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/MicroServiceInfoTest.java @@ -0,0 +1,64 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertEquals; + +public class MicroServiceInfoTest { + @Test + public void testToString() { + MicroServiceInfo microServiceInfo = getMicroServiceInfo(); + + String expectedOutput = "MicroService List:\r\n" + + "serviceName:service-name\r\n" + + "version:1.0\r\n" + + "url:http://example.com\r\n" + + "protocol:http\r\n" + + "visualRange:local\r\n" + + "enable_ssl:true\r\n" + + "nodes:\r\n" + + " ip-127.0.0.1\r\n" + + " port-8080\r\n" + + " ttl-300\r\n"; + + assertEquals(expectedOutput, microServiceInfo.toString()); + } + + private static MicroServiceInfo getMicroServiceInfo() { + MicroServiceInfo microServiceInfo = new MicroServiceInfo(); + microServiceInfo.setServiceName("service-name"); + microServiceInfo.setVersion("1.0"); + microServiceInfo.setUrl("http://example.com"); + microServiceInfo.setProtocol("http"); + microServiceInfo.setVisualRange("local"); + microServiceInfo.setEnable_ssl(true); + + Set nodes = new HashSet<>(); + Node node = new Node(); + node.setIp("127.0.0.1"); + node.setPort("8080"); + node.setTtl("300"); + nodes.add(node); + microServiceInfo.setNodes(nodes); + return microServiceInfo; + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeInfoTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeInfoTest.java new file mode 100644 index 0000000..9458dcf --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeInfoTest.java @@ -0,0 +1,47 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import java.util.Date; + +import static org.junit.Assert.*; + +public class NodeInfoTest { + @Test + public void testGettersAndSetters() { + NodeInfo nodeInfo = new NodeInfo(); + nodeInfo.setNodeId("node-1"); + nodeInfo.setStatus("active"); + + Date expiration = new Date(); + nodeInfo.setExpiration(expiration); + + Date createdAt = new Date(); + nodeInfo.setCreated_at(createdAt); + + Date updatedAt = new Date(); + nodeInfo.setUpdated_at(updatedAt); + + assertEquals("node-1", nodeInfo.getNodeId()); + assertEquals("active", nodeInfo.getStatus()); + assertEquals(expiration, nodeInfo.getExpiration()); + assertEquals(createdAt, nodeInfo.getCreated_at()); + assertEquals(updatedAt, nodeInfo.getUpdated_at()); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeTest.java new file mode 100644 index 0000000..7555ad3 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/NodeTest.java @@ -0,0 +1,56 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class NodeTest { + + @Test + public void testToString() { + Node node = new Node(); + node.setIp("127.0.0.1"); + node.setPort("8080"); + node.setTtl("300"); + + String expectedOutput = "127.0.0.1:8080 ttl:300"; + assertEquals(expectedOutput, node.toString()); + } + + @Test + public void testGettersAndSetters() { + Node node = new Node(); + node.setIp("192.168.1.1"); + node.setPort("8888"); + node.setTtl("600"); + node.setCheckType("http"); + node.setCheckUrl("/health"); + node.setCheckInterval("30s"); + node.setCheckTimeOut("5s"); + + assertEquals("192.168.1.1", node.getIp()); + assertEquals("8888", node.getPort()); + assertEquals("600", node.getTtl()); + assertEquals("http", node.getCheckType()); + assertEquals("/health", node.getCheckUrl()); + assertEquals("30s", node.getCheckInterval()); + assertEquals("5s", node.getCheckTimeOut()); + } + +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/ServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/ServiceTest.java new file mode 100644 index 0000000..ac50841 --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/msb/entity/ServiceTest.java @@ -0,0 +1,59 @@ +/** + * Copyright 2023 ZTE Corporation. + *

+ * 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 org.onap.holmes.common.msb.entity; + +import org.junit.Test; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import static org.junit.Assert.*; + +public class ServiceTest { + @Test + public void testGettersAndSetters() { + Service service = new Service<>(); + service.setServiceName("my-service"); + service.setVersion("1.0"); + service.setUrl("http://example.com"); + service.setProtocol("http"); + service.setVisualRange("local"); + service.setLb_policy("round-robin"); + service.setPath("/api"); + service.setEnable_ssl(true); + + Set nodes = new HashSet<>(); + nodes.add(8080); + nodes.add(8081); + service.setNodes(nodes); + + List metadata = List.of(new KeyValuePair("key1", "value1"), new KeyValuePair("key2", "value2")); + service.setMetadata(metadata); + + assertEquals("my-service", service.getServiceName()); + assertEquals("1.0", service.getVersion()); + assertEquals("http://example.com", service.getUrl()); + assertEquals("http", service.getProtocol()); + assertEquals("local", service.getVisualRange()); + assertEquals("round-robin", service.getLb_policy()); + assertEquals("/api", service.getPath()); + assertEquals(true, service.isEnable_ssl()); + assertEquals(nodes, service.getNodes()); + assertEquals(metadata, service.getMetadata()); + } +} \ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java deleted file mode 100644 index 073fcc7..0000000 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java +++ /dev/null @@ -1,156 +0,0 @@ -/** - * Copyright 2017-2022 ZTE Corporation. - *

- * 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 org.onap.holmes.common.utils; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.onap.holmes.common.config.MicroServiceConfig; -import org.onap.holmes.common.exception.CorrelationException; -import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; -import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import jakarta.ws.rs.client.Entity; -import jakarta.ws.rs.core.MediaType; - -import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; -import static org.powermock.api.easymock.PowerMock.createMock; -import static org.powermock.api.easymock.PowerMock.expectNew; - -@PrepareForTest({MicroServiceConfig.class, JerseyClient.class}) -@RunWith(PowerMockRunner.class) -@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) -public class MsbRegisterTest { - - private JerseyClient mockedJerseyClient; - private MicroServiceInfo msi; - - @Before - public void before() throws Exception { - msi = new MicroServiceInfo(); - String[] msbAddrInfo = {"127.0.0.1", "80"}; - - PowerMock.mockStatic(MicroServiceConfig.class); - expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo); - - mockedJerseyClient = createMock(JerseyClient.class); - expectNew(JerseyClient.class).andReturn(mockedJerseyClient); - } - - @Test - public void test_register2Msb_normal() { - expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient); - expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient); - expect(mockedJerseyClient.post(anyObject(String.class), - anyObject(Entity.class), - anyObject(Class.class))) - .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + - "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + - "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + - "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + - "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + - "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + - "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", - MicroServiceFullInfo.class)); - - PowerMock.replayAll(); - - MsbRegister msbRegister = new MsbRegister(); - try { - msbRegister.register2Msb(msi); - } catch (CorrelationException e) { - // Do nothing - } - - PowerMock.verifyAll(); - } - - @Test - public void test_register2Msb_fail_n_times() { - int requestTimes = 3; - expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes); - expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes); - expect(mockedJerseyClient.post(anyObject(String.class), - anyObject(Entity.class), - anyObject(Class.class))) - .andReturn(null).times(requestTimes - 1); - - expect(mockedJerseyClient.post(anyObject(String.class), - anyObject(Entity.class), - anyObject(Class.class))) - .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + - "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + - "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + - "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + - "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + - "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + - "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", - MicroServiceFullInfo.class)); - - PowerMock.replayAll(); - - MsbRegister msbRegister = new MsbRegister(); - msbRegister.setInterval(1); - try { - msbRegister.register2Msb(msi); - } catch (CorrelationException e) { - // Do nothing - } - - PowerMock.verifyAll(); - } - - @Test - public void test_register2Msb_fail_n_times_due_to_exception() { - int requestTimes = 3; - expect(mockedJerseyClient.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedJerseyClient).times(requestTimes); - expect(mockedJerseyClient.queryParam("createOrUpdate", true)).andReturn(mockedJerseyClient).times(requestTimes); - expect(mockedJerseyClient.post(anyObject(String.class), - anyObject(Entity.class), - anyObject(Class.class))) - .andThrow(new RuntimeException("Failure!")).times(requestTimes - 1); - - expect(mockedJerseyClient.post(anyObject(String.class), - anyObject(Entity.class), - anyObject(Class.class))) - .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + - "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + - "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + - "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + - "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + - "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + - "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", - MicroServiceFullInfo.class)); - - PowerMock.replayAll(); - - MsbRegister msbRegister = new MsbRegister(); - msbRegister.setInterval(1); - try { - msbRegister.register2Msb(msi); - } catch (CorrelationException e) { - // Do nothing - } - - PowerMock.verifyAll(); - } -} \ No newline at end of file -- cgit 1.2.3-korg