Skip to content

Commit cb735a4

Browse files
committed
feat(QTDI-1914): Improve unit tests.
1 parent 523ed42 commit cb735a4

16 files changed

Lines changed: 232 additions & 82 deletions

File tree

component-runtime-manager/src/main/java/org/talend/sdk/component/runtime/manager/ComponentManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ default boolean ignoreBeamClassLoaderExclusions() {
22622262
}
22632263

22642264
/**
2265-
* Disable default built-in component classpath building mecanism. This is useful when relying on
2265+
* Disable default built-in component classpath building mechanism. This is useful when relying on
22662266
* a custom {@link ContainerClasspathContributor} handling it.
22672267
*
22682268
* @return true if the default dependencies descriptor (TALEND-INF/dependencies.txt) must be ignored.
@@ -2295,4 +2295,4 @@ public interface ContainerClasspathContributor {
22952295

22962296
Path resolve(String path);
22972297
}
2298-
}
2298+
}

sample-parent/sample-features/loading-analysis/loading-dependencies-common/pom.xml

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,30 @@
2929
<name>Component Runtime :: Sample Feature loading dependencies common</name>
3030

3131
<dependencies>
32-
<dependency>
33-
<groupId>org.apache.maven.resolver</groupId>
34-
<artifactId>maven-resolver-api</artifactId>
35-
<version>2.0.14</version>
36-
<scope>provided</scope>
37-
</dependency>
3832
<dependency>
3933
<groupId>org.talend.sdk.component</groupId>
40-
<artifactId>sample-connector</artifactId>
34+
<artifactId>component-runtime-manager</artifactId>
4135
<version>${project.version}</version>
42-
<scope>test</scope>
36+
<scope>provided</scope>
4337
</dependency>
4438
<dependency>
45-
<groupId>org.talend.sdk.component</groupId>
46-
<artifactId>component-runtime-manager</artifactId>
47-
<version>${project.version}</version>
39+
<groupId>org.apache.maven.resolver</groupId>
40+
<artifactId>maven-resolver-api</artifactId>
41+
<version>2.0.14</version>
42+
<!-- This one is provided since there is an explicit import -->
4843
<scope>provided</scope>
4944
</dependency>
45+
<!-- End of explicit dynamic dependencies because of an issue. -->
5046
</dependencies>
5147

5248
<build>
53-
<resources>
54-
<resource>
49+
<testResources>
50+
<testResource>
5551
<!-- This is to parse the test/resources/version.properties file -->
5652
<directory>src/test/resources</directory>
5753
<filtering>true</filtering>
58-
</resource>
59-
</resources>
54+
</testResource>
55+
</testResources>
6056
<plugins>
6157
<plugin>
6258
<groupId>org.apache.maven.plugins</groupId>

sample-parent/sample-features/loading-analysis/loading-dependencies-common/src/main/java/org/talend/sdk/component/sample/feature/loadinganalysis/service/AbstractDynamicDependenciesService.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public abstract class AbstractDynamicDependenciesService implements Serializable
9595
private Resolver resolver;
9696

9797
public Iterator<Record> loadIterator(final DynamicDependencyConfig dynamicDependencyConfig) {
98-
Schema schema = buildSchema(dynamicDependencyConfig);
98+
Schema schema = buildSchema();
9999

100100
List<Record> standardDependencies = loadStandardDependencies(dynamicDependencyConfig, schema);
101101
List<Record> additionalConnectors = loadConnectors(dynamicDependencyConfig, schema);
@@ -108,16 +108,14 @@ private List<Record> loadStandardDependencies(final DynamicDependencyConfig dyna
108108
List<Record> records = new ArrayList<>();
109109

110110
List<Dependency> dependencies = new ArrayList<>();
111-
// Add a class that should be imported by a 'standard' dependency (not a dynamic one)
112-
// to have an example from which classloaded it is loaded
113-
// In that case the version doesn't matter.
111+
112+
// Hardcoded dependency to check that 'static' dependencies from configuration are well loaded and assigned
114113
dependencies.add(new Dependency("org.talend.sdk.component.loading-analysis",
115114
"loading-dependencies-common",
116115
"N/A",
117116
"org.talend.sdk.component.sample.feature.loadinganalysis.config.Dependency"));
118117

119-
// This is a hardcoded dependency to check how are loaded dependencies
120-
// provided by the runtime
118+
// Hardcoded dependency to check that 'provided' dependencies are well loaded and assigned
121119
dependencies.add(new Dependency("org.talend.sdk.component",
122120
"component-runtime",
123121
"N/A",
@@ -149,11 +147,11 @@ private List<Record> loadStandardDependencies(final DynamicDependencyConfig dyna
149147
if ("maven-resolver-api".equals(dependency.getArtifactId())) {
150148
// This dependency is automatically added by additionalDependencies()
151149
checkAssignmentFromDynamicDependency();
152-
comment = "An instance has been instantiated and assigned.";
153-
} else if ("dynamic-dependencies-common".equals(dependency.getArtifactId())) {
154-
comment = "Check static dependency.";
150+
comment = "Hardcoded dynamic dependency test. The instantiated object has been assigned.";
151+
} else if ("loading-dependencies-common".equals(dependency.getArtifactId())) {
152+
comment = "Hardcoded 'static' dependency test.";
155153
} else if ("component-runtime".equals(dependency.getArtifactId())) {
156-
comment = "Check provided dependency.";
154+
comment = "Hardcoded provided dependency test.";
157155
}
158156

159157
} catch (ClassNotFoundException e) {
@@ -274,7 +272,7 @@ private Map<String, String> json2Map(final String json) {
274272
}
275273
}
276274

277-
protected Schema buildSchema(final DynamicDependencyConfig dynamicDependencyConfig) {
275+
public Schema buildSchema() {
278276
Schema.Builder builder = factory.newSchemaBuilder(Type.RECORD)
279277
.withEntry(factory.newEntryBuilder().withName(ENTRY_MAVEN).withType(Type.STRING).build())
280278
.withEntry(factory.newEntryBuilder().withName(ENTRY_CLASS).withType(Type.STRING).build())

sample-parent/sample-features/loading-analysis/loading-dependencies-common/src/test/java/org/talend/sdk/component/sample/feature/loadinganalysis/AbstractDynamicDependenciesServiceTest.java

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@
2828
import java.io.IOException;
2929
import java.io.InputStream;
3030
import java.util.ArrayList;
31+
import java.util.Collections;
3132
import java.util.Iterator;
3233
import java.util.List;
34+
import java.util.Map;
3335
import java.util.Properties;
36+
import java.util.stream.Collectors;
3437

3538
import org.junit.jupiter.api.Assertions;
3639
import org.junit.jupiter.api.BeforeEach;
3740
import org.junit.jupiter.api.Test;
3841
import org.talend.sdk.component.api.exception.ComponentException;
3942
import org.talend.sdk.component.api.record.Record;
43+
import org.talend.sdk.component.api.record.Schema;
44+
import org.talend.sdk.component.api.record.Schema.Entry;
45+
import org.talend.sdk.component.api.record.Schema.Type;
46+
import org.talend.sdk.component.sample.feature.loadinganalysis.config.Connector;
4047
import org.talend.sdk.component.sample.feature.loadinganalysis.config.Dependency;
4148
import org.talend.sdk.component.sample.feature.loadinganalysis.config.DynamicDependencyConfig;
4249
import org.talend.sdk.component.sample.feature.loadinganalysis.service.AbstractDynamicDependenciesService;
@@ -65,8 +72,50 @@ void testLoadIterator() {
6572
result.forEachRemaining(records::add);
6673
Assertions.assertEquals(4, records.size());
6774

68-
Record fourth = records.get(3);
75+
Record record = records.get(0);
6976
ResultDetails expected = new ResultDetails(
77+
false,
78+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
79+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
80+
"org.talend.sdk.component.loading-analysis:loading-dependencies-common:N/A",
81+
System.getProperty("user.dir"),
82+
true,
83+
"org/talend/sdk/component/sample/feature/loadinganalysis/config/Dependency.class",
84+
"Hardcoded 'static' dependency test.",
85+
"org.talend.sdk.component.sample.feature.loadinganalysis.config.Dependency",
86+
"commons-numbers-primes-1.2.jar");
87+
this.assertRecord(record, expected);
88+
89+
record = records.get(1);
90+
expected = new ResultDetails(
91+
false,
92+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
93+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
94+
"org.talend.sdk.component:component-runtime:N/A",
95+
System.getProperty("user.dir"),
96+
true,
97+
"org/talend/sdk/component/api/service/asyncvalidation/ValidationResult.class",
98+
"Hardcoded provided dependency test.",
99+
"org.talend.sdk.component.api.service.asyncvalidation.ValidationResult",
100+
"component-runtime-impl");
101+
this.assertRecord(record, expected);
102+
103+
record = records.get(2);
104+
expected = new ResultDetails(
105+
false,
106+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
107+
"jdk.internal.loader.ClassLoaders$AppClassLoader",
108+
"org.apache.maven.resolver:maven-resolver-api:2.0.14",
109+
System.getProperty("user.dir"),
110+
true,
111+
"org/eclipse/aether/artifact/DefaultArtifact.class",
112+
"Hardcoded dynamic dependency test. The instantiated object has been assigned.",
113+
"org.eclipse.aether.artifact.DefaultArtifact",
114+
"maven-resolver-api-2.0.14.jar");
115+
this.assertRecord(record, expected);
116+
117+
record = records.get(3);
118+
expected = new ResultDetails(
70119
false,
71120
"jdk.internal.loader.ClassLoaders$AppClassLoader",
72121
"jdk.internal.loader.ClassLoaders$AppClassLoader",
@@ -77,10 +126,30 @@ void testLoadIterator() {
77126
null,
78127
"org.apache.commons.numbers.primes.SmallPrimes",
79128
"commons-numbers-primes-1.2.jar");
80-
this.assertRecord(fourth, expected);
129+
this.assertRecord(record, expected);
130+
}
131+
132+
@Test
133+
void testGuessSchema4Input() {
134+
Schema schema = this.getService().buildSchema();
135+
Map<String, Entry> entries = schema.getAllEntries().collect(Collectors.toMap(Entry::getName, e -> e));
136+
Assertions.assertEquals(12, entries.size());
137+
Assertions.assertEquals(Type.BOOLEAN, entries.get("is_tck_container").getType());
138+
Assertions.assertEquals(Type.STRING, entries.get("root_repository").getType());
139+
Assertions.assertEquals(Type.STRING, entries.get("clazz_classloader").getType());
140+
Assertions.assertEquals(Type.STRING, entries.get("maven").getType());
141+
Assertions.assertEquals(Type.RECORD, entries.get("first_record").getType());
142+
Assertions.assertEquals(Type.BOOLEAN, entries.get("is_loaded").getType());
143+
Assertions.assertEquals(Type.STRING, entries.get("from_location").getType());
144+
Assertions.assertEquals(Type.STRING, entries.get("working_directory").getType());
145+
Assertions.assertEquals(Type.STRING, entries.get("comment").getType());
146+
Assertions.assertEquals(Type.STRING, entries.get("connector_classloader").getType());
147+
Assertions.assertEquals(Type.STRING, entries.get("clazz").getType());
148+
Assertions.assertEquals(Type.STRING, entries.get("runtime_classpath").getType());
81149
}
82150

83151
protected List<Dependency> getDynamicDependenciesConfigurationList() {
152+
// This dependency is added in dynamic dependencies list
84153
List<Dependency> depends = new ArrayList<>();
85154
Dependency depend = new Dependency();
86155
depend.setArtifactId("commons-numbers-primes");
@@ -91,6 +160,12 @@ protected List<Dependency> getDynamicDependenciesConfigurationList() {
91160
return depends;
92161
}
93162

163+
protected List<Connector> getDynamicDependenciesConnectorsConfigurationList() {
164+
// Return an empty list since currently this is not supported at unit test time.
165+
// It is not possible to load another connector in unit test.
166+
return Collections.emptyList();
167+
}
168+
94169
private void assertRecord(Record record, ResultDetails expected) {
95170
Assertions.assertNotNull(record);
96171

sample-parent/sample-features/loading-analysis/loading-dependencies-with-dataprepRunAnnotation/pom.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,27 @@
4141
<scope>test</scope>
4242
</dependency>
4343
<dependency>
44-
<!-- The dependency that simulate a dynamic one. -->
4544
<groupId>org.apache.commons</groupId>
4645
<artifactId>commons-numbers-primes</artifactId>
4746
<version>1.2</version>
4847
<scope>test</scope>
4948
</dependency>
49+
<dependency>
50+
<groupId>org.apache.maven.resolver</groupId>
51+
<artifactId>maven-resolver-api</artifactId>
52+
<version>2.0.14</version>
53+
<scope>test</scope>
54+
</dependency>
5055
</dependencies>
5156

5257
<build>
58+
<testResources>
59+
<testResource>
60+
<!-- This is to parse the TALEND-INF/dynamic-dependencies.properties file -->
61+
<directory>src/test/resources</directory>
62+
<filtering>true</filtering>
63+
</testResource>
64+
</testResources>
5365
<plugins>
5466
<plugin>
5567
<groupId>org.apache.maven.plugins</groupId>

sample-parent/sample-features/loading-analysis/loading-dependencies-with-dataprepRunAnnotation/src/main/java/org/talend/sdk/component/sample/feature/loadinganalysis/withDataprepRunAnnotation/service/DynamicDependenciesDataprepRunAnnotationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public List<String> getDynamicDependencies(@Option("theConfig") final Config con
4747

4848
@DiscoverSchemaExtended(DEPENDENCY_WITHDATAPREPRUN_ACTION)
4949
public Schema guessSchema4Input(final @Option("configuration") Config config) {
50-
return super.buildSchema(config);
50+
return super.buildSchema();
5151
}
5252

5353
}

sample-parent/sample-features/loading-analysis/loading-dependencies-with-dataprepRunAnnotation/src/test/java/org/talend/sdk/component/sample/feature/loadinganalysis/withDataprepRunAnnotation/service/DynamicDependenciesDataprepRunAnnotationServiceTest.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
package org.talend.sdk.component.sample.feature.loadinganalysis.withDataprepRunAnnotation.service;
1717

1818
import java.util.ArrayList;
19-
import java.util.Collections;
2019
import java.util.List;
2120

2221
import org.junit.jupiter.api.Test;
2322
import org.talend.sdk.component.api.service.Service;
2423
import org.talend.sdk.component.junit5.WithComponents;
2524
import org.talend.sdk.component.sample.feature.loadinganalysis.AbstractDynamicDependenciesServiceTest;
26-
import org.talend.sdk.component.sample.feature.loadinganalysis.config.Connector;
27-
import org.talend.sdk.component.sample.feature.loadinganalysis.config.Dependency;
2825
import org.talend.sdk.component.sample.feature.loadinganalysis.withDataprepRunAnnotation.config.Config;
2926
import org.talend.sdk.component.sample.feature.loadinganalysis.withDataprepRunAnnotation.config.Dataset;
3027
import org.talend.sdk.component.sample.feature.loadinganalysis.withDataprepRunAnnotation.config.Datastore;
@@ -44,33 +41,14 @@ protected Config buildConfig() {
4441
Config config = new Config();
4542
Dataset dse = new Dataset();
4643
Datastore dso = new Datastore();
47-
List<Dependency> dynamicDependencies = this.getDynamicDependenciesConfigurationList();
48-
config.getSubConfig().setDependencies(dynamicDependencies);
49-
List<Connector> dynamicDependenciesOfConnectors = Collections.singletonList(
50-
new Connector("org.talend.sdk.component", "sample-connector", getVersion(), "the_family",
51-
"simple-input",
52-
1, true,
53-
"{\" configuration.debug\": \"false\",\" configuration.dataset.resource\": \"Account\",\" configuration.dataset.datastore.url\": \"http://localhost\",\" configuration.dataset.datastore.token\": \"123456\"}"));
54-
config.getSubConfig().setConnectors(dynamicDependenciesOfConnectors);
44+
config.getSubConfig().setDependencies(this.getDynamicDependenciesConfigurationList());
45+
config.getSubConfig().setConnectors(this.getDynamicDependenciesConnectorsConfigurationList());
5546
dse.setDso(dso);
5647
config.setDse(dse);
5748

5849
return config;
5950
}
6051

61-
// use tck connector as dependency
62-
protected List<Dependency> getDynamicDependenciesConfigurationList() {
63-
List<Dependency> depends = new ArrayList<>();
64-
Dependency depend = new Dependency();
65-
depend.setArtifactId("commons-numbers-primes");
66-
depend.setVersion("1.2");
67-
depend.setGroupId("org.apache.commons");
68-
depend.setClazz("org.apache.commons.numbers.primes.SmallPrimes");
69-
depends.add(depend);
70-
71-
return depends;
72-
}
73-
7452
@Override
7553
protected DynamicDependenciesDataprepRunAnnotationService getService() {
7654
return dynamicDependenciesServiceService;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright (C) 2006-2026 Talend Inc. - www.talend.com
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# Here you can change all your configuration display names to use more explicit labels
15+
# You can also translate your configuration by adding one file by local Messages_fr.properties for french for example
16+
17+
loading-dependencies-with-dataprepRunAnnotation = org.talend.sdk.component:sample-connector:${project.version},org.apache.commons:commons-numbers-primes:1.2,org.apache.maven.resolver:maven-resolver-api:2.0.14
18+
classes = org.talend.sdk.component:sample-connector:${project.version},org.apache.commons:commons-numbers-primes:1.2,org.apache.maven.resolver:maven-resolver-api:2.0.14

sample-parent/sample-features/loading-analysis/loading-dependencies-with-dataset/pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@
4040
<scope>test</scope>
4141
</dependency>
4242
<dependency>
43-
<!-- The dependency that simulate a dynamic one. -->
4443
<groupId>org.apache.commons</groupId>
4544
<artifactId>commons-numbers-primes</artifactId>
4645
<version>1.2</version>
4746
<scope>test</scope>
4847
</dependency>
48+
<dependency>
49+
<groupId>org.apache.maven.resolver</groupId>
50+
<artifactId>maven-resolver-api</artifactId>
51+
<version>2.0.14</version>
52+
<scope>test</scope>
53+
</dependency>
4954
</dependencies>
5055

5156
<build>

sample-parent/sample-features/loading-analysis/loading-dependencies-with-dataset/src/main/java/org/talend/sdk/component/sample/feature/loadinganalysis/withdataset/service/DynamicDependenciesWithDatasetService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public List<String> getDynamicDependencies(@Option("theDataset") final Dataset d
4545

4646
@DiscoverSchemaExtended(DEPENDENCY_WITHDATASET_ACTION)
4747
public Schema guessSchema4Input(final @Option("configuration") Config config) {
48-
return super.buildSchema(config);
48+
return super.buildSchema();
4949
}
5050

5151
}

0 commit comments

Comments
 (0)