Search in sources :

Example 56 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project nikita-noark5-core by HiOA-ABI.

the class ArchiveStructureTests method testCorrectArchiveStructureFull.

//    @Test
public void testCorrectArchiveStructureFull() throws Exception {
    String fondsTitle = "Test fonds title";
    String fondsDescription = "Test fonds description. This fonds should be automatically deleted after tests are undertaken";
    Fonds fonds = new Fonds();
    fonds.setTitle(fondsTitle);
    fonds.setDescription(fondsDescription);
    fonds.setDocumentMedium(DOCUMENT_MEDIUM_ELECTRONIC);
    MvcResult result = mockMvc.perform(post("/" + FONDS).contentType(MediaType.APPLICATION_JSON_VALUE).content(objectMapper.writeValueAsString(fonds))).andExpect(status().isOk()).andReturn();
    String responseObject = result.getResponse().getContentAsString();
    // How do I check this is actually a fonds, a try catch?
    fonds = objectMapper.readValue(responseObject, Fonds.class);
    System.out.println(fonds);
}
Also used : Fonds(nikita.model.noark5.v4.Fonds) MvcResult(org.springframework.test.web.servlet.MvcResult)

Example 57 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project ocvn by devgateway.

the class ReleaseExportTest method testReleaseExportIsValid.

@Test
public void testReleaseExportIsValid() throws Exception {
    final ClassLoader classLoader = getClass().getClassLoader();
    final File file = new File(classLoader.getResource("json/award-release-test.json").getFile());
    final JsonImport releaseJsonImport = new ReleaseJsonImport(releaseRepository, file, false);
    final Release release = (Release) releaseJsonImport.importObject();
    final MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.get("/api/ocds/release/ocid/" + release.getOcid()).accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)).andReturn();
    final String content = result.getResponse().getContentAsString();
    final JsonNode jsonNodeResponse = JsonLoader.fromString(content);
    final OcdsSchemaValidatorService.ProcessingReportWithNode processingReport = ocdsSchemaValidator.validate(jsonNodeResponse);
    if (!processingReport.getReport().isSuccess()) {
        for (ProcessingMessage processingMessage : processingReport.getReport()) {
            logger.error(">>> processingMessage: \n" + processingMessage);
        }
    }
    Assert.assertEquals("Is the release valid?", true, processingReport.getReport().isSuccess());
}
Also used : ReleaseJsonImport(org.devgateway.ocds.persistence.mongo.spring.json.ReleaseJsonImport) JsonImport(org.devgateway.ocds.persistence.mongo.spring.json.JsonImport) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) ReleaseJsonImport(org.devgateway.ocds.persistence.mongo.spring.json.ReleaseJsonImport) JsonNode(com.fasterxml.jackson.databind.JsonNode) MvcResult(org.springframework.test.web.servlet.MvcResult) OcdsSchemaValidatorService(org.devgateway.ocds.persistence.mongo.spring.OcdsSchemaValidatorService) File(java.io.File) Release(org.devgateway.ocds.persistence.mongo.Release) Test(org.junit.Test) AbstractWebTest(org.devgateway.toolkit.web.AbstractWebTest)

Example 58 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project metron by apache.

the class KafkaControllerIntegrationTest method test.

