/*
* The MIT License
*
* Copyright 2011 Sony Ericsson Mobile Communications. All rights reserved.
* Copyright 2012 Sony Mobile Communications AB. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.sonyericsson.hudson.plugins.gerrit.trigger.spec;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.sonymobile.tools.gerrit.gerritevents.GerritConnection;
import com.sonymobile.tools.gerrit.gerritevents.GerritEventListener;
import com.sonymobile.tools.gerrit.gerritevents.GerritHandler;
import com.sonyericsson.hudson.plugins.gerrit.trigger.GerritServer;
import com.sonyericsson.hudson.plugins.gerrit.trigger.PluginImpl;
import com.sonyericsson.hudson.plugins.gerrit.trigger.config.Config;
import com.sonymobile.tools.gerrit.gerritevents.mock.SshdServerMock;
import hudson.model.Item;
import hudson.model.FreeStyleProject;
import org.junit.Before;
import org.junit.Rule;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;
import org.powermock.reflect.Whitebox;
import java.util.Collection;
import java.util.List;
import static com.sonyericsson.hudson.plugins.gerrit.trigger.mock.DuplicatesUtil.createGerritTriggeredJob;
import static com.sonyericsson.hudson.plugins.gerrit.trigger.mock.TestUtils.getFormWithAction;
//CS IGNORE AvoidStarImport FOR NEXT 1 LINES. REASON: UnitTest.
import static org.junit.Assert.*;
//CS IGNORE MagicNumber FOR NEXT 200 LINES. REASON: Test data.
/**
* This tests different scenarios of adding listeners to the
* {@link com.sonymobile.tools.gerrit.gerritevents.GerritHandler}
* to make sure that no duplicates are created.
*
* @author Robert Sandell <robert.sandell@sonyericsson.com>
*/
public class DuplicateGerritListenersHudsonTestCase {
/**
* An instance of Jenkins Rule.
*/
// CS IGNORE VisibilityModifier FOR NEXT 2 LINES. REASON: JenkinsRule.
@Rule
public final JenkinsRule j = new JenkinsRule();
private SshdServerMock.KeyPairFiles keyFile;
/**
* Runs before test method.
*
* @throws Exception throw if so.
*/
@Before
public void setUp() throws Exception {
keyFile = SshdServerMock.generateKeyPair();
}
/**
* Tests creating a new project.
*
* @throws Exception if so.
*/
@LocalData
public void testNewProjectCreation() throws Exception {
createGerritTriggeredJob(j, "testJob1");
GerritHandler handler = Whitebox.getInternalState(PluginImpl.getInstance().
getServer(PluginImpl.DEFAULT_SERVER_NAME), GerritHandler.class);
Collection<GerritEventListener> gerritEventListeners =
Whitebox.getInternalState(handler, "gerritEventListeners");
// DependencyQueueTaskDispatcher adds 1 listener
// ReplicationQueueTaskDispatcher adds 1 listener
assertEquals(3, gerritEventListeners.size());
}
/**
* Tests resaving a project.
*
* @throws Exception if so.
*/
@LocalData
public void testNewProjectCreationWithReSave() throws Exception {
FreeStyleProject p = createGerritTriggeredJob(j, "testJob2");
j.configRoundtrip((Item)p);
GerritHandler handler = Whitebox.getInternalState(PluginImpl.getInstance().
getServer(PluginImpl.DEFAULT_SERVER_NAME), GerritHandler.class);
Collection<GerritEventListener> gerritEventListeners =
Whitebox.getInternalState(handler, "gerritEventListeners");
// DependencyQueueTaskDispatcher adds 1 listener
// ReplicationQueueTaskDispatcher adds 1 listener
assertEquals(3, gerritEventListeners.size());
}
/**
* Tests correct addListener behaviour when a project is renamed.
*
* @throws Exception id so.
*/
@LocalData
public void testNewProjectCreationWithReName() throws Exception {
FreeStyleProject p = createGerritTriggeredJob(j, "testJob3");
HtmlForm form = j.createWebClient().getPage(p, "configure").getFormByName("config");
form.getInputByName("name").setValueAttribute("testJob33");
HtmlPage confirmPage = j.submit(form);
j.submit(getFormWithAction("doRename", confirmPage.getForms()));
//configRoundtrip(p);
assertEquals("testJob33", p.getName());
GerritHandler handler = Whitebox.getInternalState(PluginImpl.getInstance().
getServer(PluginImpl.DEFAULT_SERVER_NAME), GerritHandler.class);
Collection<GerritEventListener> gerritEventListeners =
Whitebox.getInternalState(handler, "gerritEventListeners");
// DependencyQueueTaskDispatcher adds 1 listener
// ReplicationQueueTaskDispatcher adds 1 listener
assertEquals(3, gerritEventListeners.size());
}
/**
* Tests that the listeners are added correctly to the handler when a connection is established for the first time.
*
* @throws Exception if so.
*/
public void testNewProjectCreationFirstNoConnection() throws Exception {
@SuppressWarnings("unused")
List<GerritServer> servers = PluginImpl.getInstance().getServers();
//create a server for testing
GerritServer server = new GerritServer(PluginImpl.DEFAULT_SERVER_NAME);
servers.add(server);
server.start();
createGerritTriggeredJob(j, "testJob4");
GerritHandler handler = Whitebox.getInternalState(server, GerritHandler.class);
assertNotNull(handler);
GerritConnection connection = Whitebox.getInternalState(server, GerritConnection.class);
assertNull(connection);
Collection<GerritEventListener> savedEventListeners =
Whitebox.getInternalState(handler, "gerritEventListeners");
// DependencyQueueTaskDispatcher adds 1 listener
// ReplicationQueueTaskDispatcher adds 1 listener
assertEquals(3, savedEventListeners.size());
Config config = (Config)server.getConfig();
config.setGerritAuthKeyFile(keyFile.getPublicKey());
config.setGerritHostName("localhost");
config.setGerritFrontEndURL("http://localhost");
config.setGerritSshPort(29418);
config.setGerritProxy("");
server.startConnection();
handler = Whitebox.getInternalState(server, GerritHandler.class);
Collection<GerritEventListener> gerritEventListeners =
Whitebox.getInternalState(handler, "gerritEventListeners");
// DependencyQueueTaskDispatcher adds 1 listener
// ReplicationQueueTaskDispatcher adds 1 listener
assertEquals(3, gerritEventListeners.size());
connection = Whitebox.getInternalState(server, GerritConnection.class);
assertNotNull(connection);
}
}