Make no assumptions about configured scripts in ITs

This commit is contained in:
Yura Bondarenko
2021-09-01 13:51:48 +02:00
parent 7cb25c6121
commit c9e4bb8106

View File

@@ -22,6 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import java.io.IOException; import java.io.IOException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@@ -81,24 +82,14 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
getClient(token).perform(get("/api/system/scripts")) getClient(token).perform(get("/api/system/scripts"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.scripts", containsInAnyOrder( .andExpect(jsonPath("$._embedded.scripts", containsInAnyOrder(
ScriptMatcher.matchScript(scriptConfigurations.get(0).getName(), scriptConfigurations
scriptConfigurations.get(0).getDescription()), .stream()
ScriptMatcher.matchScript(scriptConfigurations.get(1).getName(), .map(scriptConfiguration -> ScriptMatcher.matchScript(
scriptConfigurations.get(1).getDescription()), scriptConfiguration.getName(),
ScriptMatcher.matchScript(scriptConfigurations.get(2).getName(), scriptConfiguration.getDescription()
scriptConfigurations.get(2).getDescription()), ))
ScriptMatcher.matchScript(scriptConfigurations.get(3).getName(), .collect(Collectors.toList())
scriptConfigurations.get(3).getDescription()),
ScriptMatcher.matchScript(scriptConfigurations.get(4).getName(),
scriptConfigurations.get(4).getDescription()),
ScriptMatcher.matchScript(scriptConfigurations.get(5).getName(),
scriptConfigurations.get(5).getDescription()),
ScriptMatcher.matchScript(scriptConfigurations.get(6).getName(),
scriptConfigurations.get(6).getDescription()),
ScriptMatcher.matchScript(scriptConfigurations.get(7).getName(),
scriptConfigurations.get(7).getDescription())
))); )));
} }
@@ -113,18 +104,26 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
@Test @Test
public void findAllScriptsPaginationTest() throws Exception { public void findAllScriptsPaginationTest() throws Exception {
List<ScriptConfiguration> alphabeticScripts =
scriptConfigurations.stream()
.sorted(Comparator.comparing(s -> s.getClass().getName()))
.collect(Collectors.toList());
int totalPages = scriptConfigurations.size();
int lastPage = totalPages - 1;
String token = getAuthToken(admin.getEmail(), password); String token = getAuthToken(admin.getEmail(), password);
// NOTE: the scripts are always returned in alphabetical order by fully qualified class name.
getClient(token).perform(get("/api/system/scripts").param("size", "1")) getClient(token).perform(get("/api/system/scripts").param("size", "1"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.scripts", Matchers.not(Matchers.hasItem( .andExpect(jsonPath("$._embedded.scripts", Matchers.not(Matchers.hasItem(
ScriptMatcher.matchScript(scriptConfigurations.get(2).getName(), ScriptMatcher.matchScript(alphabeticScripts.get(1).getName(),
scriptConfigurations.get(2).getDescription()) alphabeticScripts.get(1).getDescription())
)))) ))))
.andExpect(jsonPath("$._embedded.scripts", hasItem( .andExpect(jsonPath("$._embedded.scripts", hasItem(
ScriptMatcher.matchScript(scriptConfigurations.get(5).getName(), ScriptMatcher.matchScript(alphabeticScripts.get(0).getName(),
scriptConfigurations.get(5).getDescription()) alphabeticScripts.get(0).getDescription())
))) )))
.andExpect(jsonPath("$._links.first.href", Matchers.allOf( .andExpect(jsonPath("$._links.first.href", Matchers.allOf(
Matchers.containsString("/api/system/scripts?"), Matchers.containsString("/api/system/scripts?"),
@@ -137,22 +136,22 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
Matchers.containsString("page=1"), Matchers.containsString("size=1")))) Matchers.containsString("page=1"), Matchers.containsString("size=1"))))
.andExpect(jsonPath("$._links.last.href", Matchers.allOf( .andExpect(jsonPath("$._links.last.href", Matchers.allOf(
Matchers.containsString("/api/system/scripts?"), Matchers.containsString("/api/system/scripts?"),
Matchers.containsString("page=7"), Matchers.containsString("size=1")))) Matchers.containsString("page=" + lastPage), Matchers.containsString("size=1"))))
.andExpect(jsonPath("$.page.size", is(1))) .andExpect(jsonPath("$.page.size", is(1)))
.andExpect(jsonPath("$.page.number", is(0))) .andExpect(jsonPath("$.page.number", is(0)))
.andExpect(jsonPath("$.page.totalPages", is(8))) .andExpect(jsonPath("$.page.totalPages", is(totalPages)))
.andExpect(jsonPath("$.page.totalElements", is(8))); .andExpect(jsonPath("$.page.totalElements", is(totalPages)));
getClient(token).perform(get("/api/system/scripts").param("size", "1").param("page", "1")) getClient(token).perform(get("/api/system/scripts").param("size", "1").param("page", "1"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.scripts", hasItem( .andExpect(jsonPath("$._embedded.scripts", hasItem(
ScriptMatcher.matchScript(scriptConfigurations.get(2).getName(), ScriptMatcher.matchScript(alphabeticScripts.get(1).getName(),
scriptConfigurations.get(2).getDescription()) alphabeticScripts.get(1).getDescription())
))) )))
.andExpect(jsonPath("$._embedded.scripts", Matchers.not(hasItem( .andExpect(jsonPath("$._embedded.scripts", Matchers.not(hasItem(
ScriptMatcher.matchScript(scriptConfigurations.get(5).getName(), ScriptMatcher.matchScript(alphabeticScripts.get(0).getName(),
scriptConfigurations.get(5).getDescription()) alphabeticScripts.get(0).getDescription())
)))) ))))
.andExpect(jsonPath("$._links.first.href", Matchers.allOf( .andExpect(jsonPath("$._links.first.href", Matchers.allOf(
Matchers.containsString("/api/system/scripts?"), Matchers.containsString("/api/system/scripts?"),
@@ -168,11 +167,11 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
Matchers.containsString("page=2"), Matchers.containsString("size=1")))) Matchers.containsString("page=2"), Matchers.containsString("size=1"))))
.andExpect(jsonPath("$._links.last.href", Matchers.allOf( .andExpect(jsonPath("$._links.last.href", Matchers.allOf(
Matchers.containsString("/api/system/scripts?"), Matchers.containsString("/api/system/scripts?"),
Matchers.containsString("page=7"), Matchers.containsString("size=1")))) Matchers.containsString("page=" + lastPage), Matchers.containsString("size=1"))))
.andExpect(jsonPath("$.page.size", is(1))) .andExpect(jsonPath("$.page.size", is(1)))
.andExpect(jsonPath("$.page.number", is(1))) .andExpect(jsonPath("$.page.number", is(1)))
.andExpect(jsonPath("$.page.totalPages", is(8))) .andExpect(jsonPath("$.page.totalPages", is(totalPages)))
.andExpect(jsonPath("$.page.totalElements", is(8))); .andExpect(jsonPath("$.page.totalElements", is(totalPages)));
} }
@Test @Test
@@ -183,7 +182,15 @@ public class ScriptRestRepositoryIT extends AbstractControllerIntegrationTest {
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", ScriptMatcher .andExpect(jsonPath("$", ScriptMatcher
.matchMockScript( .matchMockScript(
scriptConfigurations.get(scriptConfigurations.size() - 1).getOptions()))); scriptConfigurations
.stream()
.filter(scriptConfiguration
-> scriptConfiguration.getName().equals("mock-script"))
.findAny()
.orElseThrow()
.getOptions()
)
));
} }
@Test @Test