Skip to content

Commit

Permalink
item(TCOMP-2743): fix the error
Browse files Browse the repository at this point in the history
  • Loading branch information
yyin-talend committed Jun 28, 2024
1 parent d7233ba commit e3b5845
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,41 @@
*/
package org.talend.sdk.component.tools.validator;

import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.xbean.finder.AnnotationFinder;
import org.talend.sdk.component.api.service.http.HttpClient;
import org.talend.sdk.component.api.service.http.Request;
import org.talend.sdk.component.runtime.manager.service.http.HttpClientFactoryImpl;

public class HttpValidator implements Validator {

@Override
public Stream<String> validate(final AnnotationFinder finder, final List<Class<?>> components) {
return components
.stream()
.flatMap(c -> HttpClientFactoryImpl.createErrors(c).stream()) //
.sorted();
// If the class extends HttpClient, it should use @Request.
List<String> classErrors = components.stream()
.filter(c -> {
if (HttpClient.class.isAssignableFrom(c) && finder.findAnnotatedMethods(Request.class).isEmpty()) {

Check warning on line 35 in component-tools/src/main/java/org/talend/sdk/component/tools/validator/HttpValidator.java

View check run for this annotation

sonar-eks / Component Runtime Sonarqube Results

component-tools/src/main/java/org/talend/sdk/component/tools/validator/HttpValidator.java#L35

Replace this if-then-else statement by a single return statement.
return true;
}
return false;
})
.map(c -> c.getCanonicalName() + " should extends HttpClient")
.collect(Collectors.toList());

List<String> methodError = finder
.findAnnotatedMethods(Request.class) //
.stream() //
.map(Method::getDeclaringClass) //
.distinct() //
.flatMap(c -> HttpClientFactoryImpl.createErrors(c).stream())
.sorted()
.collect(Collectors.toList());

return Stream.concat(classErrors.stream(), methodError.stream());

}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/**
* Copyright (C) 2006-2024 Talend Inc. - www.talend.com
*
* 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.talend.sdk.component.tools.validator;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -14,6 +29,7 @@
import org.talend.sdk.component.api.service.http.Request;

class HttpClientValidatorTest {

@Test
void validateClassExtendWrongCLass() {
final HttpValidator validator = new HttpValidator();
Expand Down Expand Up @@ -56,7 +72,7 @@ void validateWrongClientNoMethodRequest() {
AnnotationFinder finder = new AnnotationFinder(new ClassesArchive(WrongClientNoMethodRequest.class));
final Stream<String> errors =
validator.validate(finder, Arrays.asList(WrongClientNoMethodRequest.class));
assertEquals(2, errors.count());
assertEquals(1, errors.count());
}

@Test
Expand All @@ -74,13 +90,12 @@ interface ClientCorrect extends HttpClient {
String main1(String ok);
}

interface ClientKoWrongExtends extends List{
interface ClientKoWrongExtends extends List {

@Request
List<Object> main(String payload);
}


interface ClientKoNotExtendsAnything {

@Request
Expand Down

0 comments on commit e3b5845

Please sign in to comment.