@Test
public void test() throws Exception {
    this.kafkaService.deleteTopic("bro");
    this.kafkaService.deleteTopic("someTopic");
    Thread.sleep(1000);
    testAndRetry(() -> this.mockMvc.perform(delete(kafkaUrl + "/topic/bro").with(httpBasic(user, password)).with(csrf())).andExpect(status().isNotFound()));
    testAndRetry(() -> this.mockMvc.perform(post(kafkaUrl + "/topic").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(broTopic)).andExpect(status().isCreated()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.name").value("bro")).andExpect(jsonPath("$.numPartitions").value(1)).andExpect(jsonPath("$.replicationFactor").value(1)));
    Thread.sleep(1000);
    testAndRetry(() -> this.mockMvc.perform(get(kafkaUrl + "/topic/bro").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$.name").value("bro")).andExpect(jsonPath("$.numPartitions").value(1)).andExpect(jsonPath("$.replicationFactor").value(1)));
    this.mockMvc.perform(get(kafkaUrl + "/topic/someTopic").with(httpBasic(user, password))).andExpect(status().isNotFound());
    testAndRetry(() -> this.mockMvc.perform(get(kafkaUrl + "/topic").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("application/json;charset=UTF-8"))).andExpect(jsonPath("$", Matchers.hasItem("bro"))));
    testAndRetry(() -> this.mockMvc.perform(post(kafkaUrl + "/topic/bro/produce").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(message1)).andExpect(status().isOk()));
    testAndRetry(() -> this.mockMvc.perform(get(kafkaUrl + "/topic/bro/sample").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("text/plain;charset=UTF-8"))).andExpect(jsonPath("$.type").value("message1")));
    testAndRetry(() -> this.mockMvc.perform(post(kafkaUrl + "/topic/bro/produce").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(message2)).andExpect(status().isOk()));
    testAndRetry(() -> this.mockMvc.perform(get(kafkaUrl + "/topic/bro/sample").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("text/plain;charset=UTF-8"))).andExpect(jsonPath("$.type").value("message2")));
    testAndRetry(() -> this.mockMvc.perform(post(kafkaUrl + "/topic/bro/produce").with(httpBasic(user, password)).with(csrf()).contentType(MediaType.parseMediaType("application/json;charset=UTF-8")).content(message3)).andExpect(status().isOk()));
    testAndRetry(() -> this.mockMvc.perform(get(kafkaUrl + "/topic/bro/sample").with(httpBasic(user, password))).andExpect(status().isOk()).andExpect(content().contentType(MediaType.parseMediaType("text/plain;charset=UTF-8"))).andExpect(jsonPath("$.type").value("message3")));
    this.mockMvc.perform(get(kafkaUrl + "/topic/someTopic/sample").with(httpBasic(user, password))).andExpect(status().isNotFound());
    boolean deleted = false;
    for (int i = 0; i < KAFKA_RETRY; ++i) {
        try {
            MvcResult result = this.mockMvc.perform(delete(kafkaUrl + "/topic/bro").with(httpBasic(user, password)).with(csrf())).andReturn();
            if (result.getResponse().getStatus() == 200) {
                deleted = true;
                break;
            }
            Thread.sleep(1000);
        } catch (NestedServletException nse) {
            Throwable t = nse.getRootCause();
            if (t instanceof TopicAlreadyMarkedForDeletionException) {
                continue;
            } else {
                throw nse;
            }
        } catch (Throwable t) {
            throw t;
        }
    }
    if (!deleted) {
        throw new IllegalStateException("Unable to delete kafka topic \"bro\"");
    }
}
Also used : NestedServletException(org.springframework.web.util.NestedServletException) TopicAlreadyMarkedForDeletionException(kafka.common.TopicAlreadyMarkedForDeletionException) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 59 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project CzechIdMng by bcvsolutions.

the class IdentityContractSecurityTest method getWorkPositions.

@Test
public void getWorkPositions() {
    SecurityMockMvcRequestPostProcessors.securityContext(null);
    Exception ex = null;
    int status = 0;
    try {
        status = getMockMvc().perform(get(BaseDtoController.BASE_PATH + "/identity-contracts")).andReturn().getResponse().getStatus();
    } catch (Exception e) {
        ex = e;
    }
    assertNull(ex);
    assertEquals(403, status);
    MvcResult mvcResult = null;
    ex = null;
    status = 0;
    try {
        mvcResult = getMockMvc().perform(get(BaseDtoController.BASE_PATH + "/identity-contracts").with(authentication(getAuthentication()))).andReturn();
    } catch (Exception e) {
        ex = e;
    }
    assertNull(ex);
    assertNotNull(mvcResult);
    assertEquals(200, mvcResult.getResponse().getStatus());
    logout();
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Example 60 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project CzechIdMng by bcvsolutions.

the class IdmLongRunningTaskItemControllerRestTest method getItem.

@Test
public void getItem() throws Exception {
    SecurityMockMvcRequestPostProcessors.securityContext(null);
    int status = getMockMvc().perform(get(BaseDtoController.BASE_PATH + PATH)).andReturn().getResponse().getStatus();
    assertEquals(403, status);
    MvcResult mvcResult = getMockMvc().perform(get(BaseDtoController.BASE_PATH + PATH).with(authentication(getAuthentication()))).andReturn();
    assertNotNull(mvcResult);
    assertEquals(200, mvcResult.getResponse().getStatus());
}
Also used : MvcResult(org.springframework.test.web.servlet.MvcResult) AbstractRestTest(eu.bcvsolutions.idm.test.api.AbstractRestTest) Test(org.junit.Test)

Aggregations

MvcResult (org.springframework.test.web.servlet.MvcResult)536 Test (org.junit.Test)300 Test (org.junit.jupiter.api.Test)143 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)132 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)69 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)38 MockHttpSession (org.springframework.mock.web.MockHttpSession)35 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)34 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)31 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)26 ExpectedDatabase (com.github.springtestdbunit.annotation.ExpectedDatabase)26 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)24 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)22 Map (java.util.Map)22 ResultActions (org.springframework.test.web.servlet.ResultActions)19 Cookie (javax.servlet.http.Cookie)18 MockMvc (org.springframework.test.web.servlet.MockMvc)17 HttpSession (jakarta.servlet.http.HttpSession)16 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 AbstractSaturnConsoleTest (com.vip.saturn.job.console.AbstractSaturnConsoleTest)15