View Javadoc

1   /*
2    * Copyright 2012 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.AddProductCommand;
23  import de.smartics.maven.issue.command.CommandArgument;
24  import de.smartics.maven.issue.command.CommandResult.Page;
25  
26  /**
27   * Implementation of the {@link AddProductCommand} for Bugzilla.
28   */
29  public final class BugzillaAddProductCommand extends
30      AbstractCommand<AddProductCommand> implements AddProductCommand
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 = "editproducts.cgi";
51  
52    /**
53     * The title of the expected page.
54     * <p>
55     * The value of this constant is {@value}.
56     * </p>
57     */
58    private static final String EXPECTED_PAGE_TITLE = "Product Created";
59  
60    /**
61     * The title of a page that signals a suspicious action. This title is
62     * expected if for instance the classification is unknown.
63     * <p>
64     * The value of this constant is {@value}.
65     * </p>
66     */
67    private static final String SUSPICIOUS_ACTION = "Suspicious Action";
68  
69    // --- members --------------------------------------------------------------
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    // CHECKSTYLE:OFF
76  
77    /**
78     * Default constructor.
79     *
80     * @param classification the classification for the product.
81     * @param product the product to add the version to.
82     * @param description the description to the product.
83     * @param defaultMilestone the initial default milestone of the product.
84     * @param version the first version of the product.
85     * @param token the token from the previous command to help the browser to
86     *          secure the request.
87     */
88    public BugzillaAddProductCommand(// NOPMD
89        final CommandArgument<AddProductCommand> classification,
90        final CommandArgument<AddProductCommand> product,
91        final CommandArgument<AddProductCommand> description,
92        final CommandArgument<AddProductCommand> defaultMilestone,
93        final CommandArgument<AddProductCommand> version,
94        final CommandArgument<AddProductCommand> token)
95    {
96      super(SERVICE, attachDefaulArguments(classification, product, description,
97          defaultMilestone, version, token));
98    }
99  
100   // ****************************** Inner Classes *****************************
101 
102   // ********************************* Methods ********************************
103 
104   // --- init -----------------------------------------------------------------
105 
106   private static List<CommandArgument<AddProductCommand>> attachDefaulArguments(// NOPMD
107       final CommandArgument<AddProductCommand> classification, // NOPMD
108       final CommandArgument<AddProductCommand> product,
109       final CommandArgument<AddProductCommand> description,
110       final CommandArgument<AddProductCommand> defaultMilestone,
111       final CommandArgument<AddProductCommand> version,
112       final CommandArgument<AddProductCommand> token)
113   {
114     final List<CommandArgument<AddProductCommand>> allArguments =
115         new ArrayList<CommandArgument<AddProductCommand>>(8);
116     final CommandArgument<AddProductCommand> action =
117         CommandArgument.create(AddProductCommand.Parameter.ACTION, "new");
118     allArguments.add(action);
119     allArguments.add(classification);
120     allArguments.add(product);
121     allArguments.add(description);
122     allArguments.add(defaultMilestone);
123     allArguments.add(version);
124     final CommandArgument<AddProductCommand> active =
125         CommandArgument.create(AddProductCommand.Parameter.IS_ACTIVE, "1");
126     allArguments.add(active);
127     allArguments.add(token);
128     return allArguments;
129   }
130 
131   // CHECKSTYLE:ON
132 
133   // --- get&set --------------------------------------------------------------
134 
135   // --- business -------------------------------------------------------------
136 
137   /**
138    * {@inheritDoc}
139    *
140    * @see de.smartics.maven.issue.command.AbstractCommand#checkExpectation(de.smartics.maven.issue.command.CommandResult.Page)
141    */
142   @Override
143   protected Expectation checkExpectation(final Page page)
144   {
145     final boolean expected = EXPECTED_PAGE_TITLE.equals(page.getTitle());
146     final Expectation expectation =
147         new Expectation(expected, EXPECTED_PAGE_TITLE, expected
148             ? EXPECTED_PAGE_TITLE : Expectation.UNKNOWN_FLAG);
149     return expectation;
150   }
151 
152   /**
153    * {@inheritDoc}
154    *
155    * @see de.smartics.maven.issue.command.AbstractCommand#getCommandResultDescription(Page,Expectation)
156    */
157   @Override
158   protected String getCommandResultDescription(final Page page,
159       final Expectation expectation)
160   {
161     final CommandArgument<AddProductCommand> productArg =
162         getArgument(AddProductCommand.Parameter.PRODUCT);
163     final String expectationFlag = expectation.getFlag();
164     if (EXPECTED_PAGE_TITLE.equals(expectationFlag))
165     {
166       return "Successfully added product '" + productArg.getValue() + "'.";
167     }
168     else if (SUSPICIOUS_ACTION.equals(page.getTitle()))
169     {
170       return "Failed to add product '" + productArg.getValue()
171              + "'. Maybe the classification is unknown?"
172              + " Please check parameters below.";
173     }
174     else
175     {
176       return "Failed to add product '" + productArg.getValue() + "'.";
177     }
178   }
179 
180   // --- object basics --------------------------------------------------------
181 
182 }