Skip to content
Permalink
Browse files

[JENKINS-45748] - Take into account organization base and test coveri…

…ng it. (#9)
  • Loading branch information
alvarolobato authored and i386 committed Jul 25, 2017
1 parent ded217e commit cf3d09bd2be0abac78319346e8b9cade0a99aa20
@@ -20,7 +20,7 @@
<java.level>7</java.level>
<!-- Jenkins Test Harness version you use to test the plugin. -->
<!-- For Jenkins version >= 1.580.1 use JTH 2.x or higher. -->
<jenkins-test-harness.version>2.13</jenkins-test-harness.version>
<jenkins-test-harness.version>2.23</jenkins-test-harness.version>
<!-- Other properties you may want to use:
~ hpi-plugin.version: The HPI Maven Plugin version used by the plugin..
~ stapler-plugin.version: The Stapler Maven plugin version required by the plugin.
@@ -2,12 +2,15 @@

import com.google.common.collect.ImmutableSet;
import hudson.Extension;
import hudson.Functions;
import hudson.Util;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Run;
import io.jenkins.blueocean.rest.factory.organization.AbstractOrganization;
import io.jenkins.blueocean.rest.factory.organization.OrganizationFactory;
import io.jenkins.blueocean.rest.model.BlueOrganization;
import jenkins.branch.MultiBranchProject;
@@ -18,6 +21,8 @@

import java.util.Set;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
*`@author Ivan Meredith
@@ -91,7 +96,7 @@ public String getJobURL(Job<?, ?> job) {
}

private String getJobURL(BlueOrganization organization, Job<?, ?> job) {
String jobPath = job.getParent() instanceof MultiBranchProject ? job.getParent().getFullName() : job.getFullName();
String jobPath = job.getParent() instanceof MultiBranchProject ? getFullName(organization, job.getParent()) : getFullName(organization, job);
return String.format("%sorganizations/%s/%s/", getRoot(), Util.rawEncode(organization.getName()), Util.rawEncode(jobPath));
}

@@ -113,6 +118,48 @@ private static boolean isInstance(Object o, Set<String> clazzes) {
}

private String getJobURL(BlueOrganization organization, MultiBranchProject<?, ?> project) {
return String.format("%sorganizations/%s/%s/", getRoot(), Util.rawEncode(organization.getName()), Util.rawEncode(project.getFullName()));
return String.format("%sorganizations/%s/%s/", getRoot(), Util.rawEncode(organization.getName()), Util.rawEncode(getFullName(organization, (Item) project)));
}

/**
* Returns full name relative to the <code>BlueOrganization</code> base. Each name is separated by '/'
*
* @param org the organization the item belongs to
* @param item to return the full name of
* @return
*/
private static String getFullName(@Nullable BlueOrganization org, @Nonnull Item item) {
ItemGroup<?> group = getBaseGroup(org);
return Functions.getRelativeNameFrom(item, group);
}

/**
* Returns full name relative to the <code>BlueOrganization</code> base. Each name is separated by '/'
*
* @param org the organization the item belongs to
* @param itemGroup to return the full name of
* @return
*/
private static String getFullName(@Nullable BlueOrganization org, @Nonnull ItemGroup itemGroup) {
if (itemGroup instanceof Item) {
return getFullName(org, itemGroup);
} else {
return itemGroup.getFullName();
}
}

/**
* Tries to obtain the base group for a <code>BlueOrganization</code>
*
* @param org to get the base group of
* @return the base group
*/
private static ItemGroup<?> getBaseGroup(BlueOrganization org) {
ItemGroup<?> group = null;
if (org != null && org instanceof AbstractOrganization) {
group = ((AbstractOrganization) org).getGroup();
}
return group;
}

}
@@ -1,15 +1,21 @@
package org.jenkinsci.plugins.blueoceandisplayurl;

import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import hudson.model.FreeStyleProject;
import hudson.model.Project;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.scm.api.SCMSource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;

import org.acegisecurity.AccessDeniedException;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
@@ -19,14 +25,47 @@
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockFolder;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.verb.DELETE;
import org.springframework.util.CollectionUtils;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.FreeStyleProject;
import hudson.model.ItemGroup;
import hudson.model.Project;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import hudson.model.User;
import io.jenkins.blueocean.commons.ServiceException;
import io.jenkins.blueocean.rest.ApiHead;
import io.jenkins.blueocean.rest.factory.organization.AbstractOrganization;
import io.jenkins.blueocean.rest.factory.organization.OrganizationFactory;
import io.jenkins.blueocean.rest.hal.Link;
import io.jenkins.blueocean.rest.model.BlueOrganization;
import io.jenkins.blueocean.rest.model.BluePipelineContainer;
import io.jenkins.blueocean.rest.model.BlueUser;
import io.jenkins.blueocean.rest.model.BlueUserContainer;
import io.jenkins.blueocean.service.embedded.OrganizationFactoryImpl;
import io.jenkins.blueocean.service.embedded.rest.OrganizationImpl;
import io.jenkins.blueocean.service.embedded.rest.PipelineContainerImpl;
import io.jenkins.blueocean.service.embedded.rest.UserContainerImpl;
import io.jenkins.blueocean.service.embedded.rest.UserImpl;
import jenkins.branch.BranchProperty;
import jenkins.branch.BranchSource;
import jenkins.branch.DefaultBranchPropertyStrategy;
import jenkins.model.Jenkins;
import jenkins.model.ModifiableTopLevelItemGroup;
import jenkins.plugins.git.GitSCMSource;
import jenkins.plugins.git.GitSampleRepoRule;
import jenkins.scm.api.SCMSource;

/**
* @author Ivan Meredith
@@ -63,6 +102,13 @@ public void testProjectURL() throws Exception {
Assert.assertEquals("/jenkins/blue/organizations/jenkins/abc/", url);
}

@Test
public void testProjectURL_CustomOrganization() throws Exception {
FreeStyleProject p = orgFolder.createProject(FreeStyleProject.class, "abc");
String url = getPath(displayURL.getJobURL(p));
Assert.assertEquals("/jenkins/blue/organizations/TestOrg/abc/", url);
}

@Test
public void testProjectInFolder() throws Exception {
MockFolder folder = j.createFolder("test");
@@ -80,6 +126,23 @@ public void testProjectInFolder() throws Exception {

}

@Test
public void testProjectInFolder_CustomOrganization() throws Exception {
Folder folder = orgFolder.createProject(Folder.class, "test");
Project p = folder.createProject(FreeStyleProject.class, "abc");
String url = getPath(displayURL.getJobURL(p));
Assert.assertEquals("/jenkins/blue/organizations/TestOrg/test%2Fabc/", url);

p.scheduleBuild2(0).waitForStart();

url = getPath(displayURL.getRunURL(p.getLastBuild()));
Assert.assertEquals("/jenkins/blue/organizations/TestOrg/test%2Fabc/detail/abc/1/", url);

url = getPath(displayURL.getChangesURL(p.getLastBuild()));
Assert.assertEquals("/jenkins/blue/organizations/TestOrg/test%2Fabc/detail/abc/1/changes", url);

}

@Test
public void testMultibranchUrls() throws Exception {
repo.checkoutNewBranch("feature/test-1")
@@ -99,9 +162,31 @@ public void testMultibranchUrls() throws Exception {
Assert.assertEquals("/jenkins/blue/organizations/jenkins/folder%2Ftest/detail/feature%2Ftest-1/1/changes", url);
}

@Test
public void testMultibranchUrls_CustomOrganization() throws Exception {
repo.checkoutNewBranch("feature/test-1")
.writeJenkinsFile(JenkinsFile.createFile().node().stage("stage1").echo("test").endNode())
.addFile("Jenkinsfile")
.commit("Initial commit to feature/test-1");

MultiBranchTestBuilder mp = MultiBranchTestBuilder.createProjectInFolder(j, "folder", "test", gitSampleRepoRule, orgFolder);

WorkflowJob job = mp.scheduleAndFindBranchProject("feature%2Ftest-1");

String url = getPath(displayURL.getRunURL(job.getFirstBuild()));

Assert.assertEquals("/jenkins/blue/organizations/TestOrg/folder%2Ftest/detail/feature%2Ftest-1/1/", url);

url = getPath(displayURL.getChangesURL(job.getFirstBuild()));
Assert.assertEquals("/jenkins/blue/organizations/TestOrg/folder%2Ftest/detail/feature%2Ftest-1/1/changes", url);
}

MockFolder orgFolder;
@Before
public void setUp() {
public void setUp() throws IOException {
displayURL = Iterables.find(DisplayURLProvider.all(), Predicates.instanceOf(BlueOceanDisplayURLImpl.class));
orgFolder = j.createFolder("TestOrgFolderName");
orgFolder.setDisplayName("TestOrgFolderName Display Name");
}

public static class MultiBranchTestBuilder{
@@ -112,9 +197,18 @@ public MultiBranchTestBuilder(JenkinsRule j, WorkflowMultiBranchProject mp) {
this.j = j;
}


public static MultiBranchTestBuilder createProjectInFolder(JenkinsRule j, String folderName, String name, GitSampleRepoRule gitRepo) throws IOException {
MockFolder folder = j.createFolder(folderName);
return createProjectInFolder(j, folderName, name, gitRepo, null);
}

public static MultiBranchTestBuilder createProjectInFolder(JenkinsRule j, String folderName, String name, GitSampleRepoRule gitRepo, MockFolder parent) throws IOException {
Folder folder;
if (parent == null) {
folder = j.createProject(Folder.class, folderName);
} else {
folder = parent.createProject(Folder.class, folderName);
}

WorkflowMultiBranchProject mp = folder.createProject(WorkflowMultiBranchProject.class, name);
mp.getSourcesList().add(new BranchSource(new GitSCMSource(null, gitRepo.toString(), "", "*", "", false),
new DefaultBranchPropertyStrategy(new BranchProperty[0])));
@@ -194,4 +288,36 @@ public String getFileContents() {
}
}

@TestExtension(value = { "testProjectURL_CustomOrganization",
"testProjectInFolder_CustomOrganization",
"testMultibranchUrls_CustomOrganization" })
public static class TestOrganizationFactoryImpl extends OrganizationFactoryImpl {
private OrganizationImpl instance = new OrganizationImpl("TestOrg", Jenkins.getInstance().getItem("/TestOrgFolderName", Jenkins.getInstance(), MockFolder.class));

@Override
public OrganizationImpl get(String name) {
if (instance != null) {
if (instance.getName().equals(name)) {
System.out.println("" + name + " Intance returned " + instance);
return instance;
}
}
System.out.println("" + name + " no instance found");
return null;
}

@Override
public Collection<BlueOrganization> list() {
return Collections.singleton((BlueOrganization) instance);
}

@Override
public OrganizationImpl of(ItemGroup group) {
if (group == instance.getGroup()) {
return instance;
}
return null;
}
}

}

0 comments on commit cf3d09b

Please sign in to comment.
You can’t perform that action at this time.