<groupId>com.github.fge</groupId>
<artifactId>json-patch</artifactId>
<version>1.9</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <version>4.3.16.RELEASE</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-test</artifactId>
+ <version>1.5.1.RELEASE</version>
+ <scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
import org.apache.activemq.command.ActiveMQQueue;
import org.onap.aai.dmaap.AAIDmaapEventJMSConsumer;
import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
+import org.springframework.http.HttpHeaders;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import javax.annotation.PostConstruct;
+import org.springframework.web.client.RestTemplate;
@Profile("dmaap")
@Configuration
public class DmaapConfig {
+ @Autowired
+ private ApplicationContext ctx;
+
+ @Autowired
+ @Qualifier("dmaapRestTemplate")
+ private RestTemplate dmaapRestTemplate;
+
+ @Autowired
+ @Qualifier("dmaapHeaders")
+ private HttpHeaders dmaapHeaders;
+
@Value("${jms.bind.address}")
private String bindAddress;
return new AAIDmaapEventJMSProducer();
}
- @Bean
+ @Bean(name="jmsConsumer")
public AAIDmaapEventJMSConsumer jmsConsumer() throws Exception {
- return new AAIDmaapEventJMSConsumer();
+ return new AAIDmaapEventJMSConsumer(ctx.getEnvironment(), dmaapRestTemplate, dmaapHeaders);
}
@Bean
@Value("${dmaap.ribbon.contentType:application/json}")
private String contentType;
- @Bean
+ @Bean(name="dmaapRestTemplate")
public RestTemplate dmaapRestTemplate(){
return new RestTemplate();
}
- @Bean
+ @Bean(name="dmaapHeaders")
public HttpHeaders dmaapHeaders() throws UnsupportedEncodingException
{
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
-}
+}
\ No newline at end of file
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
+import java.util.Objects;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
import org.apache.log4j.MDC;
import org.json.JSONException;
import org.json.JSONObject;
-import org.onap.aai.config.SpringContextAware;
import org.onap.aai.logging.LogFormatTools;
import org.onap.aai.logging.LoggingContext;
import org.onap.aai.logging.LoggingContext.LoggingField;
import org.onap.aai.logging.LoggingContext.StatusCode;
-import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.client.RestTemplate;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageListener;
-import javax.jms.TextMessage;
-
public class AAIDmaapEventJMSConsumer implements MessageListener {
private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAIDmaapEventJMSConsumer.class);
private Environment environment;
- public AAIDmaapEventJMSConsumer() {
- ApplicationContext applicationContext = SpringContextAware.getApplicationContext();
-
- if(applicationContext != null){
- restTemplate = (RestTemplate) applicationContext.getBean("dmaapRestTemplate");
- httpHeaders = (HttpHeaders) applicationContext.getBean("dmaapHeaders");
- environment = applicationContext.getEnvironment();
- }
+ public AAIDmaapEventJMSConsumer(Environment environment, RestTemplate restTemplate, HttpHeaders httpHeaders) {
+ Objects.nonNull(environment);
+ Objects.nonNull(restTemplate);
+ Objects.nonNull(httpHeaders);
+ this.environment = environment;
+ this.restTemplate = restTemplate;
+ this.httpHeaders = httpHeaders;
}
@Override
* 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
+ * 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,
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.json.JSONObject;
-import org.onap.aai.config.SpringContextAware;
import org.onap.aai.util.AAIConfig;
-import org.springframework.context.ApplicationContext;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.core.JmsTemplate;
private JmsTemplate jmsTemplate;
- private ApplicationContext applicationContext;
-
public AAIDmaapEventJMSProducer() {
if(AAIConfig.get("aai.jms.enable", "true").equals("true")){
this.jmsTemplate = new JmsTemplate();
--- /dev/null
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Nokia. All rights reserved.
+ * ================================================================================
+ * 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.aai.config;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.activemq.broker.BrokerService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.aai.dmaap.AAIDmaapEventJMSConsumer;
+import org.onap.aai.dmaap.AAIDmaapEventJMSProducer;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.support.AnnotationConfigContextLoader;
+
+/**
+ * Created by Bogumil Zebek on 6/25/18.
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = {DmaapConfig.class,
+ EventClientPublisher.class}, loader = AnnotationConfigContextLoader.class)
+@ActiveProfiles(profiles = "dmaap")
+public class AAIDmaapEventJMSConsumerBeanTest {
+
+ @MockBean
+ AAIDmaapEventJMSProducer jmsProducer;
+
+ @MockBean
+ BrokerService brokerService;
+
+ @MockBean
+ ActiveMQConnectionFactory activeMQConnectionFactory;
+
+ @Autowired
+ @Qualifier("jmsConsumer")
+ AAIDmaapEventJMSConsumer jmsConsumer;
+
+ @Autowired
+ private ApplicationContext ctx;
+
+ @Test
+ public void shouldCreateJmsConsumerProperly_allDependenciesInjectedByContainer() {
+ assertNotNull(jmsConsumer);
+ }
+
+}