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