Improving coverage
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 4 Oct 2016 20:40:01 +0000 (22:40 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 4 Oct 2016 20:40:01 +0000 (22:40 +0200)
src/main/java/com/prueba/core/http/authentication/AuthenticationInfo.java
src/main/java/com/prueba/core/http/handles/ApiHandler.java
src/main/java/com/prueba/core/http/handles/PagesHandler.java
src/main/java/com/prueba/core/http/sessions/Sessions.java
src/main/java/com/prueba/services/impl/LoginServiceImpl.java
src/test/java/com/prueba/core/context/security/handle/SessionHandleTest.java [deleted file]
src/test/java/com/prueba/core/http/authentication/AuthenticationInfoTest.java [new file with mode: 0644]
src/test/java/com/prueba/core/http/sessions/SessionInfoTest.java
src/test/java/com/prueba/core/http/sessions/SessionsTest.java [new file with mode: 0644]
src/test/java/com/prueba/model/domain/AccountResourceTest.java [new file with mode: 0644]
src/test/java/com/prueba/model/domain/ApplicationResourceResourceTest.java [new file with mode: 0644]

index 3c3d277..46c5041 100644 (file)
@@ -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;
        }
 }
index 779bce7..7836a4d 100644 (file)
@@ -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);
                        
index 0a526d5..2f1e12e 100644 (file)
@@ -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(),
index 1ac93e4..ab1c515 100644 (file)
@@ -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;
index 495e8de..8df3e78 100644 (file)
@@ -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 (file)
index ebc4cb3..0000000
+++ /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 (file)
index 0000000..81d6b8a
--- /dev/null
@@ -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());
+       }
+
+}
index 6f3e776..1ab74ac 100644 (file)
@@ -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 (file)
index 0000000..70bdc7d
--- /dev/null
@@ -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 (file)
index 0000000..fbc3dc1
--- /dev/null
@@ -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 (file)
index 0000000..d849bac
--- /dev/null
@@ -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());
+       }
+}