View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.bugzilla.command;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import de.smartics.maven.issue.command.AbstractCommand;
22  import de.smartics.maven.issue.command.AddMilestoneCommand;
23  import de.smartics.maven.issue.command.CommandArgument;
24  import de.smartics.maven.issue.command.CommandResult.Page;
25  
26  /**
27   * Implementation of the {@link AddMilestoneCommand} for Bugzilla.
28   */
29  public final class BugzillaAddMilestoneCommand extends
30      AbstractCommand<AddMilestoneCommand> implements AddMilestoneCommand
31  {
32    // ********************************* Fields *********************************
33  
34    // --- constants ------------------------------------------------------------
35  
36    /**
37     * The class version identifier.
38     * <p>
39     * The value of this constant is {@value}.
40     * </p>
41     */
42    private static final long serialVersionUID = 1L;
43  
44    /**
45     * The service on the command target to call.
46     * <p>
47     * The value of this constant is {@value}.
48     * </p>
49     */
50    private static final String SERVICE = "editmilestones.cgi";
51  
52    /**
53     * The title of the expected page if the milestone has been created.
54     * <p>
55     * The value of this constant is {@value}.
56     * </p>
57     */
58    private static final String EXPECTED_PAGE_TITLE_CREATED = "Milestone Created";
59  
60    /**
61     * The title of the expected page if the milestone already existed.
62     * <p>
63     * The value of this constant is {@value}.
64     * </p>
65     */
66    private static final String EXPECTED_PAGE_TITLE_EXISTS =
67        "Milestone Already Exists";
68  
69    // --- members --------------------------------------------------------------
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    /**
76     * Default constructor.
77     *
78     * @param product the product to add the version to.
79     * @param milestone the milestone to add.
80     * @param sortkey the sort key to the milestone.
81     * @param token the token from the previous command to help the browser to
82     *          secure the request.
83     */
84    public BugzillaAddMilestoneCommand(
85        // NOPMD
86        final CommandArgument<AddMilestoneCommand> product,
87        final CommandArgument<AddMilestoneCommand> milestone,
88        final CommandArgument<AddMilestoneCommand> sortkey,
89        final CommandArgument<AddMilestoneCommand> token)
90    {
91      super(SERVICE, attachDefaulArguments(product, milestone, sortkey, token));
92    }
93  
94    // ****************************** Inner Classes *****************************
95  
96    // ********************************* Methods ********************************
97  
98    // --- init -----------------------------------------------------------------
99  
100   private static List<CommandArgument<AddMilestoneCommand>> attachDefaulArguments(
101       final CommandArgument<AddMilestoneCommand> product,
102       final CommandArgument<AddMilestoneCommand> milestone,
103       final CommandArgument<AddMilestoneCommand> sortkey,
104       final CommandArgument<AddMilestoneCommand> token)
105   {
106     final List<CommandArgument<AddMilestoneCommand>> allArguments =
107         new ArrayList<CommandArgument<AddMilestoneCommand>>(3);
108     final CommandArgument<AddMilestoneCommand> action =
109         CommandArgument.create(AddMilestoneCommand.Parameter.ACTION, "new");
110     allArguments.add(action);
111     allArguments.add(product);
112     allArguments.add(milestone);
113     if (sortkey.hasValue())
114     {
115       allArguments.add(sortkey);
116     }
117     allArguments.add(token);
118     return allArguments;
119   }
120 
121   // --- get&set --------------------------------------------------------------
122 
123   // --- business -------------------------------------------------------------
124 
125   /**
126    * {@inheritDoc}
127    *
128    * @see de.smartics.maven.issue.command.AbstractCommand#checkExpectation(de.smartics.maven.issue.command.CommandResult.Page)
129    */
130   @Override
131   protected Expectation checkExpectation(final Page page)
132   {
133     final Expectation expectation =
134         BugzillaCommandUtils.createExpectation(page,
135             EXPECTED_PAGE_TITLE_CREATED, EXPECTED_PAGE_TITLE_EXISTS);
136     return expectation;
137   }
138 
139   /**
140    * {@inheritDoc}
141    *
142    * @see de.smartics.maven.issue.command.AbstractCommand#getCommandResultDescription(Page,Expectation)
143    */
144   @Override
145   protected String getCommandResultDescription(final Page page,
146       final Expectation expectation)
147   {
148     final CommandArgument<AddMilestoneCommand> milestoneArg =
149         getArgument(AddMilestoneCommand.Parameter.MILESTONE);
150     final String expectationFlag = expectation.getFlag();
151 
152     final String messageSuffix;
153     if (EXPECTED_PAGE_TITLE_CREATED.equals(expectationFlag))
154     {
155       messageSuffix = "has been created.";
156     }
157     else if (EXPECTED_PAGE_TITLE_EXISTS.equals(expectationFlag))
158     {
159       messageSuffix = "already existed.";
160     }
161     else
162     {
163       messageSuffix = "failed to be created.";
164     }
165 
166     return "Milestone '" + milestoneArg.getValue() + "' " + messageSuffix;
167   }
168 
169   // --- object basics --------------------------------------------------------
170 
171 }