Fix component creation 65/56265/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Mon, 25 Jun 2018 11:44:15 +0000 (13:44 +0200)
committerBogumil Zebek <bogumil.zebek@nokia.com>
Thu, 12 Jul 2018 06:26:38 +0000 (08:26 +0200)
Change-Id: I08606ec83b53cd87005f7d52301e0fe8792d744e
Issue-ID: AAI-1236
Signed-off-by: Bogumil Zebek <bogumil.zebek@nokia.com>
aai-core/pom.xml
aai-core/src/main/java/org/onap/aai/config/DmaapConfig.java
aai-core/src/main/java/org/onap/aai/config/EventClientPublisher.java
aai-core/src/main/java/org/onap/aai/config/SpringContextAware.java
aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSConsumer.java
aai-core/src/main/java/org/onap/aai/dmaap/AAIDmaapEventJMSProducer.java
aai-core/src/test/java/org/onap/aai/config/AAIDmaapEventJMSConsumerBeanTest.java [new file with mode: 0644]

index c6224f1..2467aa8 100644 (file)
             <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>
index 7634b74..b2b5bcc 100644 (file)
@@ -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
index 1268bf8..9567dd8 100644 (file)
@@ -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
     {
 
index db8de9e..d8043d4 100644 (file)
@@ -37,4 +37,4 @@ public class SpringContextAware implements ApplicationContextAware {
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
         context = applicationContext;
     }
-}
+}
\ No newline at end of file
index 5e28b3a..49cc0e9 100644 (file)
@@ -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
index dc803b1..851a22f 100644 (file)
@@ -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 (file)
index 0000000..5ffd868
--- /dev/null
@@ -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);
+    }
+
+}