001/**
002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.talend.sdk.component.tools.validator;
017
018import static java.util.function.Function.identity;
019import static java.util.stream.Collectors.toMap;
020
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Map;
024import java.util.stream.Collectors;
025import java.util.stream.Stream;
026
027import org.apache.xbean.finder.AnnotationFinder;
028import org.talend.sdk.component.api.service.schema.DiscoverSchema;
029import org.talend.sdk.component.api.service.schema.DiscoverSchemaExtended;
030import org.talend.sdk.component.api.service.schema.FixedSchema;
031
032public class FixedSchemaValidator implements Validator {
033
034    @Override
035    public Stream<String> validate(final AnnotationFinder finder, final List<Class<?>> components) {
036        final List<String> errors = new ArrayList<>();
037        final Map<Class<?>, String> classes = finder.findAnnotatedClasses(FixedSchema.class)
038                .stream()
039                .collect(toMap(identity(), d -> d.getAnnotation(FixedSchema.class).value()));
040        // search for empty annotations
041        errors.addAll(classes.entrySet()
042                .stream()
043                .filter(e -> e.getValue().isEmpty())
044                .map(e -> String.format("Empty @FixedSchema annotation's value in class %s.",
045                        e.getKey().getSimpleName()))
046                .collect(Collectors.toList()));
047        // search for missing methods
048        final List<String> methods = Stream
049                .concat(finder.findAnnotatedMethods(DiscoverSchema.class)
050                        .stream()
051                        .map(m -> m.getDeclaredAnnotation(DiscoverSchema.class).value()),
052                        finder.findAnnotatedMethods(DiscoverSchemaExtended.class)
053                                .stream()
054                                .map(m -> m.getDeclaredAnnotation(DiscoverSchemaExtended.class).value()))
055                .collect(Collectors.toList());
056        errors.addAll(classes.entrySet()
057                .stream()
058                .filter(e -> !e.getValue().isEmpty())
059                .filter(e -> !methods.contains(e.getValue()))
060                .map(e -> String.format("@FixedSchema '%s' in class %s is not declared anywhere as DiscoverSchema*.",
061                        e.getValue(), e.getKey().getSimpleName()))
062                .collect(Collectors.toList()));
063
064        return errors.stream();
065    }
066}