Add WorkflowController integration test 58/141258/3 master
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Thu, 12 Jun 2025 14:36:29 +0000 (16:36 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Fri, 13 Jun 2025 06:49:24 +0000 (08:49 +0200)
- add integration test that tests WorkflowController
  with full spring context and cassandra backend
- use mvn antrun plugin to merge and copy cql files from
  init module to test for data base initialization

Issue-ID: SDC-4737
Change-Id: Ice71adb6c940445f922bb3517b15e85df904c000
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
.gitignore
sdc-workflow-designer-be/pom.xml
sdc-workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/ArtifactAssociationHandlerTest.java
sdc-workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerIntegrationTest.java [new file with mode: 0644]

index 35995a5..8d88cea 100644 (file)
@@ -16,3 +16,4 @@ dist
 *.log
 *.war
 .vscode
 *.log
 *.war
 .vscode
+sdc-workflow-designer-be/src/test/resources/db/*.cql
index 2833efb..5919a5c 100644 (file)
@@ -29,6 +29,7 @@
         <junitJupiter.version>5.9.1</junitJupiter.version>
         <junit.platform.version>1.9.0</junit.platform.version>
         <janino.version>3.1.10</janino.version>
         <junitJupiter.version>5.9.1</junitJupiter.version>
         <junit.platform.version>1.9.0</junit.platform.version>
         <janino.version>3.1.10</janino.version>
+        <testcontainers.version>1.19.1</testcontainers.version>
     </properties>
 
     <dependencyManagement>
     </properties>
 
     <dependencyManagement>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-tomcat</artifactId>
                 </exclusion>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-tomcat</artifactId>
                 </exclusion>
-                <exclusion>
-                    <groupId>org.springframework</groupId>
-                    <artifactId>spring-web</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>org.springframework</groupId>
-                    <artifactId>spring-core</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>org.springframework</groupId>
-                    <artifactId>spring-webmvc</artifactId>
-                </exclusion>
             </exclusions>
         </dependency>
         <dependency>
             </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
-            <exclusions>
-                <exclusion>
-                    <artifactId>asm</artifactId>
-                    <groupId>org.ow2.asm</groupId>
-                </exclusion>
-                <exclusion>
-                    <groupId>junit</groupId>
-                    <artifactId>junit</artifactId>
-                </exclusion>
-                <!-- <exclusion>
-                    <groupId>org.springframework</groupId>
-                    <artifactId>spring-core</artifactId>
-                </exclusion> -->
-                <exclusion>
-                    <groupId>org.junit.vintage</groupId>
-                    <artifactId>junit-vintage-engine</artifactId>
-                </exclusion>
-            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
         </dependency>
         <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>zusammen-commons-configuration</artifactId>
             <version>${zusammen.version}</version>
         </dependency>
             <artifactId>zusammen-commons-configuration</artifactId>
             <version>${zusammen.version}</version>
         </dependency>
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
+            <version>${testcontainers.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>cassandra</artifactId>
+            <version>${testcontainers.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>junit-jupiter</artifactId>
+            <version>${testcontainers.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
     </dependencies>
 
     <build>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>3.1.2</version>
             </plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>3.1.2</version>
             </plugin>
+            <plugin>
+                <artifactId>maven-antrun-plugin</artifactId>
+                <version>3.0.0</version>
+                <executions>
+                    <execution>
+                        <phase>
+                            generate-sources
+                        </phase>
+                        <!-- This copies and merges all cql files from the designer-init module
+                         into a 'init.cql' file the resources folder in the tests.
+                         This is used by the WorkflowControllerIntegrationTest to initialize the
+                         cassandra container that is used for the test. -->
+                        <configuration>
+                            <target>
+                                <concat destfile="${basedir}/src/test/resources/db/init.cql"
+                                        force="yes">
+                                    <fileset dir="../sdc-workflow-designer-init/src/main/docker">
+                                        <include name="*.cql"></include>
+                                    </fileset>
+                                </concat>
+                            </target>
+                        </configuration>
+                        <goals>
+                            <goal>run</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
 
         </plugins>
     </build>
 
index 131e14b..ba203ef 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2018 European Support Limited
 /*
  * Copyright © 2018 European Support Limited
+ * Modifications Copyright © 2025 Deutsche Telekom
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,44 +27,26 @@ import java.io.InputStream;
 import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.apache.commons.io.IOUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
 import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto;
 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.onap.sdc.workflow.api.types.dto.ArtifactDeliveriesRequestDto;
 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.mock.mockito.MockBean;
 import org.springframework.boot.web.client.RestTemplateBuilder;
 import org.springframework.boot.web.client.RestTemplateBuilder;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.HttpEntity;
 import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
-import org.springframework.stereotype.Component;
+import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.TestPropertySource;
 import org.springframework.test.context.TestPropertySource;
-import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
 import org.springframework.web.client.RestTemplate;
 
 import org.springframework.web.client.RestTemplate;
 
-
-@SpringJUnitConfig(
-        classes = {ArtifactAssociationHandlerTest.RestBuilderMockProvider.class, ArtifactAssociationService.class})
+@DirtiesContext
+@SpringBootTest(classes = {ArtifactAssociationService.class})
 @TestPropertySource(locations = "classpath:application-test.properties")
 @TestPropertySource(locations = "classpath:application-test.properties")
-@Component("ArtifactAssociationHandlerTest")
 public class ArtifactAssociationHandlerTest {
 
 public class ArtifactAssociationHandlerTest {
 
-    @Configuration
-    static class RestBuilderMockProvider {
-
-        @Bean
-        public RestTemplateBuilder templateBuilder() {
-            return Mockito.mock(RestTemplateBuilder.class);
-        }
-
-        @Bean
-        public RestTemplate restTemplate() {
-            return Mockito.mock(RestTemplate.class);
-        }
-    }
-
     private static final String FILE_NAME = "fileName.txt";
     private static final String USER_ID = "cs0008";
     private static final String END_POINT =
     private static final String FILE_NAME = "fileName.txt";
     private static final String USER_ID = "cs0008";
     private static final String END_POINT =
@@ -82,7 +65,9 @@ public class ArtifactAssociationHandlerTest {
     @Value("${sdc.be.external.password}")
     private String sdcPassword;
 
     @Value("${sdc.be.external.password}")
     private String sdcPassword;
 
-    @Autowired
+    @MockBean
+    private RestTemplateBuilder restTemplateBuilder;
+    @MockBean
     private RestTemplate restClientMock;
 
 
     private RestTemplate restClientMock;
 
 
diff --git a/sdc-workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerIntegrationTest.java b/sdc-workflow-designer-be/src/test/java/org/onap/sdc/workflow/api/WorkflowControllerIntegrationTest.java
new file mode 100644 (file)
index 0000000..21ea64d
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2025 Deutsche Telekom
+ *
+ * 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.
+ */
+
+package org.onap.sdc.workflow.api;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.boot.web.server.LocalServerPort;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.ResponseEntity;
+import org.springframework.test.context.DynamicPropertyRegistry;
+import org.springframework.test.context.DynamicPropertySource;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriComponentsBuilder;
+import org.testcontainers.containers.CassandraContainer;
+import org.testcontainers.junit.jupiter.Container;
+import org.testcontainers.junit.jupiter.Testcontainers;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import lombok.SneakyThrows;
+
+@Testcontainers
+@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
+    properties = {
+        "spring.data.cassandra.contact-points=localhost",
+        "spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS"
+})
+public class WorkflowControllerIntegrationTest {
+
+    @LocalServerPort
+    int port;
+
+    ObjectMapper mapper = new ObjectMapper();
+
+    @Autowired
+    RestTemplateBuilder builder;
+    RestTemplate restTemplate;
+
+    @BeforeEach
+    void setup() {
+        this.restTemplate = builder
+            .rootUri("http://localhost:" + port)
+            .defaultHeader(HttpHeaders.ACCEPT, "application/json")
+            .build();
+    }
+
+
+
+    @Container
+    private static CassandraContainer<?> cassandraContainer
+        = new CassandraContainer<>("cassandra:3.11.2")
+            .withInitScript("db/init.cql")
+            .withExposedPorts(9042);
+
+    @DynamicPropertySource
+    static void configureProperties(DynamicPropertyRegistry registry) {
+        registry.add("spring.data.cassandra.port", () -> cassandraContainer.getFirstMappedPort());
+    }
+
+    @Test
+    @SneakyThrows
+    public void someTest() {
+        String baseUrl = "/wf/workflows";
+        String queryParams = UriComponentsBuilder
+                .fromUriString(baseUrl)
+                .queryParam("sort", "name:asc")
+                .queryParam("limit", 31)
+                .queryParam("offset", 0)
+                .queryParam("archiving", "ACTIVE")
+                .toUriString();
+
+        ResponseEntity<String> response = restTemplate.getForEntity(queryParams, String.class);
+        assert(response.getStatusCode().is2xxSuccessful());
+
+    }
+
+}