Convert tests with Mocking to SpringBootTest 48/78548/1
authormark.j.leonard <mark.j.leonard@gmail.com>
Fri, 15 Feb 2019 11:16:58 +0000 (11:16 +0000)
committermark.j.leonard <mark.j.leonard@gmail.com>
Fri, 15 Feb 2019 11:16:58 +0000 (11:16 +0000)
Remove the dependency on Mockito and instead use the Spring framework
for testing with Mocks. This allows greater control over the properties
that are used by the test classes.

Change-Id: I3a72d41f3015f831f425f99cf44417bccdb2c823
Issue-ID: AAI-2057
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
pom.xml
src/test/java/org/onap/aai/validation/TestApplication.java
src/test/java/org/onap/aai/validation/config/TestTopicConfig.java
src/test/java/org/onap/aai/validation/modeldriven/validator/TestModelDrivenValidator.java
src/test/java/org/onap/aai/validation/services/TestInfoService.java
src/test/resources/test-application.properties [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 18f0139..dbffda5 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -213,19 +213,18 @@ limitations under the License.
                        <artifactId>junit</artifactId>
                        <scope>test</scope>
                </dependency>
+               
+        <dependency>
+            <groupId>org.hamcrest</groupId>
+            <artifactId>hamcrest-library</artifactId>
+            <scope>test</scope>
+        </dependency>
 
-               <dependency>
-                       <groupId>org.mockito</groupId>
-                       <artifactId>mockito-all</artifactId>
-                       <version>1.10.19</version>
-                       <scope>test</scope>
-               </dependency>
-
-               <dependency>
-                       <groupId>org.hamcrest</groupId>
-                       <artifactId>hamcrest-library</artifactId>
-                       <scope>test</scope>
-               </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
 
        </dependencies>
 
index 26d480a..3dfa177 100644 (file)
@@ -1,7 +1,10 @@
 /**
- * ============LICENSE_START===================================================
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
  * Copyright (c) 2018-2019 European Software Marketing Ltd.
- * ============================================================================
+ * ================================================================================
  * 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
@@ -13,7 +16,7 @@
  * 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=====================================================
+ * ============LICENSE_END=========================================================
  */
 package org.onap.aai.validation;
 
@@ -21,11 +24,17 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
 
 /**
  * Invoke the Spring Boot Application (primarily for code coverage).
  *
  */
+@SpringBootTest(classes = ValidationServiceApplication.class)
+@TestPropertySource(locations = {"classpath:oxm-reader/schemaIngest.properties", "classpath:application.properties"})
+@ContextConfiguration(locations = {"classpath:validation-service-beans.xml"})
 public class TestApplication {
 
     @Rule
@@ -35,6 +44,7 @@ public class TestApplication {
     public void init() {
         System.setProperty("CONFIG_HOME", "src/test/resources/model-validation/instance-validator");
         System.setProperty("APP_HOME", ".");
+        System.setProperty("schema.translator.list", "config");
         System.clearProperty("KEY_STORE_PASSWORD");
     }
 
@@ -46,4 +56,11 @@ public class TestApplication {
         ValidationServiceApplication.main(null);
     }
 
+    @Test
+    public void testApplicationWithNullKeyStorePassword() {
+        expectedEx.expect(IllegalArgumentException.class);
+        expectedEx.expectMessage("roperty KEY_STORE_PASSWORD not set");
+        ValidationServiceApplication.main(new String[] {});
+    }
+
 }
index 30e28d8..f323331 100644 (file)
@@ -1,20 +1,24 @@
-/*
- * ============LICENSE_START===================================================
- * Copyright (c) 2018 Amdocs
- * ============================================================================
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2018-2019 European Software Marketing Ltd.
+ * ================================================================================
  * 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
+ *       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=====================================================
+ * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.validation.config;
 
 import static org.hamcrest.Matchers.containsInAnyOrder;
@@ -23,17 +27,21 @@ import static org.junit.Assert.assertThat;
 
 import java.util.List;
 import java.util.Properties;
-import javax.annotation.Resource;
 import javax.inject.Inject;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.onap.aai.validation.config.TopicConfig.Topic;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Import;
 import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
-
+@SpringBootTest
 @RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = {"classpath:/topic-config/test-validation-service-beans.xml"})
+@Import(TopicPropertiesConfig.class)
+@TestPropertySource(locations = { "classpath:test-application.properties" })
+@ContextConfiguration(locations = { "classpath:topic-config/test-validation-service-beans.xml" })
 public class TestTopicConfig {
 
     static {
@@ -41,26 +49,22 @@ public class TestTopicConfig {
     }
 
     @Inject
-    private TopicConfig topicConfigurations;
+    private TopicConfig topicConfig;
 
-    @Resource(name = "topicProperties")
+    @Inject
     private Properties topicProperties;
 
-
     @Test
     public void testGetTopicProperties() throws Exception {
         assertThat(topicProperties.getProperty("aai-event.name"), is("aai-event"));
         assertThat(topicProperties.getProperty("aai-data-export.name"), is("aai-data-export"));
     }
 
-
-
     @Test
     public void testGetConsumerTopicsFromTopicConfig() throws Exception {
-        assertThat(topicConfigurations.getConsumerTopicNames(), containsInAnyOrder("aai-event", "aai-data-export"));
+        assertThat(topicConfig.getConsumerTopicNames(), containsInAnyOrder("aai-event", "aai-data-export"));
     }
 
-
     @Test
     public void testGetConsumerTopicConfigurationObjects() throws Exception {
         Topic eventTopic = new TopicConfig("aai-event", "aai-data-integrity").new Topic();
@@ -81,7 +85,7 @@ public class TestTopicConfig {
         exportTopic.setConsumerId("export-dummy-consumer-id");
         exportTopic.setTransportType("export-dummy-transport-type");
 
-        List<Topic> consumerTopics = topicConfigurations.getConsumerTopics();
+        List<Topic> consumerTopics = topicConfig.getConsumerTopics();
 
         assertThat(consumerTopics, containsInAnyOrder(eventTopic, exportTopic));
     }
@@ -96,10 +100,8 @@ public class TestTopicConfig {
         integrityTopic.setPassword("integrity-dummy-password");
         integrityTopic.setTransportType("integrity-dummy-transport-type");
 
-        List<Topic> publisherTopics = topicConfigurations.getPublisherTopics();
+        List<Topic> publisherTopics = topicConfig.getPublisherTopics();
 
         assertThat(publisherTopics, containsInAnyOrder(integrityTopic));
     }
-
-
 }
index de06357..5a58f41 100644 (file)
@@ -1,7 +1,10 @@
 /**
- * ============LICENSE_START===================================================
- * Copyright (c) 2018-2019 Amdocs
- * ============================================================================
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2018-2019 European Software Marketing Ltd.
+ * ================================================================================
  * 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
@@ -13,7 +16,7 @@
  * 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=====================================================
+ * ============LICENSE_END=========================================================
  */
 package org.onap.aai.validation.modeldriven.validator;
 
@@ -35,8 +38,6 @@ import org.dom4j.Element;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 import org.onap.aai.validation.controller.ValidationController;
@@ -46,10 +47,13 @@ import org.onap.aai.validation.modeldriven.parser.XMLModelParser;
 import org.onap.aai.validation.result.ValidationResult;
 import org.onap.aai.validation.result.Violation;
 import org.onap.aai.validation.test.util.TestUtil;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+@SpringBootTest
 @RunWith(SpringJUnit4ClassRunner.class)
 @TestPropertySource(locations = {"classpath:oxm-reader/schemaIngest.properties"})
 @ContextConfiguration(locations = {"classpath:model-validation/instance-validator/test-validation-service-beans.xml"})
@@ -63,10 +67,9 @@ public class TestModelDrivenValidator {
     private static final String MODEL_ID_ATTRIBUTE_MNV = "model-name-version-id";
     private static final String MODEL_ID_ATTRIBUTE_MID = "model-id";
 
-    @Mock
+    @MockBean
     private ModelCacheManager mockModelCacheManager;
 
-    @InjectMocks
     @Inject
     private ModelDrivenValidator modelDrivenValidator;
 
index ac9591a..f99748a 100644 (file)
@@ -1,7 +1,10 @@
 /**
- * ============LICENSE_START===================================================
- * Copyright (c) 2018-2019 Amdocs
- * ============================================================================
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (c) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2018-2019 European Software Marketing Ltd.
+ * ================================================================================
  * 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
@@ -13,7 +16,7 @@
  * 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=====================================================
+ * ============LICENSE_END=========================================================
  */
 package org.onap.aai.validation.services;
 
@@ -37,13 +40,15 @@ import org.onap.aai.validation.exception.ValidationServiceException;
 import org.onap.aai.validation.publisher.MockEventPublisher;
 import org.onap.aai.validation.test.util.TestEntity;
 import org.onap.aai.validation.test.util.TestUtil;
+import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+@SpringBootTest
 @RunWith(SpringJUnit4ClassRunner.class)
-@TestPropertySource(locations = {"classpath:oxm-reader/schemaIngest.properties", "classpath:application.properties"})
-@ContextConfiguration(locations = {"classpath:/info-service/test-validation-service-beans.xml"})
+@TestPropertySource(locations = { "classpath:oxm-reader/schemaIngest.properties", "classpath:application.properties" })
+@ContextConfiguration(locations = { "classpath:/info-service/test-validation-service-beans.xml" })
 public class TestInfoService {
 
     static {
@@ -51,9 +56,9 @@ public class TestInfoService {
     }
 
     enum TestData {
-        // @formatter:off
-        VSERVER("rule-driven-validator/test_events/vserver-create-event.json");
-        // @formatter:on
+        VSERVER(
+                "rule-driven-validator/test_events/vserver-create-event.json"
+        );
 
         private String filename;
 
diff --git a/src/test/resources/test-application.properties b/src/test/resources/test-application.properties
new file mode 100644 (file)
index 0000000..38b2962
--- /dev/null
@@ -0,0 +1,6 @@
+consumer.topic.names=aai-event,aai-data-export
+publisher.topic.names=aai-data-integrity
+
+topics.properties.location=src/test/resources/topic-config/
+
+server.ssl.key-store=