From 47ebca9c5e7c275a09f39cb795c4c13726a29150 Mon Sep 17 00:00:00 2001 From: Adrien Lecharpentier Date: Tue, 4 Apr 2023 12:11:49 +0200 Subject: [PATCH] Improves score controller testing Test still failing because I'm using Java record and https://github.com/hamcrest/JavaHamcrest/issues/392 --- .../scoring/http/ScoreControllerTest.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/war/src/test/java/io/jenkins/pluginhealth/scoring/http/ScoreControllerTest.java b/war/src/test/java/io/jenkins/pluginhealth/scoring/http/ScoreControllerTest.java index a0da84783..8ae1e6d68 100644 --- a/war/src/test/java/io/jenkins/pluginhealth/scoring/http/ScoreControllerTest.java +++ b/war/src/test/java/io/jenkins/pluginhealth/scoring/http/ScoreControllerTest.java @@ -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; @@ -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)) ) )); } @@ -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"))); } - }