Skip to content

Commit 5edf5f8

Browse files
feat(QTDI-1291): tests
1 parent 5c3d264 commit 5edf5f8

5 files changed

Lines changed: 158 additions & 79 deletions

File tree

component-api/src/main/java/org/talend/sdk/component/api/exception/DiscoverSchemaException.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
import javax.json.bind.annotation.JsonbCreator;
1919
import javax.json.bind.annotation.JsonbPropertyOrder;
2020

21-
import lombok.Data;
2221
import lombok.EqualsAndHashCode;
22+
import lombok.Getter;
23+
import lombok.NoArgsConstructor;
24+
import lombok.Setter;
25+
import lombok.ToString;
2326

2427
/**
2528
* This class is dedicated to Studio's guess schema feature.
@@ -29,7 +32,10 @@
2932
*
3033
* See me TCOMP-2342 for more details.
3134
*/
32-
@Data
35+
@Setter
36+
@Getter
37+
@ToString
38+
@NoArgsConstructor
3339
@EqualsAndHashCode(callSuper = true)
3440
@JsonbPropertyOrder({ "localizedMessage", "message", "stackTrace", "suppressed", "possibleHandleErrorWith" })
3541
public class DiscoverSchemaException extends RuntimeException {
@@ -58,7 +64,7 @@ public enum HandleErrorWith {
5864
* This won't query for any user input.
5965
* When specifying this option, developer should be sure that no side effect can be generated by connector.
6066
*/
61-
EXECUTE_LIFECYCLE;
67+
EXECUTE_LIFECYCLE
6268
}
6369

6470
private HandleErrorWith possibleHandleErrorWith = HandleErrorWith.EXCEPTION;
@@ -69,20 +75,19 @@ public DiscoverSchemaException(final ComponentException e) {
6975

7076
public DiscoverSchemaException(final ComponentException e, final HandleErrorWith handling) {
7177
super(e.getOriginalMessage(), e.getCause());
72-
setPossibleHandleErrorWith(handling);
78+
this.possibleHandleErrorWith = handling;
7379
}
7480

7581
public DiscoverSchemaException(final String message, final HandleErrorWith handling) {
7682
super(message);
77-
setPossibleHandleErrorWith(handling);
83+
this.possibleHandleErrorWith = handling;
7884
}
7985

8086
@JsonbCreator
8187
public DiscoverSchemaException(final String message, final StackTraceElement[] stackTrace,
8288
final HandleErrorWith handling) {
8389
super(message);
8490
setStackTrace(stackTrace);
85-
setPossibleHandleErrorWith(handling);
91+
this.possibleHandleErrorWith = handling;
8692
}
87-
8893
}

component-server-parent/component-server/src/test/java/org/talend/sdk/component/server/front/ActionResourceImplTest.java

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Iterator;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.stream.Stream;
3334

3435
import javax.inject.Inject;
3536
import javax.json.JsonObject;
@@ -44,7 +45,10 @@
4445
import org.junit.jupiter.api.RepeatedTest;
4546
import org.junit.jupiter.api.Test;
4647
import org.junit.jupiter.params.ParameterizedTest;
48+
import org.junit.jupiter.params.provider.Arguments;
49+
import org.junit.jupiter.params.provider.MethodSource;
4750
import org.junit.jupiter.params.provider.ValueSource;
51+
import org.talend.sdk.component.api.exception.DiscoverSchemaException.HandleErrorWith;
4852
import org.talend.sdk.component.api.record.Schema;
4953
import org.talend.sdk.component.api.service.healthcheck.HealthCheckStatus;
5054
import org.talend.sdk.component.api.service.record.RecordBuilderFactory;
@@ -57,14 +61,17 @@
5761
@MonoMeecrowaveConfig
5862
class ActionResourceImplTest {
5963

64+
private static final int COMMON_ACTION_COUNT = 16;
65+
6066
@Inject
6167
private WebTarget base;
6268

63-
@RepeatedTest(2) // this also checks the cache and queries usage
69+
// this also checks the cache and queries usage
70+
@RepeatedTest(2)
6471
void actionIndex() {
6572
{ // default
6673
final ActionList index = base.path("action/index").request(APPLICATION_JSON_TYPE).get(ActionList.class);
67-
assertEquals(13, index.getItems().size());
74+
assertEquals(COMMON_ACTION_COUNT, index.getItems().size());
6875
assertEquals("jdbc", index.getItems().iterator().next().getComponent());
6976
}
7077
{ // change the family
@@ -81,7 +88,7 @@ void actionIndex() {
8188
@RepeatedTest(2)
8289
void index() {
8390
final ActionList index = base.path("action/index").request(APPLICATION_JSON_TYPE).get(ActionList.class);
84-
assertEquals(13, index.getItems().size());
91+
assertEquals(COMMON_ACTION_COUNT, index.getItems().size());
8592

8693
final List<ActionItem> items = new ArrayList<>(index.getItems());
8794
items.sort(Comparator.comparing(ActionItem::getName));
@@ -165,6 +172,31 @@ void testBackendException() {
165172
assertEquals("Action execution failed with: backend exception", errorPayload.getDescription());
166173
}
167174

175+
@MethodSource("handleErrorWithSource")
176+
@ParameterizedTest
177+
void testDiscoverSchemaException(final HandleErrorWith origin) {
178+
final Response error = base
179+
.path("action/execute")
180+
.queryParam("type", "user")
181+
.queryParam("family", "custom")
182+
.queryParam("action", "discoverSchemaException" + origin)
183+
.request(APPLICATION_JSON_TYPE)
184+
.post(Entity.entity(new HashMap<String, String>(), APPLICATION_JSON_TYPE));
185+
assertEquals(400, error.getStatus());
186+
final ErrorPayload errorPayload = error.readEntity(ErrorPayload.class);
187+
assertEquals(ErrorDictionary.ACTION_ERROR, errorPayload.getCode());
188+
assertEquals(origin.toString(), errorPayload.getSubCode());
189+
assertNotNull(errorPayload.getDescription());
190+
assertTrue(errorPayload.getDescription().startsWith("Action execution failed with:"));
191+
}
192+
193+
static Stream<Arguments> handleErrorWithSource() {
194+
return Stream.of(
195+
Arguments.of(HandleErrorWith.RETRY),
196+
Arguments.of(HandleErrorWith.EXECUTE_MOCK_JOB),
197+
Arguments.of(HandleErrorWith.EXECUTE_LIFECYCLE));
198+
}
199+
168200
@Test
169201
void executeWithEnumParam() {
170202
final Response error = base
@@ -195,14 +227,38 @@ void checkSchemaSerialization() {
195227
.request(APPLICATION_JSON_TYPE)
196228
.post(Entity.entity(emptyMap(), APPLICATION_JSON_TYPE), String.class);
197229
final String expected =
198-
"{\n \"entries\":[\n {\n \"elementSchema\":{\n \"entries\":[\n ],\n" +
199-
" \"metadata\":[\n ],\n \"props\":{\n\n },\n \"type\":\"STRING\"\n"
200-
+
201-
" },\n \"errorCapable\":false," +
202-
"\n \"metadata\":false,\n \"name\":\"array\",\n \"nullable\":false,\n" +
203-
" \"props\":{\n\n },\n \"type\":\"ARRAY\",\n" +
204-
" \"valid\":true\n }\n ],\n \"metadata\":[\n" +
205-
" ],\n \"props\":{\n \"talend.fields.order\":\"array\"\n },\n \"type\":\"RECORD\"\n}";
230+
"""
231+
{
232+
"entries":[
233+
{
234+
"elementSchema":{
235+
"entries":[
236+
],
237+
"metadata":[
238+
],
239+
"props":{
240+
241+
},
242+
"type":"STRING"
243+
},
244+
"errorCapable":false,
245+
"metadata":false,
246+
"name":"array",
247+
"nullable":false,
248+
"props":{
249+
250+
},
251+
"type":"ARRAY",
252+
"valid":true
253+
}
254+
],
255+
"metadata":[
256+
],
257+
"props":{
258+
"talend.fields.order":"array"
259+
},
260+
"type":"RECORD"
261+
}""";
206262
assertEquals(expected, schema);
207263
}
208264

component-server-parent/component-server/src/test/java/org/talend/sdk/component/server/service/PropertiesServiceTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static java.util.stream.Collectors.joining;
2424
import static java.util.stream.Collectors.toList;
2525
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
2627
import static org.junit.jupiter.api.Assertions.assertThrows;
2728
import static org.junit.jupiter.api.Assertions.assertTrue;
2829

@@ -38,6 +39,7 @@
3839
import org.junit.jupiter.api.Test;
3940
import org.talend.sdk.component.api.configuration.Option;
4041
import org.talend.sdk.component.api.configuration.ui.layout.GridLayout;
42+
import org.talend.sdk.component.api.record.Schema;
4143
import org.talend.sdk.component.runtime.manager.ParameterMeta;
4244
import org.talend.sdk.component.runtime.manager.reflect.ParameterModelService;
4345
import org.talend.sdk.component.runtime.manager.reflect.parameterenricher.BaseParameterEnricher;
@@ -52,19 +54,32 @@ class PropertiesServiceTest {
5254
@Inject
5355
private PropertiesService propertiesService;
5456

57+
@SuppressWarnings("java:S1144")
5558
private static void gridLayout(@Option final WithLayout layout) {
5659
// no-op
5760
}
5861

62+
@SuppressWarnings("java:S1144")
5963
private static void boolWrapper(@Option final BoolBool wrapper) {
6064
// no-op
6165
}
6266

67+
@SuppressWarnings("java:S1144")
6368
private static void multipleParams(@Option("aa") final String first, @Option("b") final BoolWrapper config,
6469
@Option("a") final String last) {
6570
// no-op
6671
}
6772

73+
@SuppressWarnings("java:S1144")
74+
private static void schemaParam(@Option final Schema schema) {
75+
// no-op
76+
}
77+
78+
@SuppressWarnings("java:S1144")
79+
private static void nonSchemaParam(@Option final String value) {
80+
// no-op
81+
}
82+
6883
@Test
6984
void gridLayoutTranslation() throws NoSuchMethodException {
7085
final List<ParameterMeta> params = new ParameterModelService(new PropertyEditorRegistry())
@@ -100,6 +115,30 @@ void parameterIndexMeta() throws NoSuchMethodException {
100115
assertEquals("aa/b/a/b.val", props.stream().map(SimplePropertyDefinition::getPath).collect(joining("/")));
101116
}
102117

118+
@Test
119+
void schemaParameterMetadataMarker() throws NoSuchMethodException {
120+
final List<ParameterMeta> schemaParams = new ParameterModelService(new PropertyEditorRegistry())
121+
.buildParameterMetas(getClass().getDeclaredMethod("schemaParam", Schema.class), null,
122+
new BaseParameterEnricher.Context(new LocalConfigurationService(emptyList(), "test")));
123+
final List<SimplePropertyDefinition> schemaProps = propertiesService
124+
.buildProperties(schemaParams, Thread.currentThread().getContextClassLoader(), Locale.ROOT, null)
125+
.toList();
126+
assertTrue(schemaProps.stream()
127+
.filter(p -> !p.getPath().contains("."))
128+
.allMatch(p -> p.getMetadata().containsKey("definition::parameter::schema")),
129+
"Schema-typed root parameter should have definition::parameter::schema marker");
130+
131+
final List<ParameterMeta> strParams = new ParameterModelService(new PropertyEditorRegistry())
132+
.buildParameterMetas(getClass().getDeclaredMethod("nonSchemaParam", String.class), null,
133+
new BaseParameterEnricher.Context(new LocalConfigurationService(emptyList(), "test")));
134+
final List<SimplePropertyDefinition> strProps = propertiesService
135+
.buildProperties(strParams, Thread.currentThread().getContextClassLoader(), Locale.ROOT, null)
136+
.toList();
137+
assertFalse(strProps.stream()
138+
.anyMatch(p -> p.getMetadata().containsKey("definition::parameter::schema")),
139+
"Non-Schema-typed parameter should NOT have definition::parameter::schema marker");
140+
}
141+
103142
@Test
104143
void booleanDefault() throws NoSuchMethodException {
105144
final List<SimplePropertyDefinition> props = propertiesService

component-server-parent/component-server/src/test/java/org/talend/sdk/component/server/test/custom/CustomService.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import java.util.stream.Stream;
2626

2727
import org.talend.sdk.component.api.exception.ComponentException;
28+
import org.talend.sdk.component.api.exception.ComponentException.ErrorOrigin;
29+
import org.talend.sdk.component.api.exception.DiscoverSchemaException;
30+
import org.talend.sdk.component.api.exception.DiscoverSchemaException.HandleErrorWith;
2831
import org.talend.sdk.component.api.service.Action;
2932
import org.talend.sdk.component.api.service.Service;
3033
import org.talend.sdk.component.api.service.completion.SuggestionValues;
@@ -58,16 +61,34 @@ public SuggestionValues get(final LocalConfiguration configuration) throws IOExc
5861

5962
@Action("unknownException")
6063
public Map<String, String> generateUnknownException(final LocalConfiguration configuration) {
61-
throw new ComponentException(ComponentException.ErrorOrigin.UNKNOWN, "unknown exception");
64+
throw new ComponentException(ErrorOrigin.UNKNOWN, "unknown exception");
6265
}
6366

6467
@Action("userException")
6568
public Map<String, String> generateUserException(final LocalConfiguration configuration) {
66-
throw new ComponentException(ComponentException.ErrorOrigin.USER, "user exception");
69+
throw new ComponentException(ErrorOrigin.USER, "user exception");
6770
}
6871

6972
@Action("backendException")
7073
public Map<String, String> generateBackendException(final LocalConfiguration configuration) {
71-
throw new ComponentException(ComponentException.ErrorOrigin.BACKEND, "backend exception");
74+
throw new ComponentException(ErrorOrigin.BACKEND, "backend exception");
75+
}
76+
77+
@Action("discoverSchemaExceptionRETRY")
78+
public Map<String, String> generateDiscoverSchemaExceptionRetry(final LocalConfiguration configuration) {
79+
throw new DiscoverSchemaException(
80+
new ComponentException(ErrorOrigin.USER, "schema not found"), HandleErrorWith.RETRY);
81+
}
82+
83+
@Action("discoverSchemaExceptionEXECUTE_MOCK_JOB")
84+
public Map<String, String> generateDiscoverSchemaExceptionMock(final LocalConfiguration configuration) {
85+
throw new DiscoverSchemaException(
86+
new ComponentException(ErrorOrigin.USER, "schema not found"), HandleErrorWith.EXECUTE_MOCK_JOB);
87+
}
88+
89+
@Action("discoverSchemaExceptionEXECUTE_LIFECYCLE")
90+
public Map<String, String> generateDiscoverSchemaExceptionLifecycle(final LocalConfiguration configuration) {
91+
throw new DiscoverSchemaException(
92+
new ComponentException(ErrorOrigin.USER, "schema not found"), HandleErrorWith.EXECUTE_LIFECYCLE);
7293
}
7394
}

0 commit comments

Comments
 (0)