From f626a9868b89d7ce928504c131d3d4109accc947 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Mon, 25 Jun 2018 13:44:15 +0200 Subject: Fix component creation Change-Id: I08606ec83b53cd87005f7d52301e0fe8792d744e Issue-ID: AAI-1236 Signed-off-by: Bogumil Zebek --- aai-core/pom.xml | 12 ++++ .../main/java/org/onap/aai/config/DmaapConfig.java | 20 ++++++- .../org/onap/aai/config/EventClientPublisher.java | 4 +- .../org/onap/aai/config/SpringContextAware.java | 2 +- .../onap/aai/dmaap/AAIDmaapEventJMSConsumer.java | 27 ++++----- .../onap/aai/dmaap/AAIDmaapEventJMSProducer.java | 6 +- .../config/AAIDmaapEventJMSConsumerBeanTest.java | 70 ++++++++++++++++++++++ 7 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 aai-core/src/test/java/org/onap/aai/config/AAIDmaapEventJMSConsumerBeanTest.java (limited to 'aai-core') diff --git a/aai-core/pom.xml b/aai-core/pom.xml index c6224f1d..2467aa87 100644 --- a/aai-core/pom.xml +++ b/aai-core/pom.xml @@ -574,6 +574,18 @@ com.github.fge json-patch 1.9 + + + org.springframework + spring-test + 4.3.16.RELEASE + test + + + org.springframework.boot + spring-boot-test + 1.5.1.RELEASE + test org.powermock diff --git a/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java b/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java index 7634b74c..b2b5bccd 100644 --- a/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java +++ b/aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java @@ -24,20 +24,36 @@ import org.apache.activemq.broker.BrokerService; 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; @@ -89,9 +105,9 @@ public class DmaapConfig { return new AAIDmaapEventJMSProducer(); } - @Bean + @Bean(name="jmsConsumer") public AAIDmaapEventJMSConsumer jmsConsumer() throws Exception { - return new AAIDmaapEventJMSConsumer(); + return new AAIDmaapEventJMSConsumer(ctx.getEnvironment(), dmaapRestTemplate, dmaapHeaders); } @Bean diff --git a/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java b/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java index 1268bf84..9567dd87 100644 --- a/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java +++ b/aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java @@ -67,12 +67,12 @@ public class EventClientPublisher { @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 { diff --git a/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java b/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java index db8de9ec..d8043d4c 100644 --- a/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java +++ b/aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java @@ -37,4 +37,4 @@ public class SpringContextAware implements ApplicationContextAware { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } -} +} \ No newline at end of file diff --git a/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSConsumer.java b/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSConsumer.java index 5e28b3af..49cc0e90 100644 --- a/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSConsumer.java +++ b/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSConsumer.java @@ -21,26 +21,24 @@ package org.onap.aai.dmaap; 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); @@ -51,14 +49,13 @@ public class AAIDmaapEventJMSConsumer implements MessageListener { 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 diff --git a/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSProducer.java b/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSProducer.java index dc803b15..851a22fe 100644 --- a/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSProducer.java +++ b/aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSProducer.java @@ -8,7 +8,7 @@ * 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, @@ -22,9 +22,7 @@ package org.onap.aai.dmaap; 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; @@ -32,8 +30,6 @@ public class AAIDmaapEventJMSProducer implements MessageProducer { private JmsTemplate jmsTemplate; - private ApplicationContext applicationContext; - public AAIDmaapEventJMSProducer() { if(AAIConfig.get("aai.jms.enable", "true").equals("true")){ this.jmsTemplate = new JmsTemplate(); diff --git a/aai-core/src/test/java/org/onap/aai/config/AAIDmaapEventJMSConsumerBeanTest.java b/aai-core/src/test/java/org/onap/aai/config/AAIDmaapEventJMSConsumerBeanTest.java new file mode 100644 index 00000000..5ffd868e --- /dev/null +++ b/aai-core/src/test/java/org/onap/aai/config/AAIDmaapEventJMSConsumerBeanTest.java @@ -0,0 +1,70 @@ +/** + * ============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); + } + +} -- cgit 1.2.3-korg