PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jira-project/jira-components/jira-tests-parent/jira-tests-unit/src/test/java/com/atlassian/jira/jql/permission/TestProjectLiteralSanitiser.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 174 lines | 131 code | 40 blank | 3 comment | 0 complexity | af1b5e3f85c6fc2f306940126b7dad06 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.jql.permission;
  2. import com.atlassian.jira.jql.operand.QueryLiteral;
  3. import com.atlassian.jira.jql.resolver.IndexInfoResolver;
  4. import com.atlassian.jira.jql.resolver.NameResolver;
  5. import com.atlassian.jira.project.MockProject;
  6. import com.atlassian.jira.project.Project;
  7. import com.atlassian.jira.security.PermissionManager;
  8. import com.atlassian.jira.user.ApplicationUser;
  9. import com.atlassian.jira.user.MockApplicationUser;
  10. import com.atlassian.jira.util.collect.CollectionBuilder;
  11. import org.junit.Before;
  12. import org.junit.Rule;
  13. import org.junit.Test;
  14. import org.mockito.Mock;
  15. import org.mockito.junit.MockitoJUnit;
  16. import org.mockito.junit.MockitoRule;
  17. import java.util.Collections;
  18. import java.util.List;
  19. import static com.atlassian.jira.jql.operand.SimpleLiteralFactory.createLiteral;
  20. import static com.atlassian.jira.permission.ProjectPermissions.BROWSE_PROJECTS;
  21. import static org.junit.Assert.assertEquals;
  22. import static org.junit.Assert.assertFalse;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.mockito.Mockito.when;
  25. /**
  26. * @since v4.0
  27. */
  28. public class TestProjectLiteralSanitiser {
  29. @Rule
  30. public MockitoRule mockito = MockitoJUnit.rule();
  31. @Mock
  32. private IndexInfoResolver<Project> projectIndexInfoResolver;
  33. @Mock
  34. private NameResolver<Project> projectResolver;
  35. @Mock
  36. private PermissionManager permissionManager;
  37. private ApplicationUser user;
  38. private ProjectLiteralSanitiser sanitiser;
  39. @Before
  40. public void setUp() throws Exception {
  41. user = new MockApplicationUser("fred");
  42. sanitiser = new ProjectLiteralSanitiser(projectResolver, permissionManager, projectIndexInfoResolver, user);
  43. }
  44. @Test
  45. public void testGetIndexValues() throws Exception {
  46. final List<String> expectedValues1 = Collections.singletonList("StringIndexValues");
  47. final List<String> expectedValues2 = Collections.singletonList("LongIndexValues");
  48. when(projectIndexInfoResolver.getIndexedValues("litString")).thenReturn(expectedValues1);
  49. when(projectIndexInfoResolver.getIndexedValues(1111L)).thenReturn(expectedValues2);
  50. assertEquals(expectedValues1, sanitiser.getIndexValues(createLiteral("litString")));
  51. assertEquals(expectedValues2, sanitiser.getIndexValues(createLiteral(1111L)));
  52. assertEquals(Collections.<String>emptyList(), sanitiser.getIndexValues(new QueryLiteral()));
  53. }
  54. @Test
  55. public void testTwoProjectsNoModification() throws Exception {
  56. final List<QueryLiteral> literals = Collections.singletonList(createLiteral("HSP"));
  57. final MockProject project1 = new MockProject(10000L);
  58. final MockProject project2 = new MockProject(20000L);
  59. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(CollectionBuilder.newBuilder("10000", "20000").asList());
  60. when(projectResolver.get(10000L)).thenReturn(project1);
  61. when(projectResolver.get(20000L)).thenReturn(project2);
  62. when(permissionManager.hasPermission(BROWSE_PROJECTS, project1, user)).thenReturn(true);
  63. when(permissionManager.hasPermission(BROWSE_PROJECTS, project2, user)).thenReturn(true);
  64. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(literals);
  65. assertFalse(result.isModified());
  66. }
  67. @Test
  68. public void testOneProjectModification() throws Exception {
  69. final List<QueryLiteral> literals = Collections.singletonList(createLiteral("HSP"));
  70. final QueryLiteral expectedLiteral = createLiteral(10000L);
  71. final MockProject project1 = new MockProject(10000L);
  72. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(Collections.singletonList("10000"));
  73. when(projectResolver.get(10000L)).thenReturn(project1);
  74. when(permissionManager.hasPermission(BROWSE_PROJECTS, project1, user)).thenReturn(false);
  75. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(literals);
  76. assertTrue(result.isModified());
  77. assertEquals(1, result.getLiterals().size());
  78. assertEquals(expectedLiteral, result.getLiterals().get(0));
  79. }
  80. @Test
  81. public void testTwoProjectsOneModification() throws Exception {
  82. final QueryLiteral expectedLiteral1 = createLiteral("HSP");
  83. final QueryLiteral expectedLiteral2 = createLiteral(20000L);
  84. final List<QueryLiteral> inputLiterals = Collections.singletonList(expectedLiteral1);
  85. final List<QueryLiteral> expectedLiterals = CollectionBuilder.newBuilder(expectedLiteral1, expectedLiteral2).asList();
  86. final MockProject project1 = new MockProject(10000L);
  87. final MockProject project2 = new MockProject(20000L);
  88. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(CollectionBuilder.newBuilder("10000", "20000").asList());
  89. when(projectResolver.get(10000L)).thenReturn(project1);
  90. when(projectResolver.get(20000L)).thenReturn(project2);
  91. when(permissionManager.hasPermission(BROWSE_PROJECTS, project1, user)).thenReturn(true);
  92. when(permissionManager.hasPermission(BROWSE_PROJECTS, project2, user)).thenReturn(false);
  93. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(inputLiterals);
  94. assertTrue(result.isModified());
  95. assertEquals(expectedLiterals, result.getLiterals());
  96. }
  97. @Test
  98. public void testTwoProjectsModification() throws Exception {
  99. final List<QueryLiteral> inputLiterals = Collections.singletonList(createLiteral("HSP"));
  100. final List<QueryLiteral> expectedLiterals = CollectionBuilder.newBuilder(createLiteral(10000L), createLiteral(20000L)).asList();
  101. final MockProject project1 = new MockProject(10000L);
  102. final MockProject project2 = new MockProject(20000L);
  103. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(CollectionBuilder.newBuilder("10000", "20000").asList());
  104. when(projectResolver.get(10000L)).thenReturn(project1);
  105. when(projectResolver.get(20000L)).thenReturn(project2);
  106. when(permissionManager.hasPermission(BROWSE_PROJECTS, project1, user)).thenReturn(false);
  107. when(permissionManager.hasPermission(BROWSE_PROJECTS, project2, user)).thenReturn(false);
  108. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(inputLiterals);
  109. assertTrue(result.isModified());
  110. assertEquals(expectedLiterals, result.getLiterals());
  111. }
  112. @Test
  113. public void testTwoProjectsOneDoesNotExist() throws Exception {
  114. final List<QueryLiteral> inputLiterals = Collections.singletonList(createLiteral("HSP"));
  115. final List<QueryLiteral> expectedLiterals = CollectionBuilder.newBuilder(createLiteral("HSP"), createLiteral(20000L)).asList();
  116. final MockProject project2 = new MockProject(20000L);
  117. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(CollectionBuilder.newBuilder("10000", "20000").asList());
  118. when(projectResolver.get(10000L)).thenReturn(null);
  119. when(projectResolver.get(20000L)).thenReturn(project2);
  120. when(permissionManager.hasPermission(BROWSE_PROJECTS, project2, user)).thenReturn(false);
  121. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(inputLiterals);
  122. assertTrue(result.isModified());
  123. assertEquals(expectedLiterals, result.getLiterals());
  124. }
  125. @Test
  126. public void testTwoProjectsTwoDoesNotExist() throws Exception {
  127. final List<QueryLiteral> inputLiterals = Collections.singletonList(createLiteral("HSP"));
  128. when(projectIndexInfoResolver.getIndexedValues("HSP")).thenReturn(CollectionBuilder.newBuilder("10000", "20000").asList());
  129. when(projectResolver.get(10000L)).thenReturn(null);
  130. when(projectResolver.get(20000L)).thenReturn(null);
  131. final LiteralSanitiser.Result result = sanitiser.sanitiseLiterals(inputLiterals);
  132. assertFalse(result.isModified());
  133. }
  134. }