5f1b995005ff077815254c0c2821e4acd88bf0a7
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Samsung. All rights reserved.
4  *  Modifications Copyright (C) 2019,2021 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.jms;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.util.Properties;
32 import javax.naming.Context;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.parameters.GroupValidationResult;
36 import org.onap.policy.common.parameters.ParameterRuntimeException;
37
38 public class JmsCarrierTechnologyParametersTest {
39
40     JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null;
41     Properties jmsProducerProperties = null;
42     Properties jmsConsumerProperties = null;
43     GroupValidationResult result = null;
44
45     public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
46
47     public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName();
48
49     public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName();
50
51     private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
52     private static final String DEFAULT_INITIAL_CTXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
53     private static final String DEFAULT_CONSUMER_TOPIC = "apex-in";
54     private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
55     private static final int DEFAULT_CONSUMER_WAIT_TIME = 100;
56     private static final boolean DEFAULT_TO_OBJECT_MSG_SENDING = true;
57
58     /**
59      * Set up testing.
60      *
61      * @throws Exception on test set up errors.
62      */
63     @Before
64     public void setUp() throws Exception {
65         jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters();
66     }
67
68     @Test
69     public void testValidate() {
70         result = jmsCarrierTechnologyParameters.validate();
71         assertNotNull(result);
72         assertFalse(result.getStatus().isValid());
73
74         jmsCarrierTechnologyParameters.setProviderUrl("DUMMYURL");
75         jmsCarrierTechnologyParameters.setSecurityPrincipal("DUMMYPRINCIPAL");
76         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMYCREDENTIALS");
77
78         result = jmsCarrierTechnologyParameters.validate();
79         assertNotNull(result);
80         assertTrue(result.getStatus().isValid());
81     }
82
83     @Test
84     public void testJmsCarrierTechnologyParameters() {
85         assertNotNull(jmsCarrierTechnologyParameters);
86     }
87
88     @Test
89     public void testGetJmsProducerProperties() {
90         Properties producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
91         assertNotNull(producerProperties);
92
93         assertNull(producerProperties.get(Context.PROVIDER_URL));
94         assertNull(producerProperties.get(Context.SECURITY_PRINCIPAL));
95         assertNull(producerProperties.get(Context.SECURITY_CREDENTIALS));
96
97         jmsCarrierTechnologyParameters.setProviderUrl("DUMMYURL");
98         jmsCarrierTechnologyParameters.setSecurityPrincipal("DUMMYPRINCIPAL");
99         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMYCREDENTIALS");
100
101         producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
102
103         assertEquals("DUMMYURL", producerProperties.get(Context.PROVIDER_URL));
104         assertEquals("DUMMYPRINCIPAL", producerProperties.get(Context.SECURITY_PRINCIPAL));
105         assertEquals("DUMMYCREDENTIALS", producerProperties.get(Context.SECURITY_CREDENTIALS));
106
107         jmsCarrierTechnologyParameters.setProviderUrl(null);
108         jmsCarrierTechnologyParameters.setSecurityPrincipal(null);
109         jmsCarrierTechnologyParameters.setSecurityCredentials(null);
110
111         producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
112
113         assertNull(producerProperties.get(Context.PROVIDER_URL));
114         assertNull(producerProperties.get(Context.SECURITY_PRINCIPAL));
115         assertNull(producerProperties.get(Context.SECURITY_CREDENTIALS));
116     }
117
118     @Test
119     public void testGetJmsConsumerProperties() {
120         Properties consumerProperties = jmsCarrierTechnologyParameters.getJmsConsumerProperties();
121         assertNotNull(consumerProperties);
122         assertNull(consumerProperties.get(Context.SECURITY_CREDENTIALS));
123
124         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMY");
125         consumerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
126         assertEquals("DUMMY", consumerProperties.get(Context.SECURITY_CREDENTIALS));
127     }
128
129     @Test
130     public void testEqualityOfJmsConsumerAndProducerProperties() {
131         assertEquals(jmsCarrierTechnologyParameters.getJmsProducerProperties(),
132                 jmsCarrierTechnologyParameters.getJmsConsumerProperties());
133     }
134
135     @Test
136     public void testGetConnectionFactory() {
137         assertEquals(DEFAULT_CONNECTION_FACTORY, jmsCarrierTechnologyParameters.getConnectionFactory());
138     }
139
140     @Test
141     public void testSetConnectionFactory() {
142         jmsCarrierTechnologyParameters.setConnectionFactory("QueueConnectionFactory");
143         assertNotEquals(DEFAULT_CONNECTION_FACTORY, jmsCarrierTechnologyParameters.getConnectionFactory());
144     }
145
146     @Test
147     public void testSetConsumerTopic() {
148         assertEquals(DEFAULT_CONSUMER_TOPIC, jmsCarrierTechnologyParameters.getConsumerTopic());
149         jmsCarrierTechnologyParameters.setConsumerTopic(null);
150         result = jmsCarrierTechnologyParameters.validate();
151         assertFalse(result.getStatus().isValid());
152     }
153
154     @Test
155     public void testSetConsumerWaitTime() {
156         assertEquals(DEFAULT_CONSUMER_WAIT_TIME, jmsCarrierTechnologyParameters.getConsumerWaitTime());
157         jmsCarrierTechnologyParameters.setConsumerWaitTime(-1);
158         assertNotEquals(DEFAULT_CONSUMER_WAIT_TIME, jmsCarrierTechnologyParameters.getConsumerWaitTime());
159     }
160
161     @Test
162     public void testSetEventConsumerPluginClass() {
163         assertEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
164         jmsCarrierTechnologyParameters.setEventConsumerPluginClass("TestEventConsumerPluginClass");
165         assertNotEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
166     }
167
168     @Test
169     public void testSetEventProducerPluginClass() {
170         assertEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventProducerPluginClass());
171         jmsCarrierTechnologyParameters.setEventProducerPluginClass("TestEventProducerPluginClass");
172         assertNotEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS, jmsCarrierTechnologyParameters.getEventProducerPluginClass());
173     }
174
175     @Test
176     public void testSetLabel() {
177         assertEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
178         jmsCarrierTechnologyParameters.setLabel("TestLable");
179         assertNotEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
180
181     }
182
183     @Test
184     public void testSetObjectMessageSending() {
185         assertTrue(jmsCarrierTechnologyParameters.isObjectMessageSending());
186         jmsCarrierTechnologyParameters.setObjectMessageSending(!DEFAULT_TO_OBJECT_MSG_SENDING);
187         assertFalse(jmsCarrierTechnologyParameters.isObjectMessageSending());
188     }
189
190     @Test
191     public void testSetProducerTopic() {
192         assertEquals(DEFAULT_PRODUCER_TOPIC, jmsCarrierTechnologyParameters.getProducerTopic());
193         jmsCarrierTechnologyParameters.setProducerTopic("");
194         result = jmsCarrierTechnologyParameters.validate();
195         assertFalse(result.getStatus().isValid());
196     }
197
198     @Test
199     public void testSetProviderUrl() {
200         assertNull(jmsCarrierTechnologyParameters.getProviderUrl());
201         jmsCarrierTechnologyParameters.setProviderUrl(null);
202         result = jmsCarrierTechnologyParameters.validate();
203         assertFalse(result.getStatus().isValid());
204     }
205
206     @Test
207     public void testSetSecurityCredentials() {
208         assertNull(jmsCarrierTechnologyParameters.getSecurityCredentials());
209         jmsCarrierTechnologyParameters.setSecurityCredentials("");
210         result = jmsCarrierTechnologyParameters.validate();
211         assertFalse(result.getStatus().isValid());
212     }
213
214     @Test
215     public void testSetSecurityPrincipal() {
216         assertNull(jmsCarrierTechnologyParameters.getSecurityPrincipal());
217         jmsCarrierTechnologyParameters.setSecurityPrincipal(null);
218         result = jmsCarrierTechnologyParameters.validate();
219         assertFalse(result.getStatus().isValid());
220     }
221
222     @Test
223     public void testSetInitialContextFactory() {
224
225         assertEquals(DEFAULT_INITIAL_CTXT_FACTORY, jmsCarrierTechnologyParameters.getInitialContextFactory());
226
227         jmsCarrierTechnologyParameters.setInitialContextFactory(null);
228         result = jmsCarrierTechnologyParameters.validate();
229         assertFalse(result.getStatus().isValid());
230
231         jmsCarrierTechnologyParameters.setInitialContextFactory("TestInitialContextFactory");
232         assertNotEquals(DEFAULT_INITIAL_CTXT_FACTORY, jmsCarrierTechnologyParameters.getInitialContextFactory());
233     }
234
235     @Test(expected = ParameterRuntimeException.class)
236     public void testSetName() {
237         jmsCarrierTechnologyParameters.setName("TestName");
238     }
239 }