Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-70464] Improve EmbeddableBadgeConfigsActionTest test coverage #287

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
Loading