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.AddVersionCommand;
23  import de.smartics.maven.issue.command.CommandArgument;
24  import de.smartics.maven.issue.command.CommandResult.Page;
25  
26  /**
27   * Implementation of the {@link AddVersionCommand} for Bugzilla.
28   */
29  public final class BugzillaAddVersionCommand extends
30      AbstractCommand<AddVersionCommand> implements AddVersionCommand
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 = "editversions.cgi";
51  
52    /**
53     * The title of the expected page if the version 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 = "Version Created";
59  
60    /**
61     * The title of the expected page if the version 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        "Version 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 version the version to add.
80     * @param token the token from the previous command to help the browser to
81     *          secure the request.
82     */
83    public BugzillaAddVersionCommand(
84        final CommandArgument<AddVersionCommand> product,
85        final CommandArgument<AddVersionCommand> version,
86        final CommandArgument<AddVersionCommand> token)
87    {
88      super(SERVICE, attachDefaulArguments(product, version, token));
89    }
90  
91    // ****************************** Inner Classes *****************************
92  
93    // ********************************* Methods ********************************
94  
95    // --- init -----------------------------------------------------------------
96  
97    private static List<CommandArgument<AddVersionCommand>> attachDefaulArguments(
98        final CommandArgument<AddVersionCommand> product,
99        final CommandArgument<AddVersionCommand> version,
100       final CommandArgument<AddVersionCommand> token)
101   {
102     final List<CommandArgument<AddVersionCommand>> allArguments =
103         new ArrayList<CommandArgument<AddVersionCommand>>(3);
104     final CommandArgument<AddVersionCommand> action =
105         CommandArgument.create(AddVersionCommand.Parameter.ACTION, "new");
106     allArguments.add(action);
107     allArguments.add(product);
108     allArguments.add(version);
109     allArguments.add(token);
110     return allArguments;
111   }
112 
113   // --- get&set --------------------------------------------------------------
114 
115   // --- business -------------------------------------------------------------
116 
117   /**
118    * {@inheritDoc}
119    *
120    * @see de.smartics.maven.issue.command.AbstractCommand#checkExpectation(de.smartics.maven.issue.command.CommandResult.Page)
121    */
122   @Override
123   protected Expectation checkExpectation(final Page page)
124   {
125     final Expectation expectation =
126         BugzillaCommandUtils.createExpectation(page,
127             EXPECTED_PAGE_TITLE_CREATED, EXPECTED_PAGE_TITLE_EXISTS);
128     return expectation;
129   }
130 
131   /**
132    * {@inheritDoc}
133    *
134    * @see de.smartics.maven.issue.command.AbstractCommand#getCommandResultDescription(Page,Expectation)
135    */
136   @Override
137   protected String getCommandResultDescription(final Page page,
138       final Expectation expectation)
139   {
140     final CommandArgument<AddVersionCommand> versionArg =
141         getArgument(AddVersionCommand.Parameter.VERSION);
142     final String expectationFlag = expectation.getFlag();
143 
144     final String messageSuffix;
145     if (EXPECTED_PAGE_TITLE_CREATED.equals(expectationFlag))
146     {
147       messageSuffix = "has been created.";
148     }
149     else if (EXPECTED_PAGE_TITLE_EXISTS.equals(expectationFlag))
150     {
151       messageSuffix = "already existed.";
152     }
153     else
154     {
155       messageSuffix = "failed to be created.";
156     }
157 
158     return "Version '" + versionArg.getValue() + "' " + messageSuffix;
159   }
160 
161   // --- object basics --------------------------------------------------------
162 
163 }