001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.mock;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.util.*;
022
023import jakarta.servlet.*;
024import jakarta.servlet.http.*;
025
026/**
027 * An implementation of {@link HttpSession} for mocking purposes.
028 *
029 * <p>
030 * Session-based tests can use this API to create customized instances of {@link HttpSession} objects
031 * that can be passed to the {@link MockRestRequest#httpSession(HttpSession)} method.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestMockBasics">juneau-rest-mock Basics</a>
035 * </ul>
036 */
037public class MockHttpSession implements HttpSession {
038
039   /**
040    * Creates a new HTTP session.
041    *
042    * @return A new HTTP session.
043    */
044   public static MockHttpSession create() {
045      return new MockHttpSession();
046   }
047
048   private Map<String,Object> attributes = map();
049   private long creationTime, lastAccessedTime;
050   private int maxInactiveInterval;
051   private String id;
052   private ServletContext servletContext;
053
054   private boolean isNew;
055
056   /**
057    * Sets the creation time on this session.
058    *
059    * <p>
060    * Affects the results of calling {@link HttpSession#getCreationTime()}.
061    *
062    * @param value The new value for this setting.
063    * @return This object.
064    */
065   public MockHttpSession creationTime(long value) {
066      creationTime = value;
067      return this;
068   }
069
070   @Override /* Overridden from HttpSession */
071   public Object getAttribute(String name) {
072      return attributes.get(name);
073   }
074
075   @Override /* Overridden from HttpSession */
076   public Enumeration<String> getAttributeNames() { return Collections.enumeration(attributes.keySet()); }
077
078   @Override /* Overridden from HttpSession */
079   public long getCreationTime() { return creationTime; }
080
081   @Override /* Overridden from HttpSession */
082   public String getId() { return id; }
083
084   @Override /* Overridden from HttpSession */
085   public long getLastAccessedTime() { return lastAccessedTime; }
086
087   @Override /* Overridden from HttpSession */
088   public int getMaxInactiveInterval() { return maxInactiveInterval; }
089
090   @Override /* Overridden from HttpSession */
091   public ServletContext getServletContext() { return servletContext; }
092
093   /**
094    * Sets the id on this session.
095    *
096    * <p>
097    * Affects the results of calling {@link HttpSession#getId()}.
098    *
099    * @param value The new value for this setting.
100    * @return This object.
101    */
102   public MockHttpSession id(String value) {
103      id = value;
104      return this;
105   }
106
107   @Override /* Overridden from HttpSession */
108   public void invalidate() {}
109
110   @Override /* Overridden from HttpSession */
111   public boolean isNew() { return isNew; }
112
113   /**
114    * Sets the is-new value on this session.
115    *
116    * <p>
117    * Affects the results of calling {@link HttpSession#isNew()}.
118    *
119    * @param value The new value for this setting.
120    * @return This object.
121    */
122   public MockHttpSession isNew(boolean value) {
123      isNew = value;
124      return this;
125   }
126
127   /**
128    * Sets the last-accessed time on this session.
129    *
130    * <p>
131    * Affects the results of calling {@link HttpSession#getLastAccessedTime()}.
132    *
133    * @param value The new value for this setting.
134    * @return This object.
135    */
136   public MockHttpSession lastAccessedTime(long value) {
137      lastAccessedTime = value;
138      return this;
139   }
140
141   /**
142    * Sets the max-inactive interval time on this session.
143    *
144    * <p>
145    * Affects the results of calling {@link HttpSession#getMaxInactiveInterval()}.
146    *
147    * @param value The new value for this setting.
148    * @return This object.
149    */
150   public MockHttpSession maxInactiveInterval(int value) {
151      maxInactiveInterval = value;
152      return this;
153   }
154
155   @Override /* Overridden from HttpSession */
156   public void removeAttribute(String name) {
157      attributes.remove(name);
158   }
159
160   /**
161    * Sets the servlet context on this session.
162    *
163    * <p>
164    * Affects the results of calling {@link HttpSession#getServletContext()}.
165    *
166    * @param value The new value for this setting.
167    * @return This object.
168    */
169   public MockHttpSession servletContext(ServletContext value) {
170      servletContext = value;
171      return this;
172   }
173
174   @Override /* Overridden from HttpSession */
175   public void setAttribute(String name, Object value) {
176      attributes.put(name, value);
177   }
178
179   @Override /* Overridden from HttpSession */
180   public void setMaxInactiveInterval(int value) { this.maxInactiveInterval = value; }
181}