It's all about the answers!

Ask a question

how to test if a work item approval has been deleted during a work item save operation?


Krasimir Malchev (56631) | asked Oct 10 '16, 8:47 a.m.
edited Oct 10 '16, 9:56 a.m.
Hello,
  I've written a simple operation advisor (precondition) which is executed in response to a work item save operation.
  The advisor has to verify if a work item approval has been removed prior to saving the work item.
  For that purpose the advisor gets the old and the new state (of type IWorkItem) of the work item, fetches their approvals and the corresponding approval descriptors, and verifies if all approval descriptors of the old state are present in the new state. If one of the old state approvals is missing in the new state (verified by the descriptors' equals method) the advisor concludes that an approval has been deleted.
 Another test case is: Instead of deleting the existing approval, the tester creates a new one (along with the existing approval).

  In order to make a test I created a story work item with an approval of type "review".
  Then another approval is created and the work item "Save" button is clicked.
  As a result:

  - the list with approvals fetched from the old and the new states of the work item are different objects.
  - the list with approval descriptors, which are fetched from the old and the new states of the work item are different objects.
   - The corresponding IAapproval and IApprovalDescriptor objects are also different (although only a new approval is created and the existing one is not changed. I'd expect that at least the descriptor of the first approval is the same object in both states).
   - There is no unique approval descriptor ID which can be used to compare old and new state approvals.

Is there a way to test if an approval has been deleted prior to saving the work item?

Your help would be really appreciated!

Source code
Environment: RTC 5.0.2
    public void run (AdvisableOperation operation,
                              IProcessConfigurationElement advisorConfig,
                              IAdvisorInfoCollector collector,
                              IProgressMonitor monitor)
            throws TeamRepositoryException {
        workItemServer = getService (IWorkItemServer.class);

        Object data = operation.getOperationData();
        ISaveParameter saveParameter = null;
        if (data instanceof ISaveParameter) {
            saveParameter = (ISaveParameter) data;

            IAuditable oldState = saveParameter.getOldState();
            // test for newly created work item
            if ( (oldState == null)
                 || !(oldState instanceof IWorkItem)) {
                return;
            }

            IWorkItem oldStateFull = (IWorkItem)
                      workItemServer
                      .getAuditableCommon()
                      .resolveAuditable ((IWorkItem) oldState,
                                                    IWorkItem.FULL_PROFILE, null);
            IApprovals oldApprovals = oldStateFull.getApprovals();

            IAuditable newState = saveParameter.getNewState();
            if ( (newState == null)
                 || !(newState instanceof IWorkItem)) {
                return;
            }
            IWorkItem newStateFull = (IWorkItem)
                      workItemServer
                      .getAuditableCommon()
                      .resolveAuditable ((IWorkItem) newState,
                                                    IWorkItem.FULL_PROFILE,
                                                    null);
            IApprovals newApprovals = newStateFull.getApprovals();

            List<IApprovalDescriptor> oldDescrList =
                                                       oldApprovals.getDescriptors();
            List<IApprovalDescriptor> newDescrList =
                                                      newApprovals.getDescriptors();

            if ( oldDescrList.size() > 0 ) {
                final Iterator<IApprovalDescriptor> itOldList =
                                                         oldDescrList.iterator();
                while ( itOldList.hasNext() ) {
                    IApprovalDescriptor oldDescr = itOldList.next();
                    System.out.println("Print old descriptor to compare");
                    printDescriptorInfo( oldDescr, null );
                    final Iterator<IApprovalDescriptor> itNewList =
                                                                   newDescrList.iterator();
                    boolean found =false;
                    while (itNewList.hasNext()) {
                        IApprovalDescriptor newDescr =
                                                           itNewList.next();
                        System.out.println("Print new descriptor to compare");
                        printDescriptorInfo ( newDescr, null);
                        if (oldDescr.equals(newDescr) ||
                           (oldDescr == newDescr) ) {
                            found = true;
                            break;
                        }//if
                    }//while

                    if( !found ) {
                        String[] values = { String.valueOf (oldStateFull.getId()),
                                            oldDescr.getName(),
                                            oldDescr.getTypeIdentifier() };
                        String description = NLS.bind (
                                "Work item with id ''{0}'' cannot be saved as the approval with name ''{1}'' of type ''{2}'' has been deleted.",
                                values);
                            
                        IAdvisorInfo error =
                                 collector
                                .createProblemInfo ("Unable to save the work item",
                                                                description,
                                                                "Error");
                        collector.addInfo (error);
                        break;
                    } else {
                        System.out.println ("Approval "
                                                       + oldDescr.getName()
                                                       + "is found in the new work item state");
                    }//else
                }//while
            } else {
               System.out.println (
                          "No approval descriptors in old state");
            }
        }//if saveParameter
    }  


Best Regards
 

Accepted answer


permanent link
Ralph Schoon (63.4k33646) | answered Oct 10 '16, 10:35 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER
edited Oct 10 '16, 10:36 a.m.
As far as I can tell, the only thing you can do is to use the number of approvals, the type and the other available values to determine if the old and the new approval is the same. There is no UUID for any of these interfaces that I would be aware of that could otherwise be used.
Krasimir Malchev selected this answer as the correct answer

Comments
Krasimir Malchev commented Oct 10 '16, 10:39 a.m.

Thanks a lot!


Ralph Schoon commented Oct 10 '16, 10:41 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

I have wondered over this also at some point in time. If you want this to change please create an enhancement request.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.