18fc1f63ed116ad1905af3debb6d99d7292be245
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Samsung. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.assertTrue;
29
30 import java.util.Base64;
31 import java.util.Properties;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.GroupValidationResult;
35 import org.onap.policy.common.parameters.ParameterRuntimeException;
36
37 public class JmsCarrierTechnologyParametersTest {
38
39     JmsCarrierTechnologyParameters jmsCarrierTechnologyParameters = null;
40     Properties jmsProducerProperties = null;
41     Properties jmsConsumerProperties = null;
42     GroupValidationResult result = null;
43
44     public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
45
46     public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS =
47             ApexJmsProducer.class.getCanonicalName();
48
49     public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS =
50             ApexJmsConsumer.class.getCanonicalName();
51
52     private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
53     private static final String DEFAULT_INITIAL_CTXT_FACTORY =
54             "org.jboss.naming.remote.client.InitialContextFactory";
55     private static final String DEFAULT_PROVIDER_URL = "remote://localhost:4447";
56     private static final String DEFAULT_SECURITY_PRINCIPAL = "userid";
57     private static final String DEFAULT_SECURITY_CREDENTIALS = "cGFzc3dvcmQ=";
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         assertTrue(result.getStatus().isValid());
78     }
79
80     @Test
81     public void testJmsCarrierTechnologyParameters() {
82         assertNotNull(jmsCarrierTechnologyParameters);
83     }
84
85     @Test
86     public void testGetJmsProducerProperties() {
87         assertNotNull(jmsCarrierTechnologyParameters.getJmsConsumerProperties());
88     }
89
90     @Test
91     public void testGetJmsConsumerProperties() {
92         assertNotNull(jmsCarrierTechnologyParameters.getJmsProducerProperties());
93     }
94
95     @Test
96     public void testEqualityOfJmsConsumerAndProducerProperties() {
97         assertEquals(jmsCarrierTechnologyParameters.getJmsProducerProperties(),
98                 jmsCarrierTechnologyParameters.getJmsConsumerProperties());
99     }
100
101     @Test
102     public void testGetConnectionFactory() {
103         assertEquals(DEFAULT_CONNECTION_FACTORY,
104                 jmsCarrierTechnologyParameters.getConnectionFactory());
105     }
106
107     @Test
108     public void testSetConnectionFactory() {
109         jmsCarrierTechnologyParameters.setConnectionFactory("QueueConnectionFactory");
110         assertNotEquals(DEFAULT_CONNECTION_FACTORY,
111                 jmsCarrierTechnologyParameters.getConnectionFactory());
112     }
113
114     @Test
115     public void testSetConsumerTopic() {
116         assertEquals(DEFAULT_CONSUMER_TOPIC, jmsCarrierTechnologyParameters.getConsumerTopic());
117         jmsCarrierTechnologyParameters.setConsumerTopic(null);
118         result = jmsCarrierTechnologyParameters.validate();
119         assertFalse(result.getStatus().isValid());
120     }
121
122     @Test
123     public void testSetConsumerWaitTime() {
124         assertEquals(DEFAULT_CONSUMER_WAIT_TIME,
125                 jmsCarrierTechnologyParameters.getConsumerWaitTime());
126         jmsCarrierTechnologyParameters.setConsumerWaitTime(-1);
127         assertNotEquals(DEFAULT_CONSUMER_WAIT_TIME,
128                 jmsCarrierTechnologyParameters.getConsumerWaitTime());
129     }
130
131     @Test
132     public void testSetEventConsumerPluginClass() {
133         assertEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS,
134                 jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
135         jmsCarrierTechnologyParameters.setEventConsumerPluginClass("TestEventConsumerPluginClass");
136         assertNotEquals(JMS_EVENT_CONSUMER_PLUGIN_CLASS,
137                 jmsCarrierTechnologyParameters.getEventConsumerPluginClass());
138     }
139
140     @Test
141     public void testSetEventProducerPluginClass() {
142         assertEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS,
143                 jmsCarrierTechnologyParameters.getEventProducerPluginClass());
144         jmsCarrierTechnologyParameters.setEventProducerPluginClass("TestEventProducerPluginClass");
145         assertNotEquals(JMS_EVENT_PRODUCER_PLUGIN_CLASS,
146                 jmsCarrierTechnologyParameters.getEventProducerPluginClass());
147     }
148
149     @Test
150     public void testSetLabel() {
151         assertEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
152         jmsCarrierTechnologyParameters.setLabel("TestLable");
153         assertNotEquals(JMS_CARRIER_TECHNOLOGY_LABEL, jmsCarrierTechnologyParameters.getLabel());
154
155     }
156
157     @Test
158     public void testSetObjectMessageSending() {
159         assertTrue(jmsCarrierTechnologyParameters.isObjectMessageSending());
160         jmsCarrierTechnologyParameters.setObjectMessageSending(!DEFAULT_TO_OBJECT_MSG_SENDING);
161         assertFalse(jmsCarrierTechnologyParameters.isObjectMessageSending());
162     }
163
164     @Test
165     public void testSetProducerTopic() {
166         assertEquals(DEFAULT_PRODUCER_TOPIC, jmsCarrierTechnologyParameters.getProducerTopic());
167         jmsCarrierTechnologyParameters.setProducerTopic("");
168         result = jmsCarrierTechnologyParameters.validate();
169         assertFalse(result.getStatus().isValid());
170     }
171
172     @Test
173     public void testSetProviderUrl() {
174         assertEquals(DEFAULT_PROVIDER_URL, jmsCarrierTechnologyParameters.getProviderUrl());
175         jmsCarrierTechnologyParameters.setProviderUrl(null);
176         result = jmsCarrierTechnologyParameters.validate();
177         assertFalse(result.getStatus().isValid());
178     }
179
180     @Test
181     public void testSetSecurityCredentials() {
182         assertEquals(
183                 new String(Base64.getDecoder().decode(DEFAULT_SECURITY_CREDENTIALS.getBytes())),
184                 jmsCarrierTechnologyParameters.getSecurityCredentials());
185         jmsCarrierTechnologyParameters.setSecurityCredentials("");
186         result = jmsCarrierTechnologyParameters.validate();
187         assertFalse(result.getStatus().isValid());
188     }
189
190     @Test
191     public void testSetSecurityPrincipal() {
192         assertEquals(DEFAULT_SECURITY_PRINCIPAL,
193                 jmsCarrierTechnologyParameters.getSecurityPrincipal());
194         jmsCarrierTechnologyParameters.setSecurityPrincipal(null);
195         result = jmsCarrierTechnologyParameters.validate();
196         assertFalse(result.getStatus().isValid());
197     }
198
199     @Test
200     public void testSetInitialContextFactory() {
201
202         assertEquals(DEFAULT_INITIAL_CTXT_FACTORY,
203                 jmsCarrierTechnologyParameters.getInitialContextFactory());
204
205         jmsCarrierTechnologyParameters.setInitialContextFactory(null);
206         result = jmsCarrierTechnologyParameters.validate();
207         assertFalse(result.getStatus().isValid());
208
209         jmsCarrierTechnologyParameters.setInitialContextFactory("TestInitialContextFactory");
210         assertNotEquals(DEFAULT_INITIAL_CTXT_FACTORY,
211                 jmsCarrierTechnologyParameters.getInitialContextFactory());
212     }
213
214     @Test(expected = ParameterRuntimeException.class)
215     public void testSetName() {
216         jmsCarrierTechnologyParameters.setName("TestName");
217     }
218 }