Skip to content

Commit

Permalink
unit test implemented (#2715)
Browse files Browse the repository at this point in the history
* unit test implemented

* fixed formattting issues

---------

Co-authored-by: Snehit Roda <[email protected]>
  • Loading branch information
snehit221 and Snehit Roda authored Nov 10, 2023
1 parent 989ba28 commit 195c719
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.apereo.portal;

import static org.junit.Assert.*;

import org.apereo.portal.dao.usertype.FunctionalNameType;
import org.junit.Test;

/** @author snehitroda */
public class FunctionalNameTypeTest {

@Test
public void testValidateValidFunctionalName() {
// Arrange
String validFname = "my_functional-name123";

// Act & Assert
assertTrue(FunctionalNameType.isValid(validFname));
}

@Test
public void testValidateInvalidFunctionalName() {
// Arrange
String invalidFname = "my functional name"; // Contains spaces

// Act & Assert
assertFalse(FunctionalNameType.isValid(invalidFname));
}

@Test(expected = IllegalArgumentException.class)
public void testValidateThrowsIllegalArgumentException() {
// Arrange
String invalidFname = "my functional name"; // Contains spaces

// Act
FunctionalNameType.validate(invalidFname);
}

@Test
public void testMakeValidNullInput() {
// Arrange
String nullFname = null;

// Act
String result = FunctionalNameType.makeValid(nullFname);

// Assert
assertEquals("_", result);
}

@Test
public void testMakeValidWithSpecialCharacters() {
// Arrange
String fnameWithSpecialChars = "my_functional#name123";

// Act
String result = FunctionalNameType.makeValid(fnameWithSpecialChars);

// Assert
assertEquals("my_functional_name123", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
package org.apereo.portal.url;

import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertArrayEquals;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

/** */
Expand All @@ -30,4 +35,42 @@ public void testParameterMapClone() {

assertTrue(m2.equals(m1));
}

/**
* arranges a parameter map with test values
*
* @author snehit
*/
public Map<String, List<String>> arrangeParameterMap() {
Map<String, List<String>> paramMap = new LinkedHashMap<>();
paramMap.put("param1", Arrays.asList("value1", "value2"));
paramMap.put("param2", null);
return paramMap;
}

/** @author snehitroda */
@Test
public void testConvertListMapWithValues() {
// Arrange
Map<String, List<String>> paramMap = arrangeParameterMap();

// Act
ParameterMap mapResult = ParameterMap.convertListMap(paramMap);

// Assert
assertArrayEquals(new String[] {"value1", "value2"}, mapResult.get("param1"));
}

/** @author snehitroda */
@Test
public void testConvertListMapWithNoValues() {
// Arrange
Map<String, List<String>> paramMap = arrangeParameterMap();

// Act
ParameterMap mapResult = ParameterMap.convertListMap(paramMap);

// Assert
assertArrayEquals(new String[0], mapResult.get("param2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,101 @@
*/
package org.apereo.portal.portlet.container.services;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.*;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apereo.portal.portlet.dao.IPortletCookieDao;
import org.apereo.portal.portlet.om.IPortalCookie;
import org.apereo.portal.portlet.om.IPortletCookie;
import org.apereo.portal.portlet.om.IPortletWindowId;
import org.easymock.EasyMock;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

/** Tests for {@link PortletCookieServiceImpl}. */
public class PortletCookieServiceImplTest {

@Mock private HttpServletRequest request;
@Mock private HttpSession session;
@Mock private IPortletCookie portletCookie;
@Mock private SessionOnlyPortletCookieImpl sessionOnlyCookie;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}

/**
* validates the GetAllPortletCookies by verifying the cookie size generated
*
* @author snehit
*/
@Test
public void testGetAllPortletCookies() {
// Arrange
IPortletWindowId portletWindowId = mock(IPortletWindowId.class);
Cookie[] servletCookies = {
new Cookie("cookie1", "value1"), new Cookie("cookie2", "value2")
};
when(request.getCookies()).thenReturn(servletCookies);

IPortalCookie portalCookie = mock(IPortalCookie.class);

PortletCookieServiceImpl portletCookieService = new PortletCookieServiceImpl();
PortletCookieServiceImpl portletCookieServiceMock = mock(PortletCookieServiceImpl.class);

when(request.getSession()).thenReturn(session);
when(portletCookieServiceMock.getPortalCookie(request)).thenReturn(portalCookie);

Cookie convertedCookie = new Cookie("convertedCookie", "convertedValue");

when(portletCookieServiceMock.convertToCookie(any(IPortalCookie.class), anyBoolean()))
.thenReturn(convertedCookie);

when(portletCookie.getExpires()).thenReturn(DateTime.now().plusHours(1));

Set<IPortletCookie> portletCookies =
new HashSet<>(Collections.singletonList(portletCookie));
when(portalCookie.getPortletCookies()).thenReturn(portletCookies);

Map<String, SessionOnlyPortletCookieImpl> sessionOnlyPortletCookieMap = mock(Map.class);

when(portletCookieServiceMock.getSessionOnlyPortletCookieMap(request))
.thenReturn(sessionOnlyPortletCookieMap);

when(sessionOnlyCookie.getExpires()).thenReturn(DateTime.now().plusHours(1));
when(sessionOnlyCookie.toCookie()).thenReturn(new Cookie("sessionCookie", "sessionValue"));
when(sessionOnlyPortletCookieMap.values())
.thenReturn(Collections.singletonList(sessionOnlyCookie));

// Act
Cookie[] result = portletCookieService.getAllPortletCookies(request, portletWindowId);
int actualCookieSize = result.length;

// Assert
assertNotNull(result);

Cookie[] expectedCookies = {convertedCookie, sessionOnlyCookie.toCookie()};
int expectedCookieSize = expectedCookies.length;

// assert
assertEquals(expectedCookieSize, actualCookieSize);
}

/**
* Control test invocation of {@link
* PortletCookieServiceImpl#updatePortalCookie(javax.servlet.http.HttpServletRequest,
Expand Down

0 comments on commit 195c719

Please sign in to comment.