001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.mocks;
003
004import static java.util.Objects.requireNonNull;
005import java.util.ArrayList;
006import java.util.List;
007import javax.faces.component.ContextCallback;
008import javax.faces.component.UIComponent;
009import javax.faces.component.search.ComponentNotFoundException;
010import javax.faces.component.search.SearchExpressionContext;
011import javax.faces.component.search.SearchExpressionHandler;
012import javax.faces.component.search.SearchExpressionHint;
013import javax.faces.context.FacesContext;
014import de.cuioss.tools.string.MoreStrings;
015
016/**
017 * Mock variant of {@link SearchExpressionHandler} to be used for unit-tests.
018 * The concrete instance can be derived via {@link #retrieve(FacesContext)}
019 *
020 * @author Oliver Wolff
021 */
022public class CuiMockSearchExpressionHandler extends SearchExpressionHandler {
023    private static final String UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION = "Unable to find component with expression = ";
024    private static final String NOT_IMPLEMENTED = "Not implemented";
025    private UIComponent resolvedComponent;
026    private List<UIComponent> resolvedComponents = new ArrayList<>();
027    private String resolvedClientId;
028    private List<String> resolvedClientIds = new ArrayList<>();
029
030    @Override
031    public String resolveClientId(SearchExpressionContext searchExpressionContext, String expression) {
032        if (MoreStrings.isEmpty(expression)) {
033            throw new ComponentNotFoundException(UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION + expression);
034        }
035        return resolvedClientId;
036    }
037
038    @Override
039    public List<String> resolveClientIds(SearchExpressionContext searchExpressionContext, String expressions) {
040        if (MoreStrings.isEmpty(expressions)) {
041            throw new ComponentNotFoundException(UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION + expressions);
042        }
043        return resolvedClientIds;
044    }
045
046    @Override
047    public void resolveComponent(SearchExpressionContext searchExpressionContext, String expression, ContextCallback callback) {
048        if (MoreStrings.isEmpty(expression)) {
049            throw new ComponentNotFoundException(UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION + expression);
050        }
051        if (null != resolvedComponent) {
052            callback.invokeContextCallback(searchExpressionContext.getFacesContext(), resolvedComponent);
053        } else if (shouldIgnoreNoResult(searchExpressionContext)) {
054            throw new ComponentNotFoundException(UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION + expression);
055        }
056    }
057
058    @Override
059    public void resolveComponents(SearchExpressionContext searchExpressionContext, String expressions, ContextCallback callback) {
060        requireNonNull(resolvedComponents);
061        if (MoreStrings.isEmpty(expressions)) {
062            throw new ComponentNotFoundException(UNABLE_TO_FIND_COMPONENT_WITH_EXPRESSION + expressions);
063        }
064        if (resolvedComponents.isEmpty() && shouldIgnoreNoResult(searchExpressionContext)) {
065            throw new ComponentNotFoundException("Unable to find components with expression = " + expressions);
066        }
067        resolvedComponents.forEach(component -> callback.invokeContextCallback(searchExpressionContext.getFacesContext(), component));
068    }
069
070    private boolean shouldIgnoreNoResult(SearchExpressionContext searchExpressionContext) {
071        var expressionHints = searchExpressionContext.getExpressionHints();
072        return null == expressionHints || !expressionHints.contains(SearchExpressionHint.IGNORE_NO_RESULT);
073    }
074
075    @Override
076    public void invokeOnComponent(SearchExpressionContext searchExpressionContext, UIComponent previous, String expression, ContextCallback topCallback) {
077        throw new UnsupportedOperationException(NOT_IMPLEMENTED);
078    }
079
080    @Override
081    public String[] splitExpressions(FacesContext context, String expressions) {
082        throw new UnsupportedOperationException(NOT_IMPLEMENTED);
083    }
084
085    @Override
086    public boolean isPassthroughExpression(SearchExpressionContext searchExpressionContext, String expression) {
087        throw new UnsupportedOperationException(NOT_IMPLEMENTED);
088    }
089
090    @Override
091    public boolean isValidExpression(SearchExpressionContext searchExpressionContext, String expression) {
092        throw new UnsupportedOperationException(NOT_IMPLEMENTED);
093    }
094
095    /**
096     * Shorthand for accessing an instance of {@link CuiMockSearchExpressionHandler}
097     *
098     * @param context to be used
099     * @return the previously configured {@link CuiMockSearchExpressionHandler}
100     */
101    public static final CuiMockSearchExpressionHandler retrieve(FacesContext context) {
102        return (CuiMockSearchExpressionHandler) context.getApplication().getSearchExpressionHandler();
103    }
104
105    @java.lang.SuppressWarnings("all")
106    @lombok.Generated
107    public UIComponent getResolvedComponent() {
108        return this.resolvedComponent;
109    }
110
111    @java.lang.SuppressWarnings("all")
112    @lombok.Generated
113    public void setResolvedComponent(final UIComponent resolvedComponent) {
114        this.resolvedComponent = resolvedComponent;
115    }
116
117    @java.lang.SuppressWarnings("all")
118    @lombok.Generated
119    public List<UIComponent> getResolvedComponents() {
120        return this.resolvedComponents;
121    }
122
123    @java.lang.SuppressWarnings("all")
124    @lombok.Generated
125    public void setResolvedComponents(final List<UIComponent> resolvedComponents) {
126        this.resolvedComponents = resolvedComponents;
127    }
128
129    @java.lang.SuppressWarnings("all")
130    @lombok.Generated
131    public String getResolvedClientId() {
132        return this.resolvedClientId;
133    }
134
135    @java.lang.SuppressWarnings("all")
136    @lombok.Generated
137    public void setResolvedClientId(final String resolvedClientId) {
138        this.resolvedClientId = resolvedClientId;
139    }
140
141    @java.lang.SuppressWarnings("all")
142    @lombok.Generated
143    public List<String> getResolvedClientIds() {
144        return this.resolvedClientIds;
145    }
146
147    @java.lang.SuppressWarnings("all")
148    @lombok.Generated
149    public void setResolvedClientIds(final List<String> resolvedClientIds) {
150        this.resolvedClientIds = resolvedClientIds;
151    }
152}