Skip to content

Commit

Permalink
getting url type from config
Browse files Browse the repository at this point in the history
  • Loading branch information
julioc-p committed Sep 5, 2024
1 parent 160501b commit 8cc1367
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ public BitBucketPPRPullRequestServerAction(@Nonnull BitBucketPPRPayload payload)
}
}

if (!globalConfig.getPropagationUrl().isEmpty()) {
try {
this.baseUrl = new URL(globalConfig.getPropagationUrl());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
if (globalConfig.getPropagationUrl() != null) {

Check warning on line 90 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRPullRequestServerAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 90 is only partially covered, one branch is missing
this.baseUrl = globalConfig.getPropagationUrl();
}

logger.fine("BitBucketPPRPullRequestServerAction was called.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,8 @@ public BitBucketPPRServerRepositoryAction(BitBucketPPRPayload payload) {
}
}

if (!globalConfig.getPropagationUrl().isEmpty()) {
try {
this.baseUrl = new URL(globalConfig.getPropagationUrl());
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
if (globalConfig.getPropagationUrl() != null) {

Check warning on line 71 in src/main/java/io/jenkins/plugins/bitbucketpushandpullrequest/action/BitBucketPPRServerRepositoryAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 71 is only partially covered, one branch is missing
this.baseUrl = globalConfig.getPropagationUrl();
}

for (BitBucketPPRServerChange change : payload.getServerChanges()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.CheckForNull;
import java.net.URL;
import java.util.Collections;
import java.util.logging.Logger;

Expand All @@ -39,7 +40,7 @@ public class BitBucketPPRPluginConfig extends GlobalConfiguration {

public String singleJob;

public String propagationUrl;
public URL propagationUrl;

public BitBucketPPRPluginConfig() {
logger.fine("Read bitbucket push and pull request plugin global configuration.");
Expand All @@ -63,11 +64,13 @@ public void setHookUrl(String hookUrl) {

@DataBoundSetter
public void setPropagationUrl(String propagationUrl) {
if (isEmpty(propagationUrl)) {
this.propagationUrl = "";
} else {
this.propagationUrl = propagationUrl;

try {
this.propagationUrl = new URL(propagationUrl);
} catch (Exception e) {
throw new RuntimeException("Invalid URL: " + propagationUrl);
}

save();
}

Expand All @@ -83,7 +86,7 @@ public boolean getNotifyBitBucket() {
return notifyBitBucket;
}

public String getPropagationUrl() {
public URL getPropagationUrl() {
return propagationUrl;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -26,34 +27,43 @@
@ExtendWith(MockitoExtension.class)
public class BitBucketPPRTriggerTest {

@Test
public void testTriggerUrlOverridesBaseUrl() {
try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git");

BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref()).thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRPullRequestServerAction bitBucketPPRServerRepositoryAction = new BitBucketPPRPullRequestServerAction(payloadMock);
BitBucketPPRTriggerFilter bitBucketPPRTriggerFilter = mock(BitBucketPPRTriggerFilter.class);
BitBucketPPRTrigger bitBucketPPRTrigger = new BitBucketPPRTrigger(List.of(bitBucketPPRTriggerFilter));
bitBucketPPRTrigger.setPropagationUrl("https://example2.org/scm/some-namespace/some-repo2.git");
BitBucketPPRHookEvent bitBucketHookEvent = mock(BitBucketPPRHookEvent.class);

SCM scmTrigger = mock(SCM.class);
BitBucketPPRObservable observable = mock(BitBucketPPRObservable.class);
bitBucketPPRTrigger.onPost(bitBucketHookEvent, bitBucketPPRServerRepositoryAction, scmTrigger, observable);
assertEquals("https://example2.org:-1", bitBucketPPRServerRepositoryAction.getCommitLink().split("/rest/build-status/1.0/commits/")[0]);

} catch (Exception e) {
throw new RuntimeException(e);
}
@Test
public void testTriggerUrlOverridesBaseUrl() {
try (MockedStatic<BitBucketPPRPluginConfig> config =
Mockito.mockStatic(BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl())
.thenReturn(new URL("https://example.org/scm/some-namespace/some-repo.git"));

BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref())
.thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRPullRequestServerAction bitBucketPPRServerRepositoryAction =
new BitBucketPPRPullRequestServerAction(payloadMock);
BitBucketPPRTriggerFilter bitBucketPPRTriggerFilter = mock(BitBucketPPRTriggerFilter.class);
BitBucketPPRTrigger bitBucketPPRTrigger =
new BitBucketPPRTrigger(List.of(bitBucketPPRTriggerFilter));
bitBucketPPRTrigger.setPropagationUrl(
"https://example2.org/scm/some-namespace/some-repo2.git");
BitBucketPPRHookEvent bitBucketHookEvent = mock(BitBucketPPRHookEvent.class);

SCM scmTrigger = mock(SCM.class);
BitBucketPPRObservable observable = mock(BitBucketPPRObservable.class);
bitBucketPPRTrigger.onPost(
bitBucketHookEvent, bitBucketPPRServerRepositoryAction, scmTrigger, observable);
assertEquals(
"https://example2.org:-1",
bitBucketPPRServerRepositoryAction.getCommitLink()
.split("/rest/build-status/1.0/commits/")[0]);

} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.*;

public class BitBucketPPRPullRequestServerActionTest {
@Test
public void testBaseUrlSet() {
try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git");
@Test
public void testBaseUrlSet() {
try (MockedStatic<BitBucketPPRPluginConfig> config =
Mockito.mockStatic(BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl())
.thenReturn(new URL("https://example.org/scm/some-namespace/some-repo.git"));

BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref()).thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRPullRequestServerAction bitBucketPPRPullRequestServerAction;
try {
bitBucketPPRPullRequestServerAction = new BitBucketPPRPullRequestServerAction(payloadMock);
assertDoesNotThrow(() -> {
bitBucketPPRPullRequestServerAction.getCommitLink();
});
} catch (BitBucketPPRPayloadPropertyNotFoundException e) {
e.printStackTrace();
}
}
BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref())
.thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRPullRequestServerAction bitBucketPPRPullRequestServerAction;
try {
bitBucketPPRPullRequestServerAction = new BitBucketPPRPullRequestServerAction(payloadMock);
assertDoesNotThrow(
() -> {
bitBucketPPRPullRequestServerAction.getCommitLink();
});
} catch (BitBucketPPRPayloadPropertyNotFoundException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}


}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.jenkins.plugins.bitbucketpushandpullrequest.action;


import io.jenkins.plugins.bitbucketpushandpullrequest.config.BitBucketPPRPluginConfig;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.BitBucketPPRPayload;
import io.jenkins.plugins.bitbucketpushandpullrequest.model.server.BitBucketPPRServerClone;
Expand All @@ -10,6 +9,8 @@
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -19,31 +20,32 @@
@ExtendWith(MockitoExtension.class)
public class BitBucketPPRServerRepositoryActionTest {


@Test
public void testBaseUrlSet() {
try (MockedStatic<BitBucketPPRPluginConfig> config = Mockito.mockStatic(
BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl()).thenReturn("https://example.org/scm/some-namespace/some-repo.git");

BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref()).thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRServerRepositoryAction bitBucketPPRServerRepositoryAction = new BitBucketPPRServerRepositoryAction(payloadMock);

assertDoesNotThrow(() -> {
bitBucketPPRServerRepositoryAction.getCommitLinks();
});
}


@Test
public void testBaseUrlSet() {
try (MockedStatic<BitBucketPPRPluginConfig> config =
Mockito.mockStatic(BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl())
.thenReturn(new URL("https://example.org/scm/some-namespace/some-repo.git"));

BitBucketPPRPayload payloadMock = mock(BitBucketPPRPayload.class, RETURNS_DEEP_STUBS);
List<BitBucketPPRServerClone> clones = new ArrayList<>();
BitBucketPPRServerClone mockServerClone = mock(BitBucketPPRServerClone.class);
when(mockServerClone.getName()).thenReturn("ssh");
when(mockServerClone.getHref())
.thenReturn("ssh://[email protected]/some-namespace/some-repo.git");
clones.add(mockServerClone);
when(payloadMock.getServerRepository().getLinks().getCloneProperty()).thenReturn(clones);
BitBucketPPRServerRepositoryAction bitBucketPPRServerRepositoryAction =
new BitBucketPPRServerRepositoryAction(payloadMock);

assertDoesNotThrow(
() -> {
bitBucketPPRServerRepositoryAction.getCommitLinks();
});
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -76,7 +77,8 @@ public void testProcessPayload() throws Exception {
Mockito.mockStatic(BitBucketPPRPluginConfig.class)) {
BitBucketPPRPluginConfig c = mock(BitBucketPPRPluginConfig.class);
config.when(BitBucketPPRPluginConfig::getInstance).thenReturn(c);
when(c.getPropagationUrl()).thenReturn("");
when(c.getPropagationUrl())
.thenReturn(new URL("https://example.org/scm/some-namespace/some-repo.git"));

BitBucketPPRJobProbe probe = mock(BitBucketPPRJobProbe.class);

Expand Down

0 comments on commit 8cc1367

Please sign in to comment.