6be0fd32c83cfce8f7b9fc558ee66a4ff29918b4
[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 =
48             ApexJmsProducer.class.getName();
49
50     public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS =
51             ApexJmsConsumer.class.getName();
52
53     private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
54     private static final String DEFAULT_INITIAL_CTXT_FACTORY =
55             "org.jboss.naming.remote.client.InitialContextFactory";
56     private static final String DEFAULT_PROVIDER_URL = "remote://localhost:4447";
57     private static final String DEFAULT_SECURITY_PRINCIPAL = "userid";
58     private static final String DEFAULT_CONSUMER_TOPIC = "apex-in";
59     private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
60     private static final int DEFAULT_CONSUMER_WAIT_TIME = 100;
61     private static final boolean DEFAULT_TO_OBJECT_MSG_SENDING = true;
62
63     /**
64      * Set up testing.
65      *
66      * @throws Exception on test set up errors.
67      */
68     @Before
69     public void setUp() throws Exception {
70         jmsCarrierTechnologyParameters = new JmsCarrierTechnologyParameters();
71     }
72
73     @Test
74     public void testValidate() {
75         result = jmsCarrierTechnologyParameters.validate();
76         assertNotNull(result);
77         assertFalse(result.getStatus().isValid());
78
79         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMY");
80         result = jmsCarrierTechnologyParameters.validate();
81         assertNotNull(result);
82         assertTrue(result.getStatus().isValid());
83     }
84
85     @Test
86     public void testJmsCarrierTechnologyParameters() {
87         assertNotNull(jmsCarrierTechnologyParameters);
88     }
89
90     @Test
91     public void testGetJmsProducerProperties() {
92         Properties producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
93         assertNotNull(producerProperties);
94         assertNull(producerProperties.get(Context.SECURITY_CREDENTIALS));
95
96         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMY");
97         producerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
98         assertEquals("DUMMY", producerProperties.get(Context.SECURITY_CREDENTIALS));
99     }
100
101     @Test
102     public void testGetJmsConsumerProperties() {
103         Properties consumerProperties = jmsCarrierTechnologyParameters.getJmsConsumerProperties();
104         assertNotNull(consumerProperties);
105         assertNull(consumerProperties.get(Context.SECURITY_CREDENTIALS));
106
107         jmsCarrierTechnologyParameters.setSecurityCredentials("DUMMY");
108         consumerProperties = jmsCarrierTechnologyParameters.getJmsProducerProperties();
109         assertEquals("DUMMY", consumerProperties.get(Context.SECURITY_CREDENTIALS));
110     }
111
112     @Test
113     public void testEqualityOfJmsConsumerAndProducerProperties() {
114         assertEquals(jmsCarrierTechnologyParameters.getJmsProducerProperties(),
115                 jmsCarrierTechnologyParameters.getJmsConsumerProperties());
116     }
117
118     @Test
119     public void testGetConnectionFactory() {
120         assertEquals(DEFAULT_CONNECTION_FACTORY,
121                 jmsCarrierTechnologyParameters.getConnectionFactory());
122     }
123
124     @Test
125     public void testSetConnectionFactory() {
126         jmsCarrierTechnologyParameters.setConnectionFactory("QueueConnectionFactory");
127         assertNotEquals(DEFAULT_CONNECTION_FACTORY,
128                 jmsCarrierTechnologyParameters.getConnectionFactory());
129     }
130
131     @Test
132     public void testSetConsumerTopic() {
133         assertEquals(DEFAULT_CONSUMER_TOPIC, jmsCarrierTechnologyParameters.getConsumerTopic());
134         jmsCarrierTechnologyParameters.setConsumerTopic(null);
135         result = jmsCarrierTechnologyParameters.validate();
136         assertFalse(result.getStatus().isValid());
137     }
138
139     @Test
140     public void testSetConsumerWaitTime() {
141         assertEquals(DEFAULT_CONSUMER_WAIT_TIME,
142                 jmsCarrierTechnologyParameters.getConsumerWaitTime());
143         jmsCarrierTechnologyParameters.setConsumerWaitTime(-1);
144         assertNotEquals(DEFAULT_CONSUMER_WAIT_TIME,
145                 jmsCarrierTechnologyParameters.getConsumerWaitTime());
146     }
147
148     @Test
149     public void testSetEventConsumerPluginClass() {
150         assertEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS,
151                 jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
152         jmsCarrierTechnologyParameters.setEventConsumerPluginClass("TestEventConsumerPluginClass");
153         assertNotEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS,
154                 jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
155     }
156
157     @Test
158     public void testSetEventProducerPluginClass() {
159         assertEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS,
160                 jmsCarrierTechnologyParameters.getEventProducerPluginClass());
161         jmsCarrierTechnologyParameters.setEventProducerPluginClass("TestEventProducerPluginClass");
162         assertNotEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS,
163                 jmsCarrierTechnologyParameters.getEventProducerPluginClass());
164     }
165
166     @Test
167     public void testSetLabel() {
168         assertEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
169         jmsCarrierTechnologyParameters.setLabel("TestLable");
170         assertNotEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
171
172     }
173
174     @Test
175     public void testSetObjectMessageSending() {
176         assertTrue(jmsCarrierTechnologyParameters.isObjectMessageSending());
177         jmsCarrierTechnologyParameters.setObjectMessageSending(!DEFAULT_TO_OBJECT_MSG_SENDING);
178         assertFalse(jmsCarrierTechnologyParameters.isObjectMessageSending());
179     }
180
181     @Test
182     public void testSetProducerTopic() {
183         assertEquals(DEFAULT_PRODUCER_TOPIC, jmsCarrierTechnologyParameters.getProducerTopic());
184         jmsCarrierTechnologyParameters.setProducerTopic("");
185         result = jmsCarrierTechnologyParameters.validate();
186         assertFalse(result.getStatus().isValid());
187     }
188
189     @Test
190     public void testSetProviderUrl() {
191         assertEquals(DEFAULT_PROVIDER_URL, jmsCarrierTechnologyParameters.getProviderUrl());
192         jmsCarrierTechnologyParameters.setProviderUrl(null);
193         result = jmsCarrierTechnologyParameters.validate();
194         assertFalse(result.getStatus().isValid());
195     }
196
197     @Test
198     public void testSetSecurityCredentials() {
199         assertNull(jmsCarrierTechnologyParameters.getSecurityCredentials());
200         jmsCarrierTechnologyParameters.setSecurityCredentials("");
201         result = jmsCarrierTechnologyParameters.validate();
202         assertFalse(result.getStatus().isValid());
203     }
204
205     @Test
206     public void testSetSecurityPrincipal() {
207         assertEquals(DEFAULT_SECURITY_PRINCIPAL,
208                 jmsCarrierTechnologyParameters.getSecurityPrincipal());
209         jmsCarrierTechnologyParameters.setSecurityPrincipal(null);
210         result = jmsCarrierTechnologyParameters.validate();
211         assertFalse(result.getStatus().isValid());
212     }
213
214     @Test
215     public void testSetInitialContextFactory() {
216
217         assertEquals(DEFAULT_INITIAL_CTXT_FACTORY,
218                 jmsCarrierTechnologyParameters.getInitialContextFactory());
219
220         jmsCarrierTechnologyParameters.setInitialContextFactory(null);
221         result = jmsCarrierTechnologyParameters.validate();
222         assertFalse(result.getStatus().isValid());
223
224         jmsCarrierTechnologyParameters.setInitialContextFactory("TestInitialContextFactory");
225         assertNotEquals(DEFAULT_INITIAL_CTXT_FACTORY,
226                 jmsCarrierTechnologyParameters.getInitialContextFactory());
227     }
228
229     @Test(expected = ParameterRuntimeException.class)
230     public void testSetName() {
231         jmsCarrierTechnologyParameters.setName("TestName");
232     }
233 }