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.internal.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   private Map<String,Object> attributes = map();
040
041   private long creationTime, lastAccessedTime;
042   private int maxInactiveInterval;
043   private String id;
044   private ServletContext servletContext;
045   private boolean isNew;
046
047   /**
048    * Creates a new HTTP session.
049    *
050    * @return A new HTTP session.
051    */
052   public static MockHttpSession create() {
053      return new MockHttpSession();
054   }
055
056   //------------------------------------------------------------------------------------------------------------------
057   // Setter methods
058   //------------------------------------------------------------------------------------------------------------------
059
060   /**
061    * Sets the creation time on this session.
062    *
063    * <p>
064    * Affects the results of calling {@link HttpSession#getCreationTime()}.
065    *
066    * @param value The new value for this setting.
067    * @return This object.
068    */
069   public MockHttpSession creationTime(long value) {
070      this.creationTime = value;
071      return this;
072   }
073
074   /**
075    * Sets the last-accessed time on this session.
076    *
077    * <p>
078    * Affects the results of calling {@link HttpSession#getLastAccessedTime()}.
079    *
080    * @param value The new value for this setting.
081    * @return This object.
082    */
083   public MockHttpSession lastAccessedTime(long value) {
084      this.lastAccessedTime = value;
085      return this;
086   }
087
088   /**
089    * Sets the max-inactive interval time on this session.
090    *
091    * <p>
092    * Affects the results of calling {@link HttpSession#getMaxInactiveInterval()}.
093    *
094    * @param value The new value for this setting.
095    * @return This object.
096    */
097   public MockHttpSession maxInactiveInterval(int value) {
098      this.maxInactiveInterval = value;
099      return this;
100   }
101
102   /**
103    * Sets the id on this session.
104    *
105    * <p>
106    * Affects the results of calling {@link HttpSession#getId()}.
107    *
108    * @param value The new value for this setting.
109    * @return This object.
110    */
111   public MockHttpSession id(String value) {
112      this.id = value;
113      return this;
114   }
115
116   /**
117    * Sets the servlet context on this session.
118    *
119    * <p>
120    * Affects the results of calling {@link HttpSession#getServletContext()}.
121    *
122    * @param value The new value for this setting.
123    * @return This object.
124    */
125   public MockHttpSession servletContext(ServletContext value) {
126      this.servletContext = value;
127      return this;
128   }
129
130   /**
131    * Sets the is-new value on this session.
132    *
133    * <p>
134    * Affects the results of calling {@link HttpSession#isNew()}.
135    *
136    * @param value The new value for this setting.
137    * @return This object.
138    */
139   public MockHttpSession isNew(boolean value) {
140      this.isNew = value;
141      return this;
142   }
143
144   //------------------------------------------------------------------------------------------------------------------
145   // HttpSession methods
146   //------------------------------------------------------------------------------------------------------------------
147
148   @Override /* HttpSession */
149   public long getCreationTime() {
150      return creationTime;
151   }
152
153   @Override /* HttpSession */
154   public String getId() {
155      return id;
156   }
157
158   @Override /* HttpSession */
159   public long getLastAccessedTime() {
160      return lastAccessedTime;
161   }
162
163   @Override /* HttpSession */
164   public ServletContext getServletContext() {
165      return servletContext;
166   }
167
168   @Override /* HttpSession */
169   public void setMaxInactiveInterval(int value) {
170      this.maxInactiveInterval = value;
171   }
172
173   @Override /* HttpSession */
174   public int getMaxInactiveInterval() {
175      return maxInactiveInterval;
176   }
177
178   @Override /* HttpSession */
179   public Object getAttribute(String name) {
180      return attributes.get(name);
181   }
182
183   @Override /* HttpSession */
184   public Enumeration<String> getAttributeNames() {
185      return Collections.enumeration(attributes.keySet());
186   }
187
188   @Override /* HttpSession */
189   public void setAttribute(String name, Object value) {
190      attributes.put(name, value);
191   }
192   @Override /* HttpSession */
193   public void removeAttribute(String name) {
194      attributes.remove(name);
195   }
196
197   @Override /* HttpSession */
198   public void invalidate() {
199   }
200
201   @Override /* HttpSession */
202   public boolean isNew() {
203      return isNew;
204   }
205}