From f59cd0596559045f711bcebbe70714a8c8fe382a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 4 Oct 2016 22:40:01 +0200 Subject: [PATCH] Improving coverage --- .../http/authentication/AuthenticationInfo.java | 10 ++-- .../com/prueba/core/http/handles/ApiHandler.java | 2 +- .../com/prueba/core/http/handles/PagesHandler.java | 2 +- .../com/prueba/core/http/sessions/Sessions.java | 4 +- .../com/prueba/services/impl/LoginServiceImpl.java | 4 +- .../context/security/handle/SessionHandleTest.java | 16 ------ .../authentication/AuthenticationInfoTest.java | 17 ++++++ .../prueba/core/http/sessions/SessionInfoTest.java | 4 +- .../prueba/core/http/sessions/SessionsTest.java | 62 ++++++++++++++++++++++ .../prueba/model/domain/AccountResourceTest.java | 30 +++++++++++ .../domain/ApplicationResourceResourceTest.java | 18 +++++++ 11 files changed, 139 insertions(+), 30 deletions(-) delete mode 100644 src/test/java/com/prueba/core/context/security/handle/SessionHandleTest.java create mode 100644 src/test/java/com/prueba/core/http/authentication/AuthenticationInfoTest.java create mode 100644 src/test/java/com/prueba/core/http/sessions/SessionsTest.java create mode 100644 src/test/java/com/prueba/model/domain/AccountResourceTest.java create mode 100644 src/test/java/com/prueba/model/domain/ApplicationResourceResourceTest.java diff --git a/src/main/java/com/prueba/core/http/authentication/AuthenticationInfo.java b/src/main/java/com/prueba/core/http/authentication/AuthenticationInfo.java index 3c3d277..46c5041 100644 --- a/src/main/java/com/prueba/core/http/authentication/AuthenticationInfo.java +++ b/src/main/java/com/prueba/core/http/authentication/AuthenticationInfo.java @@ -1,13 +1,13 @@ package com.prueba.core.http.authentication; public class AuthenticationInfo { - private final String userName; + private final String username; - public AuthenticationInfo(String userName) { - this.userName = userName; + public AuthenticationInfo(String username) { + this.username = username; } - public String getUserName() { - return userName; + public String getUsername() { + return username; } } diff --git a/src/main/java/com/prueba/core/http/handles/ApiHandler.java b/src/main/java/com/prueba/core/http/handles/ApiHandler.java index 779bce7..7836a4d 100644 --- a/src/main/java/com/prueba/core/http/handles/ApiHandler.java +++ b/src/main/java/com/prueba/core/http/handles/ApiHandler.java @@ -38,7 +38,7 @@ public class ApiHandler implements HttpHandler { AuthenticationInfo authenticationInfo = BasicAuthenticationContext.getAuthentication(); if(authorizationService.isAuthorized(httpExchange.getRequestMethod(), - httpExchange.getRequestURI().toString(), authenticationInfo.getUserName())) { + httpExchange.getRequestURI().toString(), authenticationInfo.getUsername())) { apiController.handle(httpExchange); diff --git a/src/main/java/com/prueba/core/http/handles/PagesHandler.java b/src/main/java/com/prueba/core/http/handles/PagesHandler.java index 0a526d5..2f1e12e 100644 --- a/src/main/java/com/prueba/core/http/handles/PagesHandler.java +++ b/src/main/java/com/prueba/core/http/handles/PagesHandler.java @@ -46,7 +46,7 @@ public class PagesHandler implements HttpHandler { protected void handleThrowable(HttpExchange httpExchange) throws IOException { this.sessionHandler.handle(httpExchange); - if (Sessions.getInstance().isValidSession(httpExchange)) { + if (Sessions.getInstance().isValidSession()) { final SessionInfo sessionInfo = SessionContext.getSession(); if(authorizationService.isAuthorized(httpExchange.getRequestMethod(), diff --git a/src/main/java/com/prueba/core/http/sessions/Sessions.java b/src/main/java/com/prueba/core/http/sessions/Sessions.java index 1ac93e4..ab1c515 100644 --- a/src/main/java/com/prueba/core/http/sessions/Sessions.java +++ b/src/main/java/com/prueba/core/http/sessions/Sessions.java @@ -11,8 +11,6 @@ import java.util.concurrent.ConcurrentHashMap; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.sun.net.httpserver.HttpExchange; - public class Sessions { private static final Logger LOGGER = LoggerFactory.getLogger(Sessions.class); @@ -48,7 +46,7 @@ public class Sessions { sessions.remove(uuid); } - public boolean isValidSession(HttpExchange httpExchange) { + public boolean isValidSession() { final SessionInfo sessionInfo = SessionContext.getSession(); boolean isValid = false; diff --git a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java index 495e8de..8df3e78 100644 --- a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java @@ -46,7 +46,7 @@ public class LoginServiceImpl { final LoginFormImpl loginForm = new LoginFormImpl(); String html = ""; - if (Sessions.getInstance().isValidSession(httpExchange)) { + if (Sessions.getInstance().isValidSession()) { html = loginForm.doNoRequiredLogin(); Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); } else { @@ -64,7 +64,7 @@ public class LoginServiceImpl { public void processLoginPost(HttpExchange httpExchange) throws IOException { final SessionInfo sessionInfo = SessionContext.getSession(); - if (!Sessions.getInstance().isValidSession(httpExchange)) { + if (!Sessions.getInstance().isValidSession()) { String body = this.getBody(httpExchange); String [] formData = body.split("&"); if (formData.length == 2) { diff --git a/src/test/java/com/prueba/core/context/security/handle/SessionHandleTest.java b/src/test/java/com/prueba/core/context/security/handle/SessionHandleTest.java deleted file mode 100644 index ebc4cb3..0000000 --- a/src/test/java/com/prueba/core/context/security/handle/SessionHandleTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.prueba.core.context.security.handle; - -import static org.junit.Assert.*; - -import org.junit.Ignore; -import org.junit.Test; - -@Ignore -public class SessionHandleTest { - - @Test - public void test() { - fail("Not yet implemented"); - } - -} diff --git a/src/test/java/com/prueba/core/http/authentication/AuthenticationInfoTest.java b/src/test/java/com/prueba/core/http/authentication/AuthenticationInfoTest.java new file mode 100644 index 0000000..81d6b8a --- /dev/null +++ b/src/test/java/com/prueba/core/http/authentication/AuthenticationInfoTest.java @@ -0,0 +1,17 @@ +package com.prueba.core.http.authentication; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class AuthenticationInfoTest { + private static final String USERNAME = "GUMARTINM"; + + @Test + public void shouldCallGetters() { + AuthenticationInfo authenticationInfo = new AuthenticationInfo(USERNAME); + + assertEquals(USERNAME, authenticationInfo.getUsername()); + } + +} diff --git a/src/test/java/com/prueba/core/http/sessions/SessionInfoTest.java b/src/test/java/com/prueba/core/http/sessions/SessionInfoTest.java index 6f3e776..1ab74ac 100644 --- a/src/test/java/com/prueba/core/http/sessions/SessionInfoTest.java +++ b/src/test/java/com/prueba/core/http/sessions/SessionInfoTest.java @@ -1,6 +1,6 @@ package com.prueba.core.http.sessions; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import java.time.LocalDateTime; import java.util.UUID; @@ -14,7 +14,7 @@ public class SessionInfoTest { @Test public void shouldCallGetters() { - SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION); + final SessionInfo sessionInfo = new SessionInfo(UUID_VALUE, USERNAME, LAST_SESSION); assertEquals(UUID_VALUE, sessionInfo.getUUID()); assertEquals(USERNAME, sessionInfo.getUsername()); diff --git a/src/test/java/com/prueba/core/http/sessions/SessionsTest.java b/src/test/java/com/prueba/core/http/sessions/SessionsTest.java new file mode 100644 index 0000000..70bdc7d --- /dev/null +++ b/src/test/java/com/prueba/core/http/sessions/SessionsTest.java @@ -0,0 +1,62 @@ +package com.prueba.core.http.sessions; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.time.LocalDateTime; +import java.util.UUID; + +import org.junit.Test; + +public class SessionsTest { + private static final String USERNAME = "GUMARTIN"; + + @Test + public void shouldCreateSessionSingleton() { + final Sessions sessions = Sessions.getInstance(); + + assertNotNull(sessions); + } + + @Test + public void whenUUIDSessionIsNotPersistedThenReturnNull() { + final Sessions sessions = Sessions.getInstance(); + + assertNull(sessions.getSession(UUID.randomUUID())); + } + + @Test + public void whenUUIDSessionIsPersistedThenReturnSession() { + final UUID uuidSession = UUID.randomUUID(); + final Sessions sessions = Sessions.getInstance(); + sessions.refreshSession(uuidSession, USERNAME); + + SessionInfo sessionInfo = sessions.getSession(uuidSession); + + assertEquals(USERNAME, sessionInfo.getUsername()); + assertEquals(uuidSession, sessionInfo.getUUID()); + } + + @Test + public void whenThereIsNotSessionInThreadContextThenIsNotValidSession() { + final Sessions sessions = Sessions.getInstance(); + + assertFalse(sessions.isValidSession()); + } + + @Test + public void whenThereIsSessionInThreadContextAndSessionDidNotExpireThenIsValidSession() { + final SessionInfo sessionInfo = doSampleSessionInfo(); + SessionContext.setSession(sessionInfo); + final Sessions sessions = Sessions.getInstance(); + + assertTrue(sessions.isValidSession()); + } + + private SessionInfo doSampleSessionInfo() { + return new SessionInfo(UUID.randomUUID(), USERNAME, LocalDateTime.now()); + } +} diff --git a/src/test/java/com/prueba/model/domain/AccountResourceTest.java b/src/test/java/com/prueba/model/domain/AccountResourceTest.java new file mode 100644 index 0000000..fbc3dc1 --- /dev/null +++ b/src/test/java/com/prueba/model/domain/AccountResourceTest.java @@ -0,0 +1,30 @@ +package com.prueba.model.domain; + +import static org.junit.Assert.*; + +import java.time.LocalDateTime; +import java.util.UUID; + +import org.junit.Test; + +import com.prueba.core.http.sessions.SessionInfo; + +public class AccountResourceTest { + private static final String CODE = "GUMARTIN"; + private static final String NAME = "Gustavo"; + private static final String SURNAME = "Martin Morcuende"; + private static final String PASSWORD = "lame"; + private static final String ROLE = "MEGA_SUPER_ADMIN"; + + @Test + public void shouldCallGetters() { + final AccountResource accountResource = new AccountResource(CODE, NAME, SURNAME, PASSWORD, ROLE); + + assertEquals(CODE, accountResource.getCode()); + assertEquals(NAME, accountResource.getName()); + assertEquals(SURNAME, accountResource.getSurname()); + assertEquals(PASSWORD, accountResource.getPassword()); + assertEquals(ROLE, accountResource.getRole()); + } + +} diff --git a/src/test/java/com/prueba/model/domain/ApplicationResourceResourceTest.java b/src/test/java/com/prueba/model/domain/ApplicationResourceResourceTest.java new file mode 100644 index 0000000..d849bac --- /dev/null +++ b/src/test/java/com/prueba/model/domain/ApplicationResourceResourceTest.java @@ -0,0 +1,18 @@ +package com.prueba.model.domain; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ApplicationResourceResourceTest { + private static final String HTTPMETHOD = "GET"; + private static final String URLPATTERN = "/app/pages/page_1.html"; + + @Test + public void shouldCallGetters() { + final ApplicationResourceResource applicationResource = new ApplicationResourceResource(URLPATTERN, HTTPMETHOD); + + assertEquals(HTTPMETHOD, applicationResource.getHttpMethod()); + assertEquals(URLPATTERN, applicationResource.getUrlPattern()); + } +} -- 2.1.4