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.AddComponentCommand;
23  import de.smartics.maven.issue.command.CommandArgument;
24  import de.smartics.maven.issue.command.CommandResult.Page;
25  
26  /**
27   * Implementation of the {@link AddComponentCommand} for Bugzilla.
28   */
29  public final class BugzillaAddComponentCommand extends
30      AbstractCommand<AddComponentCommand> implements AddComponentCommand
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 = "editcomponents.cgi";
51  
52    /**
53     * The title of the expected page if the component 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 = "Component Created";
59  
60    /**
61     * The title of the expected page if the component already exists.
62     * <p>
63     * The value of this constant is {@value}.
64     * </p>
65     */
66    private static final String EXPECTED_PAGE_TITLE_EXISTS =
67        "Component Already Exists";
68  
69    // --- members --------------------------------------------------------------
70  
71    // ****************************** Initializer *******************************
72  
73    // ****************************** Constructors ******************************
74  
75    // CHECKSTYLE:OFF
76    /**
77     * Default constructor.
78     *
79     * @param product the product to component belongs to.
80     * @param component the name of the component for the product.
81     * @param description the description to the component.
82     * @param initialOwner the name of the inital owner of issues.
83     * @param token the token from the previous command to help the browser to
84     *          secure the request.
85     */
86    public BugzillaAddComponentCommand(
87        // NOPMD
88        final CommandArgument<AddComponentCommand> product,
89        final CommandArgument<AddComponentCommand> component,
90        final CommandArgument<AddComponentCommand> description,
91        final CommandArgument<AddComponentCommand> initialOwner,
92        final CommandArgument<AddComponentCommand> token)
93    {
94      super(SERVICE, attachDefaulArguments(product, component, description,
95          initialOwner, token));
96    }
97  
98    // CHECKSTYLE:ON
99  
100   // ****************************** Inner Classes *****************************
101 
102   // ********************************* Methods ********************************
103 
104   // --- init -----------------------------------------------------------------
105 
106   private static List<CommandArgument<AddComponentCommand>> attachDefaulArguments(
107       final CommandArgument<AddComponentCommand> product,
108       final CommandArgument<AddComponentCommand> component,
109       final CommandArgument<AddComponentCommand> description,
110       final CommandArgument<AddComponentCommand> initialOwner,
111       final CommandArgument<AddComponentCommand> token)
112   {
113     final List<CommandArgument<AddComponentCommand>> allArguments =
114         new ArrayList<CommandArgument<AddComponentCommand>>(3);
115     final CommandArgument<AddComponentCommand> action =
116         CommandArgument.create(AddComponentCommand.Parameter.ACTION, "new");
117     allArguments.add(action);
118     allArguments.add(product);
119     allArguments.add(component);
120     allArguments.add(description);
121     allArguments.add(initialOwner);
122     allArguments.add(token);
123     return allArguments;
124   }
125 
126   // --- get&set --------------------------------------------------------------
127 
128   // --- business -------------------------------------------------------------
129 
130   /**
131    * {@inheritDoc}
132    *
133    * @see de.smartics.maven.issue.command.AbstractCommand#checkExpectation(de.smartics.maven.issue.command.CommandResult.Page)
134    */
135   @Override
136   protected Expectation checkExpectation(final Page page)
137   {
138     final Expectation expectation =
139         BugzillaCommandUtils.createExpectation(page,
140             EXPECTED_PAGE_TITLE_CREATED, EXPECTED_PAGE_TITLE_EXISTS);
141     return expectation;
142   }
143 
144   /**
145    * {@inheritDoc}
146    *
147    * @see de.smartics.maven.issue.command.AbstractCommand#getCommandResultDescription(Page,Expectation)
148    */
149   @Override
150   protected String getCommandResultDescription(final Page page,
151       final Expectation expectation)
152   {
153     final CommandArgument<AddComponentCommand> componentArg =
154         getArgument(AddComponentCommand.Parameter.COMPONENT);
155     final String expectationFlag = expectation.getFlag();
156 
157     final String messageSuffix;
158     if (EXPECTED_PAGE_TITLE_CREATED.equals(expectationFlag))
159     {
160       messageSuffix = "has been created.";
161     }
162     else if (EXPECTED_PAGE_TITLE_EXISTS.equals(expectationFlag))
163     {
164       messageSuffix = "already existed.";
165     }
166     else
167     {
168       messageSuffix = "failed to be created.";
169     }
170 
171     return "Component '" + componentArg.getValue() + "' " + messageSuffix;
172   }
173 
174   // --- object basics --------------------------------------------------------
175 
176 }