private GenericValue newChangeItem(String field, String newValue) { GenericValue changeItem = mock(GenericValue.class); when(changeItem.getString("field")).thenReturn(field); when(changeItem.getString("newvalue")).thenReturn(newValue); when(changeItem.getString("newstring")).thenReturn(newValue); return changeItem; }
public void testGetQueryWorksCorrectly() throws Exception { SecurityType securityType = (SecurityType) ManagerFactory.getPermissionTypeManager().getSchemeType("reporter"); // Setup permissions so that a query is created GenericValue project = UtilsForTests.getTestEntity("Project", EasyMap.build("name", "Project")); PermissionSchemeManager permissionSchemeManager = ManagerFactory.getPermissionSchemeManager(); GenericValue scheme = permissionSchemeManager.createScheme("Scheme", "scheme"); permissionSchemeManager.addSchemeToProject(project, scheme); SchemeEntity schemeEntity = new SchemeEntity(securityType.getType(), null, new Long(Permissions.BROWSE)); permissionSchemeManager.createSchemeEntity(scheme, schemeEntity); Query query = securityType.getQuery(u, project, null); assertEquals( "(+" + DocumentConstants.PROJECT_ID + ":" + project.getLong("id") + " +" + DocumentConstants.ISSUE_AUTHOR + ":owen)", query.toString("")); }
// I have found no way of unit testing GenericValues private Collection<String> genericValueToString(Collection<GenericValue> values, String field) { Collection<String> comps = new ArrayList<String>(); for (GenericValue ge : values) { comps.add(ge.getString(field)); } return comps; }
// Ensure that the filter subscriptions table does not contain references to search requests that // have been deleted. protected void doRealCheck( boolean correct, GenericValue subscription, List<DeleteEntityAmendment> messages) throws IntegrityException { // try to find the related quartz trigger, if null then flag JobDetails jobDetails = getScheduledJob(subscription); if (jobDetails == null) { if (correct) { // flag the current subscription for deletion messages.add( new DeleteEntityAmendment( Amendment.CORRECTION, getI18NBean() .getText( "admin.integrity.check.filter.subscriptions.trigger.message", subscription.getString("id")), subscription)); } else { messages.add( new DeleteEntityAmendment( Amendment.ERROR, getI18NBean() .getText( "admin.integrity.check.filter.subscriptions.trigger.preview", subscription.getString("id")), subscription)); } } }
public void doUpgrade(boolean setupMode) throws Exception { // find all portalpages that have no version set yet. final OfBizListIterator iterator = delegator.findListIteratorByCondition( OfBizPortalPageStore.Table.NAME, new EntityExpr(OfBizPortalPageStore.Column.VERSION, EntityOperator.EQUALS, null)); final List<Long> portalPageIds = new ArrayList<Long>(); try { GenericValue portalPageGv = iterator.next(); while (portalPageGv != null) { portalPageIds.add(portalPageGv.getLong(OfBizPortalPageStore.Column.ID)); portalPageGv = iterator.next(); } } finally { iterator.close(); } try { // set version to 0 for all dashboard pages returned by the previous query. final int rowsUpdated = delegator.bulkUpdateByPrimaryKey( OfBizPortalPageStore.Table.NAME, MapBuilder.<String, Object>newBuilder() .add(OfBizPortalPageStore.Column.VERSION, 0L) .toMap(), portalPageIds); log.info("Initialised " + rowsUpdated + " dashboard versions to 0."); } finally { portalPageStore.flush(); } }
@Override public void update( Comment comment, Map<String, JSONObject> commentProperties, boolean dispatchEvent) { if (comment == null) { throw new IllegalArgumentException("Comment must not be null"); } if (comment.getId() == null) { throw new IllegalArgumentException("Comment ID must not be null"); } // create persistable generic value GenericValue commentGV; // We need an in-memory copy of the old comment so we can pass it through in the fired event and // to make sure // that some fields have changed. Comment originalComment = getCommentById(comment.getId()); if (originalComment == null) { throw new IllegalArgumentException( "Can not find a comment in the datastore with id: " + comment.getId()); } // Make sure that either the comment body or visibility data has changed, otherwise do not // update the datastore if (!areCommentsEquivalent(originalComment, comment)) { try { commentGV = delegator.findById(COMMENT_ENTITY, comment.getId()); populateGenericValueFromComment(comment, commentGV); commentGV.store(); } catch (GenericEntityException e) { throw new DataAccessException(e); } // Update the issue object IssueFactory issueFactory = ComponentAccessor.getComponentOfType(IssueFactory.class); GenericValue issueGV = comment.getIssue().getGenericValue(); MutableIssue mutableIssue = issueFactory.getIssue(issueGV); mutableIssue.setUpdated(UtilDateTime.nowTimestamp()); mutableIssue.store(); } // Update comment properties if (commentProperties != null) { setProperties(comment.getAuthorApplicationUser(), comment, commentProperties); } // Dispatch an event if required if (dispatchEvent) { dispatchIssueCommentEditedEvent( comment, MapBuilder.build( "eventsource", IssueEventSource.ACTION, EVENT_ORIGINAL_COMMENT_PARAMETER, originalComment)); } }
private void populateGenericValueFromComment(Comment updatedComment, GenericValue commentGV) { ApplicationUser updateAuthor = updatedComment.getUpdateAuthorApplicationUser(); commentGV.setString("updateauthor", updateAuthor == null ? null : updateAuthor.getKey()); commentGV.setString("body", updatedComment.getBody()); commentGV.setString("level", updatedComment.getGroupLevel()); commentGV.set("rolelevel", updatedComment.getRoleLevelId()); commentGV.set( "updated", JiraDateUtils.copyOrCreateTimestampNullsafe(updatedComment.getUpdated())); }
public void testDeleteEntity() throws CreateException, GenericEntityException { setupSchemes(); GenericValue entity2 = issueSchemeManager.createSchemeEntity( issueScheme, new SchemeEntity("type1", "group", new Long(2))); assertTrue(issueSchemeManager.getEntities(issueScheme).size() == 1); issueSchemeManager.deleteEntity(entity2.getLong("id")); assertTrue(issueSchemeManager.getEntities(issueScheme).size() == 0); }
@Override public void createValueWithoutId(final String entityName, final Map<String, Object> fields) throws DataAccessException { try { final GenericValue v = delegatorInterface.makeValue(entityName, fields); v.create(); } catch (final GenericEntityException ex) { throw new DataAccessException(ex); } }
/** Given a collection of entities, return an array of Strings representing the IDs */ public static String[] getStringArrayFromList(Collection entities) { String[] ar = new String[entities.size()]; int i = 0; for (Iterator iterator = entities.iterator(); iterator.hasNext(); i++) { GenericValue genericValue = (GenericValue) iterator.next(); ar[i] = genericValue.getString("id"); } return ar; }
private String getUserKey(String featureType, long entityId) { if (featureType.equals(USER_FEATURES)) { GenericValue usergv = ofBizDelegator.findById(Table.APPLICATION_USER, entityId); if (usergv != null) { return usergv.getString(Column.USERKEY); } } return null; }
public void testGetScheme() throws CreateException, GenericEntityException { setupSchemes(); assertTrue(issueSchemeManager.getSchemes().size() == 1); assertTrue(issueSchemeManager.getSchemes(project).size() == 1); assertNotNull(issueSchemeManager.getScheme(issueScheme.getLong("id"))); assertNotNull(issueSchemeManager.getScheme(issueScheme.getString("name"))); assertTrue(issueSchemeManager.getSchemes(project2).size() == 0); }
private RewardType fromGenericValue(GenericValue genval) { if (genval == null) { return null; } long id = (Long) genval.get(ID_FIELD); String name = (String) genval.get(NAME_FIELD); String namepl = (String) genval.get(NAMEPL_FIELD); String desc = (String) genval.get(DESC_FIELD); String iconURL = (String) genval.get(ICON_FIELD); return new RewardType(id, name, namepl, desc, iconURL); }
@Override public long getCountByAnd(final String entityName, final Map<String, ?> fields) { try { final EntityCondition condition = new EntityFieldMap(fields, EntityOperator.AND); final GenericValue countGV = EntityUtil.getOnly( delegatorInterface.findByCondition( entityName + "Count", condition, ImmutableList.of(COUNT_FIELD_NAME), null)); return countGV.getLong(COUNT_FIELD_NAME); } catch (final GenericEntityException e) { throw new DataAccessException(e); } }
/** * We need to make sure that we account for failed upgrades - the QA-EACJ migration showed that * the table may linger but the sequence ids go * * @param entityName * @param nextId * @throws GenericEntityException */ private void setNextId(String entityName, Long nextId) throws GenericEntityException { final GenericDelegator delegator = getDelegator(); // First ensure we have an entry in SequenecValueItem table delegator.getNextSeqId(entityName); // Now set it to nextId GenericValue sequenceItem = EntityUtil.getOnly( delegator.findByAnd("SequenceValueItem", ImmutableMap.of("seqName", entityName))); if (sequenceItem != null) { sequenceItem.set("seqId", nextId); sequenceItem.store(); delegator.refreshSequencer(); } }
private List doCheck(boolean correct) throws IntegrityException { List results = new ArrayList(); String message; List filtersToRemove = new ArrayList(); OfBizListIterator listIterator = null; try { listIterator = ofBizDelegator.findListIteratorByCondition("SearchRequest", null); GenericValue filter = (GenericValue) listIterator.next(); while (filter != null) { if (TextUtils.stringSet(filter.getString("project"))) { if (ofBizDelegator.findById("Project", filter.getLong("project")) == null) { if (correct) { message = getI18NBean() .getText( "admin.integrity.check.search.request.relation.check.message", filter.getString("name")); results.add(new CheckAmendment(Amendment.ERROR, message, "JRA-2279")); filtersToRemove.add(filter); } else { message = getI18NBean() .getText( "admin.integrity.check.search.request.relation.check.preview", filter.getString("name")); results.add(new CheckAmendment(Amendment.ERROR, message, "JRA-2279")); } } } filter = (GenericValue) listIterator.next(); } } finally { if (listIterator != null) { // Close the iterator listIterator.close(); } } if (correct && !filtersToRemove.isEmpty()) { try { ofBizDelegator.removeAll(filtersToRemove); } catch (DataAccessException e) { throw new IntegrityException(e); } } return results; }
public Collection getAddableIssueTypes() { if (addableIssueTypes == null) { addableIssueTypes = new LinkedList(getAllRelevantIssueTypeObjects()); for (Iterator iterator = addableIssueTypes.iterator(); iterator.hasNext(); ) { IssueType issueType = (IssueType) iterator.next(); GenericValue issueTypeGV = issueType.getGenericValue(); if (getFieldLayoutScheme().getEntity(issueTypeGV.getString("id")) != null) { iterator.remove(); } } } return addableIssueTypes; }
public void testMergeDuplicateSchemes() throws Exception { for (int i = 1; i <= PROJECTS_WITH_PERMS; i++) { // if (i > Permissions.MAX_PERMISSION) break; projectPerms = EntityUtils.createValue("Project", EasyMap.build("name", "Test Project" + i)); EntityUtils.createValue( "Permission", EasyMap.build( "project", projectPerms.getLong("id"), "type", new Long(10), "group", "test")); } task.createProjectSchemes(psm, psm.createDefaultScheme()); task.mergeDuplicateSchemes(psm); // There should be a scheme for projects with permissions and the default assertEquals(psm.getSchemes().size(), 2); List schemes = psm.getSchemes(); for (int i = 0; i < schemes.size(); i++) { GenericValue scheme = (GenericValue) schemes.get(i); List projects = psm.getProjects(scheme); for (int j = 0; j < projects.size(); j++) { GenericValue project = (GenericValue) projects.get(j); } } }
public void testDoUpgrade() throws Exception { for (int i = 1; i <= PROJECTS_WITH_PERMS; i++) { if (i > Permissions.MAX_PERMISSION) break; projectPerms = EntityUtils.createValue("Project", EasyMap.build("name", "Project" + i)); EntityUtils.createValue( "Permission", EasyMap.build( "project", projectPerms.getLong("id"), "type", new Long(10 + i), "group", "test")); } for (int i = 1; i <= PROJECTS_WITHOUT_PERMS; i++) { projectNoPerms = EntityUtils.createValue("Project", EasyMap.build("name", "No Perms" + i)); } task.doUpgrade(false); // check that the default permission scheme was added assertNotNull(psm.getDefaultScheme()); // All projects with no permissions should be added to default scheme assertEquals(psm.getProjects(psm.getDefaultScheme()).size(), PROJECTS_WITHOUT_PERMS); // There should be a scheme for all . The default and the one for project with permissions assertEquals(psm.getSchemes().size(), PROJECTS_WITH_PERMS + 1); }
public void testDeleteScheme() throws CreateException, GenericEntityException { setupSchemes(); assertTrue(issueSchemeManager.getSchemes().size() == 1); issueSchemeManager.deleteScheme(issueScheme.getLong("id")); assertTrue(issueSchemeManager.getSchemes().size() == 0); }
public GenericValue createValue(final String entityName, final Map<String, Object> fields) { try { final Map<String, Object> params = (fields == null) ? new HashMap<String, Object>(2) : new HashMap<String, Object>(fields); if (params.get("id") == null) { final Long id = delegatorInterface.getNextSeqId(entityName); params.put("id", id); } final GenericValue v = delegatorInterface.makeValue(entityName, params); v.create(); return v; } catch (final GenericEntityException ex) { throw new DataAccessException(ex); } }
@Test public void testGetProjectObjectsFromProjectCategory() throws GenericEntityException { // test null projectCategory id Collection<Project> projects = testedObject.getProjectObjectsFromProjectCategory(null); assertTrue(projects.isEmpty()); // test a valid projectCategory id associated with NO projects projects = testedObject.getProjectObjectsFromProjectCategory(projectCategory.getLong("id")); assertTrue(projects.isEmpty()); // test a valid projectCategory associated with a project testedObject.setProjectCategory(projectGV1, projectCategory); projects = testedObject.getProjectObjectsFromProjectCategory(projectCategory.getLong("id")); final Project project = Iterables.getOnlyElement(projects); assertEquals(project1.getId(), project.getId()); assertEquals(projectCategory, project.getProjectCategory()); }
public void testUpdateScheme() throws CreateException, GenericEntityException { setupSchemes(); issueScheme.set("name", "Test Update Scheme"); issueSchemeManager.updateScheme(issueScheme); assertTrue(issueSchemeManager.getSchemes().size() == 1); assertNotNull(issueSchemeManager.getScheme("Test Update Scheme")); }
@Override public Attachment setZip(Attachment attachment, boolean zip) { GenericValue attachmentGV; try { attachmentGV = ofBizDelegator.findById(AttachmentConstants.ATTACHMENT_ENTITY_NAME, attachment.getId()); attachmentGV.put("zip", zip ? IS_ZIP : NOT_ZIP); attachmentGV.store(); } catch (DataAccessException e) { log.error("Unable to find a file attachment with id: " + attachment.getId()); throw e; } catch (GenericEntityException e) { log.error("Unable to find a file attachment with id: " + attachment.getId()); throw new DataAccessException(e); } return new Attachment(issueManager, attachmentGV, attachment.getProperties()); }
public List<Attachment> getStoredAttachments(Issue issue) { try { GenericValue issueGV = issue.getGenericValue(); Collection<GenericValue> attachmentGvs = issueGV.getRelatedOrderBy( "ChildFileAttachment", EasyList.build("filename ASC", "created DESC")); List<Attachment> attachments = new ArrayList<Attachment>(attachmentGvs.size()); for (GenericValue attachmentGV : attachmentGvs) { attachments.add( new Attachment( issueManager, attachmentGV, OFBizPropertyUtils.getPropertySet(attachmentGV))); } return attachments; } catch (GenericEntityException e) { throw new DataAccessException(e); } }
private void migrateFeatures(String featureType) throws GenericEntityException { GenericValue sourcePropertyGV = getPropertyGV(Table.ENTRY, Column.KEY, featureType); if (sourcePropertyGV != null) { long entityId = sourcePropertyGV.getLong(Column.ENTITY_ID); long id = sourcePropertyGV.getLong(Column.ID); GenericValue sourceFeatureGV = ofBizDelegator.findById(Table.STRING, id); if (sourceFeatureGV != null) { Set<String> sourceFeatures = deserialize(sourceFeatureGV.getString(Column.VALUE)); sourceFeatureGV.remove(); for (String feature : sourceFeatures) { MapBuilder<String, Object> propertyMapBuilder = MapBuilder.newBuilder(); propertyMapBuilder.add(Column.FEATURE_NAME, feature); propertyMapBuilder.add(Column.FEATURE_TYPE, getFeatureType(featureType)); propertyMapBuilder.add(Column.USERKEY, getUserKey(featureType, entityId)); final Map<String, Object> featureMap = propertyMapBuilder.toMap(); if (featureNotPresent(featureMap)) { ofBizDelegator.createValue(Table.FEATURE, featureMap); } } } sourcePropertyGV.remove(); } }
@Test public void testDefaultAssigneeWithUnassigned() throws DefaultAssigneeException, GenericEntityException, OperationNotPermittedException, InvalidUserException, InvalidCredentialException { final User projectLead = userMockFactory.getProjectLead(); final GenericValue projectWithDefaultAssigneeLead = projectMockFactory.getProjectWithDefaultAssigneeLead(); final GenericValue projectWithDefaultUnassigned = projectMockFactory.getProjectWithDefaultUnassigned(); // Should be false as unassigned is turned off and project lead cannot be assigned issues. _testNoDefaultAssignee(projectWithDefaultUnassigned, null); when(permissionManager.hasPermission( Permissions.ASSIGNABLE_USER, projectWithDefaultAssigneeLead, projectLead)) .thenReturn(Boolean.TRUE); when(permissionManager.hasPermission( Permissions.ASSIGNABLE_USER, projectWithDefaultUnassigned, projectLead)) .thenReturn(Boolean.TRUE); // Should be false as unassigned is turned off and the lead is null so it fails _testNoDefaultAssignee(projectWithDefaultUnassigned, null); projectWithDefaultUnassigned.set("lead", projectLead.getName()); projectWithDefaultUnassigned.store(); // Should be true as unassigned is turned off and project lead can be assigned issues, // so it defaults to project lead. assertTrue(testedObject.isDefaultAssignee(projectWithDefaultUnassigned, null)); final User defaultAssignee = testedObject.getDefaultAssignee(projectWithDefaultUnassigned, null); assertEquals(projectLead, defaultAssignee); // Turn on unassigned ComponentAccessor.getApplicationProperties() .setOption(APKeys.JIRA_OPTION_ALLOWUNASSIGNED, true); // Reset permissions // Should be true as unassigned is turned on _testDefaultAssignee(projectWithDefaultUnassigned, null, null); }
public void testaddOldGlobalPermissionsToScheme() throws Exception { GenericValue scheme = psm.createScheme("test", "test"); EntityUtils.createValue( "Permission", EasyMap.build("project", null, "type", new Long(99), "group", "test")); task.addOldGlobalPermissionsToScheme(psm, 99, scheme); assertNotNull(psm.getScheme("test")); List perms = psm.getEntities(scheme, new Long(99), "test"); assertTrue(perms.size() > 0); assertTrue(perms.size() == 1); GenericValue p = (GenericValue) perms.get(0); assertEquals(p.getString("parameter"), "test"); }
@Override public long getCountForCommentsRestrictedByGroup(String groupName) { if (groupName == null) { throw new IllegalArgumentException("You must provide a non null group name."); } EntityCondition condition = new EntityFieldMap( FieldMap.build("level", groupName, "type", ActionConstants.TYPE_COMMENT), EntityOperator.AND); List commentCount = delegator.findByCondition( "ActionCount", condition, EasyList.build("count"), Collections.EMPTY_LIST); if (commentCount != null && commentCount.size() == 1) { GenericValue commentCountGV = (GenericValue) commentCount.get(0); return commentCountGV.getLong("count").longValue(); } else { throw new DataAccessException("Unable to access the count for the Action table"); } }
public Collection getAllIssueTypes() throws Exception { if (subTaskManager.isSubTasksEnabled()) { return constantsManager.getAllIssueTypeObjects(); } else { final ArrayList returnValues = new ArrayList(constantsManager.getRegularIssueTypeObjects()); // Now, since subtasks are disabled we want to make sure we add any subtask issue types that // are already // selected in the custom field and make sure that the sort order is the same as when we call // getAllIssueTypeObjects final List intersection = new ArrayList( CollectionUtils.intersection( constantsManager.getSubTaskIssueTypes(), getCustomField().getAssociatedIssueTypes())); Collections.sort(intersection); for (final Object anIntersection : intersection) { final GenericValue genericValue = (GenericValue) anIntersection; returnValues.add(0, constantsManager.getIssueTypeObject(genericValue.getString("id"))); } return returnValues; } }