Skip to content

Commit

Permalink
Improves score controller testing
Browse files Browse the repository at this point in the history
Test still failing because I'm using Java record and hamcrest/JavaHamcrest#392
  • Loading branch information
alecharp committed Apr 4, 2023
1 parent a465486 commit 47ebca9
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,23 @@
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.jenkins.pluginhealth.scoring.model.Plugin;
import io.jenkins.pluginhealth.scoring.model.ProbeResult;
import io.jenkins.pluginhealth.scoring.model.Score;
import io.jenkins.pluginhealth.scoring.model.ScoreResult;
import io.jenkins.pluginhealth.scoring.scores.Scoring;
Expand Down Expand Up @@ -74,39 +78,43 @@ void shouldDisplayListOfScoring() throws Exception {
mockMvc.perform(get("/scores"))
.andExpect(status().isOk())
.andExpect(view().name("scores/listing"))
.andExpect(model().attribute("module", "scores"))
.andExpect(model().attributeExists("scorings"));
}

@Test
void shouldDisplayScoreForSpecificPlugin() throws Exception {
final String pluginName = "foo-bar";
final ZonedDateTime scoreComputationTime = ZonedDateTime.now().minusMinutes(10);
final String probeKey = "probe-key";
final String scoreKey = "score-key";

final Plugin plugin = mock(Plugin.class);
when(plugin.getName()).thenReturn(pluginName);

final Scoring scoring = mock(Scoring.class);
when(scoring.description()).thenReturn("description");
when(plugin.getDetails()).thenReturn(Map.of(
probeKey, ProbeResult.success(probeKey, "message")
));

final Score score = mock(Score.class);
when(score.getPlugin()).thenReturn(plugin);
when(score.getComputedAt()).thenReturn(scoreComputationTime);
when(score.getValue()).thenReturn(42L);
when(score.getDetails()).thenReturn(Set.of(
new ScoreResult("foo", .42f, 1f)
new ScoreResult(scoreKey, .42f, 1f)
));

when(scoringService.get("foo")).thenReturn(Optional.of(scoring));
when(scoreService.latestScoreFor(pluginName)).thenReturn(Optional.of(score));

mockMvc.perform(get("/scores/{pluginName}", pluginName))
.andExpect(status().isOk())
.andExpect(model().attributeExists("score"))
.andExpect(view().name("scores/details"))
.andExpect(model().attribute("module", "scores"))
.andExpect(model().attributeExists("score", "scores", "probes"))
.andExpect(model().attribute(
"score",
allOf(
hasProperty("value", equalTo(42L)),
hasProperty("computedAt", equalTo(scoreComputationTime))
hasProperty("pluginName", equalTo(pluginName)),
hasProperty("probeResults", instanceOf(Map.class)),
hasProperty("details", instanceOf(Collection.class))
)
));
}
Expand All @@ -119,7 +127,7 @@ void shouldDisplayErrorWhenPluginUnknown() throws Exception {
mockMvc.perform(get("/scores/foo-bar"))
.andExpect(status().isNotFound())
.andExpect(view().name("scores/unknown"))
.andExpect(model().attribute("module", "scores"))
.andExpect(model().attribute("pluginName", equalTo("foo-bar")));
}

}

0 comments on commit 47ebca9

Please sign in to comment.