|
| 1 | +/* |
| 2 | + * Copyright 2024-Present Entando Inc. (http://www.entando.com) All rights reserved. |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU Lesser General Public License as published by the Free |
| 6 | + * Software Foundation; either version 2.1 of the License, or (at your option) |
| 7 | + * any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 12 | + * details. |
| 13 | + */ |
| 14 | +package com.agiletec.plugins.jacms.apsadmin.content; |
| 15 | + |
| 16 | +import com.agiletec.aps.system.SystemConstants; |
| 17 | +import com.agiletec.aps.system.services.authorization.IAuthorizationManager; |
| 18 | +import com.agiletec.aps.system.services.group.Group; |
| 19 | +import com.agiletec.aps.system.services.group.IGroupManager; |
| 20 | +import com.agiletec.aps.system.services.lang.ILangManager; |
| 21 | +import com.agiletec.aps.system.services.user.UserDetails; |
| 22 | +import com.agiletec.apsadmin.system.BaseAction; |
| 23 | +import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager; |
| 24 | +import com.agiletec.plugins.jacms.aps.system.services.content.model.Content; |
| 25 | +import com.agiletec.plugins.jacms.apsadmin.content.helper.IContentActionHelper; |
| 26 | +import com.opensymphony.xwork2.ActionSupport; |
| 27 | +import javax.servlet.http.HttpServletRequest; |
| 28 | +import javax.servlet.http.HttpSession; |
| 29 | +import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager; |
| 30 | +import org.entando.entando.aps.system.services.userpreferences.UserPreferences; |
| 31 | +import org.entando.entando.ent.exception.EntException; |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 36 | +import org.mockito.InjectMocks; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.mockito.MockitoAnnotations; |
| 40 | +import org.mockito.Spy; |
| 41 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 42 | + |
| 43 | +@ExtendWith(MockitoExtension.class) |
| 44 | +class IntroNewContentActionTest { |
| 45 | + |
| 46 | + @Mock |
| 47 | + private UserDetails currentUser; |
| 48 | + @Mock |
| 49 | + private HttpServletRequest request; |
| 50 | + @Mock |
| 51 | + private HttpSession session; |
| 52 | + @Mock |
| 53 | + private ILangManager langManager; |
| 54 | + @Mock |
| 55 | + private IGroupManager groupManager; |
| 56 | + @Mock |
| 57 | + private IUserPreferencesManager userPreferencesManager; |
| 58 | + @Mock |
| 59 | + private IContentManager contentManager; |
| 60 | + @Mock |
| 61 | + private IContentActionHelper contentActionHelper; |
| 62 | + @Mock |
| 63 | + private IAuthorizationManager authorizationManager; |
| 64 | + |
| 65 | + @Spy |
| 66 | + @InjectMocks |
| 67 | + private IntroNewContentAction action; |
| 68 | + |
| 69 | + @BeforeEach |
| 70 | + void initMocks() { |
| 71 | + MockitoAnnotations.initMocks(this); |
| 72 | + action.setServletRequest(this.request); |
| 73 | + action.setGroupManager(this.groupManager); |
| 74 | + action.setLangManager(this.langManager); |
| 75 | + action.setContentManager(this.contentManager); |
| 76 | + action.setContentActionHelper(this.contentActionHelper); |
| 77 | + action.setLangManager(this.langManager); |
| 78 | + action.setUserPreferencesManager(this.userPreferencesManager); |
| 79 | + action.setAuthorizationManager(this.authorizationManager); |
| 80 | + Mockito.when(this.request.getSession()).thenReturn(this.session); |
| 81 | + Mockito.when(this.request.getSession().getAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER)).thenReturn(this.currentUser); |
| 82 | + Mockito.when(this.currentUser.getUsername()).thenReturn("username"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void showldReturnErrorWithMissingContentType() { |
| 87 | + Mockito.doReturn("invalid content type").when(action).getText(Mockito.anyString()); |
| 88 | + action.setContentTypeCode("XXX"); |
| 89 | + Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(null); |
| 90 | + String result = this.action.createNew(); |
| 91 | + Assertions.assertEquals(ActionSupport.INPUT, result); |
| 92 | + Assertions.assertEquals(1, action.getFieldErrors().size()); |
| 93 | + Assertions.assertEquals(1, action.getFieldErrors().get("contentTypeCode").size()); |
| 94 | + Assertions.assertEquals("invalid content type", action.getFieldErrors().get("contentTypeCode").get(0)); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + void showldExecuteCreateNewContentWithoutPreferences() { |
| 99 | + action.setContentTypeCode("XXX"); |
| 100 | + Content prototype = new Content(); |
| 101 | + Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype); |
| 102 | + String result = this.action.createNew(); |
| 103 | + Assertions.assertEquals(ActionSupport.SUCCESS, result); |
| 104 | + Assertions.assertEquals(0, action.getFieldErrors().size()); |
| 105 | + Assertions.assertNull(prototype.getMainGroup()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void showldExecuteCreateNewContentWithPreferences() throws Exception { |
| 110 | + action.setContentTypeCode("XXX"); |
| 111 | + Content prototype = new Content(); |
| 112 | + Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype); |
| 113 | + UserPreferences userPreferences = new UserPreferences(); |
| 114 | + userPreferences.setDefaultContentOwnerGroup("main_group"); |
| 115 | + userPreferences.setDefaultContentJoinGroups("join1;join2"); |
| 116 | + Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenReturn(userPreferences); |
| 117 | + Mockito.when(this.groupManager.getGroup(Mockito.anyString())).thenReturn(Mockito.mock(Group.class)); |
| 118 | + String result = this.action.createNew(); |
| 119 | + Assertions.assertEquals(ActionSupport.SUCCESS, result); |
| 120 | + Assertions.assertEquals(0, action.getFieldErrors().size()); |
| 121 | + Mockito.verify(this.session, Mockito.times(1)) |
| 122 | + .setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP, "main_group"); |
| 123 | + Assertions.assertEquals(2, prototype.getGroups().size()); |
| 124 | + Assertions.assertTrue(prototype.getGroups().contains("join1")); |
| 125 | + Assertions.assertTrue(prototype.getGroups().contains("join2")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void showldExecuteCreateNewContentWithEmptyPreferences() throws Exception { |
| 130 | + action.setContentTypeCode("XXX"); |
| 131 | + Content prototype = new Content(); |
| 132 | + Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype); |
| 133 | + Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenReturn(new UserPreferences()); |
| 134 | + String result = this.action.createNew(); |
| 135 | + Assertions.assertEquals(ActionSupport.SUCCESS, result); |
| 136 | + Assertions.assertEquals(0, action.getFieldErrors().size()); |
| 137 | + Assertions.assertNull(prototype.getMainGroup()); |
| 138 | + Assertions.assertEquals(0, prototype.getGroups().size()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void showldExecuteCreateNewContentWithErrorOnPreferences() throws Exception { |
| 143 | + action.setContentTypeCode("XXX"); |
| 144 | + Content prototype = new Content(); |
| 145 | + Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype); |
| 146 | + Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenThrow(new EntException("Error")); |
| 147 | + String result = this.action.createNew(); |
| 148 | + Assertions.assertEquals(BaseAction.FAILURE, result); |
| 149 | + Assertions.assertEquals(0, action.getFieldErrors().size()); |
| 150 | + Assertions.assertNull(prototype.getMainGroup()); |
| 151 | + Assertions.assertEquals(0, prototype.getGroups().size()); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments