mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 10:58:51 +00:00
@@ -1,52 +1,59 @@
|
||||
package com.iluwatar.sessionserver;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import com.iluwatar.sessionserver.LoginHandler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* LoginHandlerTest.
|
||||
*/
|
||||
public class LoginHandlerTest {
|
||||
|
||||
private LoginHandler loginHandler;
|
||||
//private Headers headers;
|
||||
private Map<String, Integer> sessions;
|
||||
private Map<String, Instant> sessionCreationTimes;
|
||||
private LoginHandler loginHandler;
|
||||
//private Headers headers;
|
||||
private Map<String, Integer> sessions;
|
||||
private Map<String, Instant> sessionCreationTimes;
|
||||
|
||||
@Mock
|
||||
private HttpExchange exchange;
|
||||
@Mock
|
||||
private HttpExchange exchange;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
sessions = new HashMap<>();
|
||||
sessionCreationTimes = new HashMap<>();
|
||||
loginHandler = new LoginHandler(sessions, sessionCreationTimes);
|
||||
}
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
sessions = new HashMap<>();
|
||||
sessionCreationTimes = new HashMap<>();
|
||||
loginHandler = new LoginHandler(sessions, sessionCreationTimes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandle() throws IOException {
|
||||
@Test
|
||||
public void testHandle() throws IOException {
|
||||
|
||||
//assemble
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //Exchange object is mocked so OutputStream must be manually created
|
||||
when(exchange.getResponseHeaders()).thenReturn(new Headers()); //Exchange object is mocked so Header object must be manually created
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
//assemble
|
||||
ByteArrayOutputStream outputStream =
|
||||
new ByteArrayOutputStream(); //Exchange object is mocked so OutputStream must be manually created
|
||||
when(exchange.getResponseHeaders()).thenReturn(
|
||||
new Headers()); //Exchange object is mocked so Header object must be manually created
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
|
||||
//act
|
||||
loginHandler.handle(exchange);
|
||||
//act
|
||||
loginHandler.handle(exchange);
|
||||
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
assertEquals(sessions.entrySet().toArray()[0].toString().split("=1")[0], response[1]);
|
||||
}
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
assertEquals(sessions.entrySet().toArray()[0].toString().split("=1")[0], response[1]);
|
||||
}
|
||||
}
|
||||
@@ -1,75 +1,83 @@
|
||||
package com.iluwatar.sessionserver;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import com.iluwatar.sessionserver.LogoutHandler;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* LogoutHandlerTest.
|
||||
*/
|
||||
public class LogoutHandlerTest {
|
||||
|
||||
private LogoutHandler logoutHandler;
|
||||
private Headers headers;
|
||||
private Map<String, Integer> sessions;
|
||||
private Map<String, Instant> sessionCreationTimes;
|
||||
private LogoutHandler logoutHandler;
|
||||
private Headers headers;
|
||||
private Map<String, Integer> sessions;
|
||||
private Map<String, Instant> sessionCreationTimes;
|
||||
|
||||
@Mock
|
||||
private HttpExchange exchange;
|
||||
@Mock
|
||||
private HttpExchange exchange;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
sessions = new HashMap<>();
|
||||
sessionCreationTimes = new HashMap<>();
|
||||
logoutHandler = new LogoutHandler(sessions, sessionCreationTimes);
|
||||
headers = new Headers();
|
||||
headers.add("Cookie", "sessionID=1234"); //Exchange object methods return Header Object but Exchange is mocked so Headers must be manually created
|
||||
}
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
sessions = new HashMap<>();
|
||||
sessionCreationTimes = new HashMap<>();
|
||||
logoutHandler = new LogoutHandler(sessions, sessionCreationTimes);
|
||||
headers = new Headers();
|
||||
headers.add("Cookie",
|
||||
"sessionID=1234"); //Exchange object methods return Header Object but Exchange is mocked so Headers must be manually created
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandler_SessionNotExpired() throws IOException {
|
||||
@Test
|
||||
public void testHandler_SessionNotExpired() throws IOException {
|
||||
|
||||
//assemble
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
sessions.put("1234", 1); //Fake login details since LoginHandler isn't called
|
||||
sessionCreationTimes.put("1234", Instant.now()); //Fake login details since LoginHandler isn't called
|
||||
when(exchange.getRequestHeaders()).thenReturn(headers);
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
//assemble
|
||||
sessions.put("1234", 1); //Fake login details since LoginHandler isn't called
|
||||
sessionCreationTimes.put("1234",
|
||||
Instant.now()); //Fake login details since LoginHandler isn't called
|
||||
when(exchange.getRequestHeaders()).thenReturn(headers);
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
|
||||
//act
|
||||
logoutHandler.handle(exchange);
|
||||
//act
|
||||
logoutHandler.handle(exchange);
|
||||
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
Assertions.assertEquals("1234", response[1]);
|
||||
Assertions.assertFalse(sessions.containsKey(response));
|
||||
Assertions.assertFalse(sessionCreationTimes.containsKey(response));
|
||||
}
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
Assertions.assertEquals("1234", response[1]);
|
||||
Assertions.assertFalse(sessions.containsKey(response));
|
||||
Assertions.assertFalse(sessionCreationTimes.containsKey(response));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHandler_SessionExpired() throws IOException {
|
||||
@Test
|
||||
public void testHandler_SessionExpired() throws IOException {
|
||||
|
||||
//assemble
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
when(exchange.getRequestHeaders()).thenReturn(headers);
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
//assemble
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
when(exchange.getRequestHeaders()).thenReturn(headers);
|
||||
when(exchange.getResponseBody()).thenReturn(outputStream);
|
||||
|
||||
//act
|
||||
logoutHandler.handle(exchange);
|
||||
//act
|
||||
logoutHandler.handle(exchange);
|
||||
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
Assertions.assertEquals("Session has already expired!", response[0]);
|
||||
Assertions.assertFalse(sessions.containsKey(response));
|
||||
Assertions.assertFalse(sessionCreationTimes.containsKey(response));
|
||||
}
|
||||
//assert
|
||||
String[] response = outputStream.toString().split("Session ID: ");
|
||||
Assertions.assertEquals("Session has already expired!", response[0]);
|
||||
Assertions.assertFalse(sessions.containsKey(response));
|
||||
Assertions.assertFalse(sessionCreationTimes.containsKey(response));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user