aboutsummaryrefslogtreecommitdiffstats
path: root/policy-endpoints/src
diff options
context:
space:
mode:
authorwaynedunican <wayne.dunican@est.tech>2024-08-20 08:27:23 +0100
committerwaynedunican <wayne.dunican@est.tech>2024-09-03 16:03:15 +0100
commitc780793359e31dac7d99731360cf1e575485f638 (patch)
tree897f61ba970728a80a75c35cac23033f68f941b4 /policy-endpoints/src
parent460b684f934776e9c4eefab00d1febb398e87bb1 (diff)
Increase coverage in common
Issue-ID: POLICY-5106 Change-Id: I54dfcc2ab1100e959ee786ce7a9791ef3465686f Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'policy-endpoints/src')
-rw-r--r--policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxy.java2
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxyTest.java74
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/IndexedKafkaTopicSourceFactoryTest.java74
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/KafkaPublisherWrapperTest.java97
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/features/NetLoggerFeatureApiTest.java86
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/AuthorizationFilterTest.java84
-rw-r--r--policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/RestClientParametersTest.java116
7 files changed, 532 insertions, 1 deletions
diff --git a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxy.java b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxy.java
index d9e55ddd..98fbbf0b 100644
--- a/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxy.java
+++ b/policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxy.java
@@ -45,7 +45,7 @@ import org.slf4j.LoggerFactory;
* implementation(s).
*/
@Getter
-class TopicEndpointProxy implements TopicEndpoint {
+public class TopicEndpointProxy implements TopicEndpoint {
/**
* Logger.
*/
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxyTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxyTest.java
index 198cdc2c..a30904dd 100644
--- a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxyTest.java
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/TopicEndpointProxyTest.java
@@ -24,6 +24,7 @@ package org.onap.policy.common.endpoints.event.comm;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
@@ -35,6 +36,8 @@ import java.util.Properties;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.event.comm.bus.KafkaTopicFactories;
+import org.onap.policy.common.endpoints.event.comm.bus.KafkaTopicPropertyBuilder;
import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicFactories;
import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicPropertyBuilder;
import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
@@ -47,6 +50,9 @@ class TopicEndpointProxyTest {
private static final String NOOP_SOURCE_TOPIC = "noop-source";
private static final String NOOP_SINK_TOPIC = "noop-sink";
+ private static final String KAFKA_SOURCE_TOPIC = "kafka-source";
+ private static final String KAFKA_SINK_TOPIC = "kafka-sink";
+
private final Properties configuration = new Properties();
private final TopicParameterGroup group = new TopicParameterGroup();
@@ -104,6 +110,8 @@ class TopicEndpointProxyTest {
public void tearDown() {
NoopTopicFactories.getSinkFactory().destroy();
NoopTopicFactories.getSourceFactory().destroy();
+ KafkaTopicFactories.getSinkFactory().destroy();
+ KafkaTopicFactories.getSourceFactory().destroy();
}
@Test
@@ -126,6 +134,29 @@ class TopicEndpointProxyTest {
assertTrue(allSources(sources));
assertFalse(anySink(sources));
+
+ sources = manager.addTopicSources(group.getTopicSources());
+ assertSame(1, sources.size());
+ assertTrue(allSources(sources));
+ }
+
+ @Test
+ void testAddTopicSourcesKafka() {
+ TopicEndpoint manager = new TopicEndpointProxy();
+
+ KafkaTopicPropertyBuilder kafkaTopicPropertyBuilder =
+ new KafkaTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_KAFKA_SOURCE_TOPICS)
+ .makeTopic(KAFKA_SOURCE_TOPIC);
+
+ configuration.putAll(kafkaTopicPropertyBuilder.build());
+ group.getTopicSources().add(kafkaTopicPropertyBuilder.getParams());
+ List<TopicSource> sources = manager.addTopicSources(group.getTopicSources());
+ assertSame(2, sources.size());
+
+ configuration.remove(KAFKA_SOURCE_TOPIC);
+ group.setTopicSources(new LinkedList<>());
+ sources = manager.addTopicSources(group.getTopicSources());
+ assertSame(0, sources.size());
}
@Test
@@ -151,6 +182,28 @@ class TopicEndpointProxyTest {
}
@Test
+ void testAddTopicSinksListOfTopicParametersKafka() {
+ TopicEndpoint manager = new TopicEndpointProxy();
+
+ List<TopicSink> sinks = manager.addTopicSinks(group.getTopicSinks());
+ assertSame(1, sinks.size());
+
+ KafkaTopicPropertyBuilder kafkaTopicPropertyBuilder =
+ new KafkaTopicPropertyBuilder(PolicyEndPointProperties.PROPERTY_KAFKA_SINK_TOPICS)
+ .makeTopic(KAFKA_SINK_TOPIC);
+
+ configuration.putAll(kafkaTopicPropertyBuilder.build());
+ group.getTopicSources().add(kafkaTopicPropertyBuilder.getParams());
+ sinks = manager.addTopicSinks(group.getTopicSources());
+ assertSame(2, sinks.size());
+
+ configuration.remove(KAFKA_SOURCE_TOPIC);
+ group.setTopicSources(new LinkedList<>());
+ sinks = manager.addTopicSinks(group.getTopicSources());
+ assertSame(0, sinks.size());
+ }
+
+ @Test
void testAddTopicSinksProperties() {
TopicEndpoint manager = new TopicEndpointProxy();
@@ -220,6 +273,13 @@ class TopicEndpointProxyTest {
assertTrue(allSources(sources));
assertFalse(anySink(sources));
+
+ assertThatThrownBy(() -> manager.getKafkaTopicSource("testTopic"))
+ .hasMessageContaining("KafkaTopiceSource for testTopic not found");
+
+ List<String> topicName = null;
+ assertThatThrownBy(() -> manager.getTopicSources(topicName))
+ .hasMessageContaining("must provide a list of topics");
}
@Test
@@ -234,6 +294,20 @@ class TopicEndpointProxyTest {
assertFalse(anySource(sinks));
assertTrue(allSinks(sinks));
+
+ final List<String> sinks2 = null;
+ assertThatThrownBy(() -> manager.getTopicSinks(sinks2)).hasMessageContaining("must provide a list of topics");
+
+ List<String> sinks3 = List.of(NOOP_SINK_TOPIC);
+ assertThatCode(() -> manager.getTopicSinks(sinks3)).doesNotThrowAnyException();
+
+ String sinkTest = null;
+ assertThatThrownBy(() -> manager.getTopicSinks(sinkTest))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Invalid parameter");
+
+ assertThatThrownBy(() -> manager.getKafkaTopicSink("testTopic"))
+ .hasMessageContaining("KafkaTopicSink for testTopic not found");
}
@Test
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/IndexedKafkaTopicSourceFactoryTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/IndexedKafkaTopicSourceFactoryTest.java
new file mode 100644
index 00000000..80229419
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/IndexedKafkaTopicSourceFactoryTest.java
@@ -0,0 +1,74 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.event.comm.bus;
+
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.List;
+import org.apache.kafka.clients.ClientUtils;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+
+class IndexedKafkaTopicSourceFactoryTest {
+
+ private IndexedKafkaTopicSourceFactory factory;
+
+ @Mock
+ ClientUtils mockClientUtils;
+
+ @Test
+ void testBuild() {
+ factory = new IndexedKafkaTopicSourceFactory();
+ BusTopicParams params = new BusTopicParams();
+
+ // set servers to null
+ params.setServers(null);
+ assertThatThrownBy(() -> factory.build(params))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("KAFKA Server(s) must be provided");
+
+ // set servers to empty
+ params.setServers(List.of());
+ assertThatThrownBy(() -> factory.build(params))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("KAFKA Server(s) must be provided");
+
+ List<String> servers = List.of("kafka:9092", "kafka:29092");
+ params.setServers(servers);
+
+ // set topic to null
+ params.setTopic(null);
+ assertThatThrownBy(() -> factory.build(params))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("A topic must be provided");
+
+ // set topic to empty
+ params.setTopic("");
+ assertThatThrownBy(() -> factory.build(params))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("A topic must be provided");
+
+ params.setTopic("topic01");
+
+ assertThatThrownBy(() -> factory.build(servers, "topic1"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("cannot create topic");
+ }
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/KafkaPublisherWrapperTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/KafkaPublisherWrapperTest.java
new file mode 100644
index 00000000..1f7c2cf7
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/KafkaPublisherWrapperTest.java
@@ -0,0 +1,97 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.event.comm.bus.internal;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.Properties;
+import org.apache.kafka.clients.producer.KafkaProducer;
+import org.apache.kafka.clients.producer.Producer;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentMatchers;
+
+class KafkaPublisherWrapperTest {
+
+ private BusPublisher.KafkaPublisherWrapper kafkaPublisherWrapper;
+ private Producer<String, String> mockProducer;
+ private BusTopicParams mockBusTopicParams;
+
+ @BeforeEach
+ void setUp() {
+ mockProducer = mock(KafkaProducer.class);
+ mockBusTopicParams = mock(BusTopicParams.class);
+
+ when(mockBusTopicParams.getTopic()).thenReturn("testTopic");
+ when(mockBusTopicParams.getServers()).thenReturn(Collections.singletonList("localhost:9092"));
+ when(mockBusTopicParams.isTopicInvalid()).thenReturn(false);
+ when(mockBusTopicParams.isAdditionalPropsValid()).thenReturn(false);
+ when(mockBusTopicParams.isAllowTracing()).thenReturn(false);
+
+ kafkaPublisherWrapper = new BusPublisher.KafkaPublisherWrapper(mockBusTopicParams) {
+ protected Producer<String, String> createProducer(Properties props) { // NOSONAR instance creation
+ return mockProducer;
+ }
+ };
+ }
+
+ @Test
+ void testConstructor() {
+ verify(mockBusTopicParams).getTopic();
+ verify(mockBusTopicParams).getServers();
+ verify(mockBusTopicParams).isTopicInvalid();
+ verify(mockBusTopicParams).isAdditionalPropsValid();
+ verify(mockBusTopicParams).isAllowTracing();
+ }
+
+ @Test
+ void testSendSuccess() {
+ when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenReturn(null);
+ assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
+ }
+
+ @Test
+ void testSendNullMessage() {
+ IllegalArgumentException thrown = assertThrows(
+ IllegalArgumentException.class,
+ () -> kafkaPublisherWrapper.send("partitionId", null),
+ "Expected send() to throw, but it didn't"
+ );
+ assertEquals("No message provided", thrown.getMessage());
+ }
+
+ @Test
+ void testSendFailure() {
+ when(mockProducer.send(ArgumentMatchers.any(ProducerRecord.class))).thenThrow(RuntimeException.class);
+ assertTrue(kafkaPublisherWrapper.send("partitionId", "testMessage"));
+ }
+
+ @Test
+ void testClose() {
+ assertThatCode(kafkaPublisherWrapper::close).doesNotThrowAnyException();
+ }
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/features/NetLoggerFeatureApiTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/features/NetLoggerFeatureApiTest.java
new file mode 100644
index 00000000..2ea64239
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/features/NetLoggerFeatureApiTest.java
@@ -0,0 +1,86 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.features;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
+import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
+import org.slf4j.Logger;
+
+@ExtendWith(MockitoExtension.class)
+class NetLoggerFeatureApiTest {
+
+ @Mock
+ private Logger mockLogger;
+
+ @Mock
+ private EventType mockEventType;
+
+ @Mock
+ private CommInfrastructure mockCommInfrastructure;
+
+ private NetLoggerFeatureApi featureApi;
+
+ @BeforeEach
+ public void setUp() {
+ featureApi = new NetLoggerFeatureApi() {
+ @Override
+ public boolean beforeLog(Logger eventLogger, EventType type, CommInfrastructure protocol, String topic,
+ String message) {
+ return NetLoggerFeatureApi.super.beforeLog(eventLogger, type, protocol, topic, message);
+ }
+
+ @Override
+ public boolean afterLog(Logger eventLogger, EventType type, CommInfrastructure protocol, String topic,
+ String message) {
+ return NetLoggerFeatureApi.super.afterLog(eventLogger, type, protocol, topic, message);
+ }
+
+ @Override
+ public int getSequenceNumber() {
+ return 0;
+ }
+
+ @Override
+ public String getName() {
+ return NetLoggerFeatureApi.super.getName();
+ }
+ };
+ }
+
+ @Test
+ void testBeforeLogDefaultBehavior() {
+ boolean result = featureApi.beforeLog(mockLogger, mockEventType, mockCommInfrastructure,
+ "testTopic", "testMessage");
+ assertFalse(result, "Expected beforeLog to return false by default");
+ }
+
+ @Test
+ void testAfterLogDefaultBehavior() {
+ boolean result = featureApi.afterLog(mockLogger, mockEventType, mockCommInfrastructure,
+ "testTopic", "testMessage");
+ assertFalse(result, "Expected afterLog to return false by default");
+ }
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/AuthorizationFilterTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/AuthorizationFilterTest.java
new file mode 100644
index 00000000..2ab3071f
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/http/server/test/AuthorizationFilterTest.java
@@ -0,0 +1,84 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.http.server.test;
+
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.Mockito.mock;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.ServletRequest;
+import jakarta.servlet.ServletResponse;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.onap.policy.common.endpoints.http.server.AuthorizationFilter;
+
+class AuthorizationFilterTest {
+
+ AuthorizationFilter filter;
+
+ @Mock
+ ServletRequest request;
+
+ @Mock
+ HttpServletRequest httpRequest;
+
+ @Mock
+ HttpServletResponse httpResponse;
+
+ @Mock
+ ServletResponse response;
+
+ @Mock
+ FilterChain chain;
+
+ @BeforeEach
+ void setUp() {
+ request = mock(ServletRequest.class);
+ response = mock(ServletResponse.class);
+ chain = mock(FilterChain.class);
+ httpRequest = mock(HttpServletRequest.class);
+ httpResponse = mock(HttpServletResponse.class);
+
+ filter = new AuthorizationFilter() {
+ @Override
+ protected String getRole(HttpServletRequest request) {
+ return "testRole";
+ }
+ };
+ }
+
+ @Test
+ void testAuthorizationFilter() {
+ assertThatThrownBy(() -> filter.doFilter(request, response, chain))
+ .isInstanceOf(ServletException.class)
+ .hasMessageContaining("Not an HttpServletRequest instance");
+
+ assertThatThrownBy(() -> filter.doFilter(httpRequest, response, chain))
+ .isInstanceOf(ServletException.class)
+ .hasMessageContaining("Not an HttpServletResponse instance");
+
+ assertThatCode(() -> filter.doFilter(httpRequest, httpResponse, chain))
+ .doesNotThrowAnyException();
+ }
+}
diff --git a/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/RestClientParametersTest.java b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/RestClientParametersTest.java
new file mode 100644
index 00000000..6013ff2d
--- /dev/null
+++ b/policy-endpoints/src/test/java/org/onap/policy/common/endpoints/parameters/RestClientParametersTest.java
@@ -0,0 +1,116 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2024 Nordix Foundation
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.endpoints.parameters;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+
+class RestClientParametersTest {
+
+ private RestClientParameters params;
+
+ @BeforeEach
+ void setUp() {
+ params = new RestClientParameters();
+ }
+
+ @Test
+ void testValidate_ValidParameters() {
+ params.setHostname("localhost");
+ params.setClientName("testClient");
+ params.setPort(8080);
+
+ BeanValidationResult result = params.validate();
+
+ assertEquals(ValidationStatus.CLEAN, result.getStatus(), "Expected the parameters to be valid");
+ assertNull(result.getResult(), "Expected no validation errors");
+ }
+
+ @Test
+ void testValidate_InvalidHostname() {
+ params.setHostname("");
+ params.setClientName("testClient");
+ params.setPort(8080);
+
+ BeanValidationResult result = params.validate();
+
+ assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
+ assertTrue(result.getResult().contains("hostname") && result.getResult().contains("is blank"),
+ "Expected invalid hostname error message");
+ }
+
+ @Test
+ void testValidate_InvalidClientName() {
+ params.setHostname("localhost");
+ params.setClientName("");
+ params.setPort(8080);
+
+ BeanValidationResult result = params.validate();
+
+ assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
+ assertTrue(result.getResult().contains("clientName") && result.getResult().contains("is blank"),
+ "Expected invalid clientName error message");
+ }
+
+ @Test
+ void testValidate_InvalidPort() {
+ params.setHostname("localhost");
+ params.setClientName("testClient");
+ params.setPort(-1);
+
+ BeanValidationResult result = params.validate();
+
+ assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
+ assertTrue(result.getResult().contains("port") && result.getResult().contains("is not valid"),
+ "Expected invalid port error message");
+ }
+
+ @Test
+ void testValidate_MultipleInvalidParameters() {
+ params.setHostname("");
+ params.setClientName("");
+ params.setPort(-1);
+
+ BeanValidationResult result = params.validate();
+
+ assertEquals(ValidationStatus.INVALID, result.getStatus(), "Expected the parameters to be invalid");
+
+ assertTrue(result.getResult().contains("hostname") && result.getResult().contains("is blank"),
+ "Expected invalid hostname error message");
+
+ assertTrue(result.getResult().contains("clientName") && result.getResult().contains("is blank"),
+ "Expected invalid clientName error message");
+
+ assertTrue(result.getResult().contains("port") && result.getResult().contains("is not valid"),
+ "Expected invalid port error message");
+ }
+
+ @Test
+ void testGetAndSetName() {
+ String name = "testClient";
+ params.setName(name);
+ assertEquals(name, params.getName(), "Expected the client name to be set and retrieved correctly");
+ }
+}