001// Generated by delombok at Sun Jul 30 17:21:56 UTC 2023
002package de.cuioss.test.jsf.config.decorator;
003
004import java.io.Serializable;
005import java.util.Arrays;
006import java.util.List;
007import java.util.Locale;
008import javax.faces.component.UIViewRoot;
009import javax.faces.context.ExternalContext;
010import javax.faces.context.FacesContext;
011import javax.servlet.http.Cookie;
012import javax.servlet.http.HttpServletRequest;
013import org.apache.myfaces.test.mock.MockExternalContext22;
014import org.apache.myfaces.test.mock.MockFacesContext22;
015import org.apache.myfaces.test.mock.MockHttpServletRequest;
016import de.cuioss.test.jsf.mocks.CuiMockHttpServletRequest;
017
018/**
019 * Helper class for configuring the request
020 *
021 * @author Oliver Wolff
022 */
023public class RequestConfigDecorator {
024    private final MockFacesContext22 facesContext;
025    private final MockExternalContext22 externalContext;
026
027    /**
028     * Sets the postback attribute to {@link FacesContext#isPostback()}
029     *
030     * @param postback to be set
031     * @return the {@link RequestConfigDecorator} itself in order to enable a
032     *         fluent-api style usage
033     */
034    public RequestConfigDecorator setPostback(final boolean postback) {
035        facesContext.setPostback(postback);
036        return this;
037    }
038
039    /**
040     * Sets the viewId in {@link UIViewRoot}
041     *
042     * @param viewId to be set
043     * @return the {@link RequestConfigDecorator} itself in order to enable a
044     *         fluent-api style usage
045     */
046    public RequestConfigDecorator setViewId(final String viewId) {
047        facesContext.getViewRoot().setViewId(viewId);
048        return this;
049    }
050
051    /**
052     * Registers a concrete RequestHeader to
053     * {@link ExternalContext#getRequestHeaderMap()}
054     *
055     * @param key   used as the key for the
056     *              {@link ExternalContext#getRequestHeaderMap()}
057     * @param value used as the value for the
058     *              {@link ExternalContext#getRequestHeaderMap()}
059     * @return the {@link RequestConfigDecorator} itself in order to enable a
060     *         fluent-api style usage
061     */
062    public RequestConfigDecorator setRequestHeader(final String key, final String value) {
063        externalContext.addRequestHeader(key, value);
064        return this;
065    }
066
067    /**
068     * Registers a concrete Request-parameter to
069     * {@link ExternalContext#getRequestParameterMap()}
070     *
071     * @param key   used as the key for the
072     *              {@link ExternalContext#getRequestHeaderMap()}
073     * @param value used as the value for the
074     *              {@link ExternalContext#getRequestHeaderMap()}
075     * @return the {@link RequestConfigDecorator} itself in order to enable a
076     *         fluent-api style usage
077     */
078    public RequestConfigDecorator setRequestParameter(final String key, final String value) {
079        externalContext.addRequestParameterMap(key, value);
080        return this;
081    }
082
083    /**
084     * Registers a concrete Request-attribute to
085     * {@link HttpServletRequest#setAttribute(String, Object)}
086     *
087     * @param key   used as the key for the
088     *              {@link HttpServletRequest#setAttribute(String, Object)}
089     * @param value used as the value for the
090     *              {@link HttpServletRequest#setAttribute(String, Object)}
091     * @return the {@link RequestConfigDecorator} itself in order to enable a
092     *         fluent-api style usage
093     */
094    public RequestConfigDecorator setRequestAttribute(final String key, final Serializable value) {
095        ((HttpServletRequest) externalContext.getRequest()).setAttribute(key, value);
096        return this;
097    }
098
099    /**
100     * Registers one or more cookies the the contained request
101     *
102     * @param cookie to be added
103     * @return the {@link RequestConfigDecorator} itself in order to enable a
104     *         fluent-api style usage
105     */
106    public RequestConfigDecorator addRequestCookie(final Cookie... cookie) {
107        var request = (MockHttpServletRequest) externalContext.getRequest();
108        for (Cookie aCookie : cookie) {
109            request.addCookie(aCookie);
110        }
111        return this;
112    }
113
114    /**
115     * <p>
116     * Registers one or more requestLocale to
117     * {@link ExternalContext#getRequestLocales()}. It can be used for resetting the
118     * locales as well.
119     * </p>
120     * <em>Caution: </em> It expects the {@link HttpServletRequest} being an
121     * instance of {@link CuiMockHttpServletRequest}
122     *
123     * @param requestLocale one or more requestLocales to be set
124     * @return the {@link RequestConfigDecorator} itself in order to enable a
125     *         fluent-api style usage
126     */
127    public RequestConfigDecorator setRequestLocale(final Locale... requestLocale) {
128        var request = (CuiMockHttpServletRequest) externalContext.getRequest();
129        List<Locale> localeList = Arrays.asList(requestLocale);
130        Locale locale = null;
131        if (!localeList.isEmpty()) {
132            locale = localeList.iterator().next();
133        }
134        request.setLocale(locale);
135        request.setRequestLocales(localeList);
136        return this;
137    }
138
139    /**
140     * Explicitly sets a given queryString as path-element of the request
141     *
142     * @param parameterString
143     */
144    public void setQueryString(String parameterString) {
145        var request = (CuiMockHttpServletRequest) externalContext.getRequest();
146        request.setPathElements(request.getContextPath(), request.getServletPath(), request.getPathInfo(), parameterString);
147    }
148
149    @java.lang.SuppressWarnings("all")
150    @lombok.Generated
151    public RequestConfigDecorator(final MockFacesContext22 facesContext, final MockExternalContext22 externalContext) {
152        this.facesContext = facesContext;
153        this.externalContext = externalContext;
154    }
155}