Skip to content

Commit

Permalink
improve 'EmbeddableBadgeConfigsActionTest' test coverage (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekmaity authored Jan 27, 2024
1 parent 98bd706 commit d38681f
Showing 1 changed file with 107 additions and 15 deletions.
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");
}
}

0 comments on commit d38681f

Please sign in to comment.