forked from sakaiproject/sakai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SAK-32166 Allow import from site on custom site type. (sakaiproject#3882
) When you are in a site that is inheriting it’s tools from another site type through configuration: # This allows repository site types to have all the normal project tools. projectSiteType=repository,project projectSiteTargetType=project The tools list presented is correct, but when you attempt to perform the import it fails. This is because when checking that the tools are valid for the site being imported into it doesn’t use this configuration to locate the list of valid tools. Although this isn’t an API I’ve added some tests to that we can check that the internal methods are behaving as expected.
- Loading branch information
Showing
4 changed files
with
208 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...manage/site-manage-tool/tool/src/test/org/sakaiproject/site/tool/SiteActionTestTools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.sakaiproject.site.tool; | ||
|
||
import org.hamcrest.collection.IsCollectionContaining; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.invocation.InvocationOnMock; | ||
import org.mockito.stubbing.Answer; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.sakaiproject.component.cover.ComponentManager; | ||
import org.sakaiproject.component.cover.ServerConfigurationService; | ||
import org.sakaiproject.event.api.SessionState; | ||
import org.sakaiproject.site.cover.SiteService; | ||
import org.sakaiproject.tool.api.Session; | ||
import org.sakaiproject.tool.api.SessionManager; | ||
import org.sakaiproject.tool.api.Tool; | ||
import org.sakaiproject.tool.cover.ToolManager; | ||
|
||
import java.util.*; | ||
|
||
import static java.util.Collections.singleton; | ||
import static java.util.Collections.singletonList; | ||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.sakaiproject.site.tool.SiteAction.STATE_DEFAULT_SITE_TYPE; | ||
import static org.sakaiproject.site.tool.SiteAction.STATE_SITE_TYPE; | ||
|
||
/** | ||
* Tests for SiteAction | ||
*/ | ||
@SuppressWarnings("deprecation") | ||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({ComponentManager.class}) | ||
public class SiteActionTestTools { | ||
|
||
private SiteAction siteAction; | ||
|
||
@Before | ||
public void setUp() { | ||
PowerMockito.mockStatic(ComponentManager.class); | ||
// A mock component manager. | ||
when(ComponentManager.get(any(Class.class))).then(new Answer<Object>() { | ||
private Map<Class, Object> mocks = new HashMap<>(); | ||
@Override | ||
public Object answer(InvocationOnMock invocation) throws Throwable { | ||
Class classToMock = (Class) invocation.getArguments()[0]; | ||
return mocks.computeIfAbsent(classToMock, k -> mock(classToMock)); | ||
} | ||
}); | ||
// Mock the Session so that ResourceLoader doesn't NPE on init | ||
when(ComponentManager.get(SessionManager.class).getCurrentSession()).thenReturn(mock(Session.class)); | ||
siteAction = new SiteAction(); | ||
} | ||
|
||
@Test | ||
public void testGetToolRegistrationsSimple() { | ||
// Normal project site type | ||
Tool projectTool = mock(Tool.class); | ||
when(ToolManager.findTools(singleton("project"), null)).thenReturn(singleton(projectTool)); | ||
when(SiteService.getSiteTypeStrings("project")).thenReturn(singletonList("project")); | ||
when(ServerConfigurationService.getString("projectSiteTargetType", "project")).thenReturn("project"); | ||
|
||
SessionState state = mock(SessionState.class); | ||
Set<Tool> project = siteAction.getToolRegistrations(state, "project"); | ||
assertThat(project, IsCollectionContaining.hasItems(projectTool)); | ||
} | ||
|
||
@Test | ||
public void testGetToolRegistrationNone() { | ||
// Site type that doesn't exist | ||
SessionState state = mock(SessionState.class); | ||
Set<Tool> other = siteAction.getToolRegistrations(state, "other"); | ||
assertTrue(other.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testGetToolRegistrationDefault() { | ||
// Check that we fallback to the default site type | ||
Tool projectTool = mock(Tool.class); | ||
when(ToolManager.findTools(singleton("project"), null)).thenReturn(singleton(projectTool)); | ||
when(SiteService.getSiteTypeStrings("new")).thenReturn(Arrays.asList("project", "new")); | ||
when(ServerConfigurationService.getString("projectSiteTargetType", "project")).thenReturn("project"); | ||
|
||
SessionState state = mock(SessionState.class); | ||
when(state.getAttribute(STATE_DEFAULT_SITE_TYPE)).thenReturn("project"); | ||
Set<Tool> tools = siteAction.getToolRegistrations(state, "new"); | ||
assertThat(tools, IsCollectionContaining.hasItems(projectTool)); | ||
} | ||
|
||
@Test | ||
public void testOriginalToolIds() { | ||
// Normal project site type | ||
Tool singleTool = mock(Tool.class); | ||
Tool multipleTool = mock(Tool.class); | ||
when(singleTool.getId()).thenReturn("sakai.single"); | ||
when(multipleTool.getId()).thenReturn("sakai.multiple"); | ||
when(ToolManager.findTools(singleton("project"), null)).thenReturn(new HashSet<>(Arrays.asList(singleTool, multipleTool))); | ||
when(ToolManager.getTool("sakai.single")).thenReturn(singleTool); | ||
when(ToolManager.getTool("sakai.multiple")).thenReturn(multipleTool); | ||
Properties singleToolProperties = new Properties(); | ||
singleToolProperties.put("allowMultipleInstances", "false"); | ||
when(singleTool.getRegisteredConfig()).thenReturn(singleToolProperties); | ||
Properties multipleToolProperties = new Properties(); | ||
multipleToolProperties.put("allowMultipleInstances", "true"); | ||
when(multipleTool.getRegisteredConfig()).thenReturn(multipleToolProperties); | ||
|
||
when(SiteService.getSiteTypeStrings("project")).thenReturn(singletonList("project")); | ||
when(ServerConfigurationService.getString("projectSiteTargetType", "project")).thenReturn("project"); | ||
|
||
SessionState state = mock(SessionState.class); | ||
when(state.getAttribute(STATE_SITE_TYPE)).thenReturn("project"); | ||
|
||
List<String> filteredToolIds; | ||
filteredToolIds = siteAction.originalToolIds(singletonList("not.present"), state); | ||
assertTrue(filteredToolIds.isEmpty()); | ||
|
||
filteredToolIds = siteAction.originalToolIds(singletonList("sakai.single"), state); | ||
assertEquals(Arrays.asList("sakai.single"), filteredToolIds); | ||
|
||
filteredToolIds = siteAction.originalToolIds(Arrays.asList("sakai.single", "not.present"), state); | ||
assertEquals(Arrays.asList("sakai.single"), filteredToolIds); | ||
|
||
filteredToolIds = siteAction.originalToolIds(Arrays.asList("sakai.single1", "not.present"), state); | ||
assertEquals(Arrays.asList(), filteredToolIds); | ||
|
||
filteredToolIds = siteAction.originalToolIds(Arrays.asList("sakai.multiple", "not.present"), state); | ||
assertEquals(Arrays.asList("sakai.multiple"), filteredToolIds); | ||
|
||
filteredToolIds = siteAction.originalToolIds(Arrays.asList("sakai.multiple1", "sakai.multiple2"), state); | ||
assertEquals(Arrays.asList("sakai.multiple"), filteredToolIds); | ||
|
||
// Has to be 36 characters long. | ||
filteredToolIds = siteAction.originalToolIds(Arrays.asList("012345678901234567890123456789012345sakai.multiple"), state); | ||
assertEquals(Arrays.asList("sakai.multiple"), filteredToolIds); | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters