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