-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into test-case-improve-2
- Loading branch information
Showing
2 changed files
with
223 additions
and
15 deletions.
There are no files selected for viewing
122 changes: 107 additions & 15 deletions
122
src/test/java/org/jenkinsci/plugins/badge/actions/EmbeddableBadgeConfigsActionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,125 @@ | ||
package org.jenkinsci.plugins.badge.actions; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNull.nullValue; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import hudson.model.Run; | ||
import org.jenkinsci.plugins.badge.EmbeddableBadgeConfig; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class EmbeddableBadgeConfigsActionTest { | ||
class EmbeddableBadgeConfigsActionTest { | ||
|
||
EmbeddableBadgeConfigsAction embeddableBadgeConfigsAction; | ||
@Test | ||
void testGetUrlName() { | ||
// Given | ||
EmbeddableBadgeConfigsAction action = new EmbeddableBadgeConfigsAction(); | ||
|
||
// When | ||
String urlName = action.getUrlName(); | ||
|
||
// Then | ||
assertEquals("", urlName, "UrlName should be an empty string"); | ||
} | ||
|
||
@Test | ||
void testGetDisplayName() { | ||
// Given | ||
EmbeddableBadgeConfigsAction action = new EmbeddableBadgeConfigsAction(); | ||
|
||
// When | ||
String displayName = action.getDisplayName(); | ||
|
||
// Then | ||
assertEquals("", displayName, "DisplayName should be an empty string"); | ||
} | ||
|
||
@Test | ||
void testGetIconFileName() { | ||
// Given | ||
EmbeddableBadgeConfigsAction action = new EmbeddableBadgeConfigsAction(); | ||
|
||
// When | ||
String iconFileName = action.getIconFileName(); | ||
|
||
// Then | ||
assertNull(iconFileName, "IconFileName should be null"); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
embeddableBadgeConfigsAction = new EmbeddableBadgeConfigsAction(); | ||
@Test | ||
void testResolveWithValidId() { | ||
// Given | ||
Run<?, ?> run = mock(Run.class); | ||
EmbeddableBadgeConfigsAction action = mock(EmbeddableBadgeConfigsAction.class); | ||
when(run.getAction(EmbeddableBadgeConfigsAction.class)).thenReturn(action); | ||
String id = "someId"; | ||
EmbeddableBadgeConfig expectedConfig = mock(EmbeddableBadgeConfig.class); | ||
when(action.getConfig(id)).thenReturn(expectedConfig); | ||
|
||
// When | ||
EmbeddableBadgeConfig actualConfig = EmbeddableBadgeConfigsAction.resolve(run, id); | ||
|
||
// Then | ||
assertEquals(expectedConfig, actualConfig); | ||
} | ||
|
||
@Test | ||
void testResolveWithNullId() { | ||
// Given | ||
Run<?, ?> run = mock(Run.class); | ||
|
||
// When | ||
EmbeddableBadgeConfig actualConfig = EmbeddableBadgeConfigsAction.resolve(run, null); | ||
|
||
// Then | ||
assertNull(actualConfig); | ||
} | ||
|
||
@Test | ||
public void getUrlNameTest() { | ||
assertThat(embeddableBadgeConfigsAction.getUrlName(), is("")); | ||
void testResolveWithNullAction() { | ||
// Given | ||
Run<?, ?> run = mock(Run.class); | ||
when(run.getAction(EmbeddableBadgeConfigsAction.class)).thenReturn(null); | ||
String id = "someId"; | ||
|
||
// When | ||
EmbeddableBadgeConfig actualConfig = EmbeddableBadgeConfigsAction.resolve(run, id); | ||
|
||
// Then | ||
assertNull(actualConfig); | ||
} | ||
|
||
@Test | ||
public void getDisplayNameTest() { | ||
assertThat(embeddableBadgeConfigsAction.getDisplayName(), is("")); | ||
void testGetConfig() { | ||
// Given | ||
EmbeddableBadgeConfigsAction action = new EmbeddableBadgeConfigsAction(); | ||
EmbeddableBadgeConfig config = mock(EmbeddableBadgeConfig.class); | ||
when(config.getID()).thenReturn("someId"); | ||
action.addConfig(config); | ||
|
||
// When | ||
EmbeddableBadgeConfig actualConfig = action.getConfig("someId"); | ||
|
||
// Then | ||
assertNotNull(actualConfig, "Config should not be null"); | ||
assertEquals(config, actualConfig, "Config objects should be equal"); | ||
assertEquals("someId", actualConfig.getID(), "Config ID should match"); | ||
} | ||
|
||
@Test | ||
public void getIconFileNameTest() { | ||
assertThat(embeddableBadgeConfigsAction.getIconFileName(), is(nullValue())); | ||
void testAddConfig() { | ||
// Given | ||
EmbeddableBadgeConfigsAction action = new EmbeddableBadgeConfigsAction(); | ||
EmbeddableBadgeConfig config = mock(EmbeddableBadgeConfig.class); | ||
when(config.getID()).thenReturn("someId"); | ||
|
||
// When | ||
action.addConfig(config); | ||
|
||
// Then | ||
EmbeddableBadgeConfig actualConfig = action.getConfig("someId"); | ||
assertNotNull(actualConfig, "Config should not be null"); | ||
assertEquals(config, actualConfig, "Config objects should be equal"); | ||
assertEquals("someId", actualConfig.getID(), "Config ID should match"); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
...test/java/org/jenkinsci/plugins/badge/extensions/BuildParameterResolverExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package org.jenkinsci.plugins.badge.extensions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import hudson.model.ParameterValue; | ||
import hudson.model.ParametersAction; | ||
import hudson.model.Run; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
public class BuildParameterResolverExtensionTest { | ||
|
||
@Mock | ||
private Run<?, ?> runMock; | ||
|
||
@Mock | ||
private ParametersAction paramsMock; | ||
|
||
private BuildParameterResolverExtension resolver; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
resolver = new BuildParameterResolverExtension(); | ||
} | ||
|
||
// A custom class to control the toString() behavior | ||
private static class CustomObject { | ||
final String returnValue; | ||
|
||
CustomObject(String returnValue) { | ||
this.returnValue = returnValue; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return returnValue; | ||
} | ||
} | ||
|
||
@Test | ||
void resolveShouldReturnOriginalParameterIfRunIsNull() { | ||
String parameter = "params.paramName"; | ||
assertEquals(parameter, resolver.resolve(null, parameter)); | ||
} | ||
|
||
@Test | ||
void resolveShouldReturnOriginalParameterIfNoParametersAction() { | ||
String parameter = "params.paramName"; | ||
when(runMock.getAction(ParametersAction.class)).thenReturn(null); | ||
|
||
assertEquals(parameter, resolver.resolve(runMock, parameter)); | ||
} | ||
|
||
@Test | ||
void resolveShouldReplaceParameterWithParamValue() { | ||
String paramName = "paramName"; | ||
String parameter = "params." + paramName; | ||
String paramValue = "paramValue"; | ||
|
||
ParameterValue valueMock = mock(ParameterValue.class); | ||
when(valueMock.getValue()).thenReturn(paramValue); | ||
|
||
when(paramsMock.getParameter(paramName)).thenReturn(valueMock); | ||
when(runMock.getAction(ParametersAction.class)).thenReturn(paramsMock); | ||
|
||
assertEquals(paramValue, resolver.resolve(runMock, parameter)); | ||
} | ||
|
||
@Test | ||
void resolveShouldReplaceParameterWithDefaultValueIfParamNotFound() { | ||
String paramName = "paramName"; | ||
String defaultValue = "default"; | ||
String parameter = "params." + paramName + "|" + defaultValue; | ||
|
||
when(paramsMock.getParameter(paramName)).thenReturn(null); | ||
when(runMock.getAction(ParametersAction.class)).thenReturn(paramsMock); | ||
|
||
assertEquals(defaultValue, resolver.resolve(runMock, parameter)); | ||
} | ||
|
||
@Test | ||
void resolveShouldReplaceParameterWithDefaultValueIfParamValueIsNull() { | ||
String paramName = "paramName"; | ||
String defaultValue = "default"; | ||
String parameter = "params." + paramName + "|" + defaultValue; | ||
|
||
ParameterValue valueMock = mock(ParameterValue.class); | ||
when(valueMock.getValue()).thenReturn(null); | ||
|
||
when(paramsMock.getParameter(paramName)).thenReturn(valueMock); | ||
when(runMock.getAction(ParametersAction.class)).thenReturn(paramsMock); | ||
|
||
assertEquals(defaultValue, resolver.resolve(runMock, parameter)); | ||
} | ||
|
||
@Test | ||
void resolveShouldReplaceParameterWithCustomObjectValueIfParamValueToStringIsNull() { | ||
String paramName = "paramName"; | ||
String defaultValue = "default"; | ||
String parameter = "params." + paramName + "|" + defaultValue; | ||
String customObjectReturnValue = defaultValue + "CustomObject"; | ||
|
||
ParameterValue valueMock = mock(ParameterValue.class); | ||
when(valueMock.getValue()).thenReturn(new CustomObject(customObjectReturnValue)); | ||
|
||
when(paramsMock.getParameter(paramName)).thenReturn(valueMock); | ||
when(runMock.getAction(ParametersAction.class)).thenReturn(paramsMock); | ||
|
||
assertEquals(customObjectReturnValue, resolver.resolve(runMock, parameter)); | ||
} | ||
} |