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.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     * @param component the component to add on product creation.
88     * @param componentDescription the description to the component.
89     * @param componentInitialOwner the initial owner of the component.
90     */
91    public BugzillaAddProductCommand(
92        // NOPMD
93        final CommandArgument<AddProductCommand> classification,
94        final CommandArgument<AddProductCommand> product,
95        final CommandArgument<AddProductCommand> description,
96        final CommandArgument<AddProductCommand> defaultMilestone,
97        final CommandArgument<AddProductCommand> version,
98        final CommandArgument<AddProductCommand> token,
99        final CommandArgument<AddProductCommand> component,
100       final CommandArgument<AddProductCommand> componentDescription,
101       final CommandArgument<AddProductCommand> componentInitialOwner)
102   {
103     super(SERVICE, attachDefaulArguments(classification, product, description,
104         defaultMilestone, version, token, component, componentDescription,
105         componentInitialOwner));
106   }
107 
108   // ****************************** Inner Classes *****************************
109 
110   // ********************************* Methods ********************************
111 
112   // --- init -----------------------------------------------------------------
113 
114   private static List<CommandArgument<AddProductCommand>> attachDefaulArguments(
115       // NOPMD
116       final CommandArgument<AddProductCommand> classification, // NOPMD
117       final CommandArgument<AddProductCommand> product,
118       final CommandArgument<AddProductCommand> description,
119       final CommandArgument<AddProductCommand> defaultMilestone,
120       final CommandArgument<AddProductCommand> version,
121       final CommandArgument<AddProductCommand> token,
122       final CommandArgument<AddProductCommand> component,
123       final CommandArgument<AddProductCommand> componentDescription,
124       final CommandArgument<AddProductCommand> componentInitialOwner)
125   {
126     final List<CommandArgument<AddProductCommand>> allArguments =
127         new ArrayList<CommandArgument<AddProductCommand>>(8);
128     final CommandArgument<AddProductCommand> action =
129         CommandArgument.create(AddProductCommand.Parameter.ACTION, "new");
130     allArguments.add(action);
131     allArguments.add(classification);
132     allArguments.add(product);
133     allArguments.add(description);
134     allArguments.add(defaultMilestone);
135     allArguments.add(version);
136     final CommandArgument<AddProductCommand> active =
137         CommandArgument.create(AddProductCommand.Parameter.IS_ACTIVE, "1");
138     allArguments.add(active);
139     allArguments.add(token);
140     if (component != null)
141     {
142       allArguments.add(component);
143     }
144     if (componentDescription != null)
145     {
146       allArguments.add(componentDescription);
147     }
148     if (componentInitialOwner != null)
149     {
150       allArguments.add(componentInitialOwner);
151     }
152     return allArguments;
153   }
154 
155   // CHECKSTYLE:ON
156 
157   // --- get&set --------------------------------------------------------------
158 
159   // --- business -------------------------------------------------------------
160 
161   /**
162    * {@inheritDoc}
163    *
164    * @see de.smartics.maven.issue.command.AbstractCommand#checkExpectation(de.smartics.maven.issue.command.CommandResult.Page)
165    */
166   @Override
167   protected Expectation checkExpectation(final Page page)
168   {
169     final boolean expected = EXPECTED_PAGE_TITLE.equals(page.getTitle());
170     final Expectation expectation =
171         new Expectation(expected, EXPECTED_PAGE_TITLE, expected
172             ? EXPECTED_PAGE_TITLE : Expectation.UNKNOWN_FLAG);
173     return expectation;
174   }
175 
176   /**
177    * {@inheritDoc}
178    *
179    * @see de.smartics.maven.issue.command.AbstractCommand#getCommandResultDescription(Page,Expectation)
180    */
181   @Override
182   protected String getCommandResultDescription(final Page page,
183       final Expectation expectation)
184   {
185     final CommandArgument<AddProductCommand> productArg =
186         getArgument(AddProductCommand.Parameter.PRODUCT);
187     final String expectationFlag = expectation.getFlag();
188     if (EXPECTED_PAGE_TITLE.equals(expectationFlag))
189     {
190       return "Successfully added product '" + productArg.getValue() + "'.";
191     }
192     else if (SUSPICIOUS_ACTION.equals(page.getTitle()))
193     {
194       return "Failed to add product '" + productArg.getValue()
195              + "'. Maybe the classification is unknown?"
196              + " Please check parameters below.";
197     }
198     else
199     {
200       return "Failed to add product '" + productArg.getValue() + "'.";
201     }
202   }
203 
204   // --- object basics --------------------------------------------------------
205 
206 }