Search in sources :

Example 46 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project TOSCAna by StuPro-TOSCAna.

the class TransformationControllerTest method getInputsTest2.

@Test
public void getInputsTest2() throws Exception {
    preInitNonCreationTests();
    // Set a SimpleProperty value
    String inputKey = "secret_input";
    String inputValue = "geheim";
    csarService.getCsar(VALID_CSAR_NAME).get().getTransformation(VALID_PLATFORM_NAME).get().getInputs().set(inputKey, inputValue);
    // Perform a request
    MvcResult result = mvc.perform(get(INPUTS_VALID_URL)).andDo(print()).andExpect(status().is(200)).andExpect(content().contentType(DEFAULT_CHARSET_HAL_JSON)).andReturn();
    // Check if only one value is set (the one that has been set above) and the others are not!
    JSONArray obj = new JSONObject(result.getResponse().getContentAsString()).getJSONArray("inputs");
    boolean valueFound = false;
    boolean restNull = true;
    for (int i = 0; i < obj.length(); i++) {
        JSONObject content = obj.getJSONObject(i);
        if (content.getString("key").equals(inputKey)) {
            valueFound = content.getString("value").equals(inputValue);
        } else {
            restNull = restNull && (content.isNull("value") || content.getString("value").equals(INPUT_TEST_DEFAULT_VALUE));
        }
    }
    assertTrue("Could not find valid value in property list", valueFound);
    assertTrue("Not all other values in property list are null or equal to the default value", restNull);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvcResultHandlers.print(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print) Test(org.junit.Test) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest)

Example 47 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project TOSCAna by StuPro-TOSCAna.

the class TransformationControllerTest method getInputsTest.

@Test
public void getInputsTest() throws Exception {
    preInitNonCreationTests();
    MvcResult result = mvc.perform(get(INPUTS_VALID_URL)).andDo(print()).andExpect(status().is(200)).andExpect(content().contentType(DEFAULT_CHARSET_HAL_JSON)).andExpect(jsonPath("$.inputs").isArray()).andExpect(jsonPath("$.inputs").isNotEmpty()).andExpect(jsonPath("$.inputs[0].key").isString()).andExpect(jsonPath("$.inputs[0].type").isString()).andExpect(jsonPath("$.inputs[0].description").isString()).andExpect(jsonPath("$.inputs[0].required").isBoolean()).andExpect(jsonPath("$.links[0].rel").value("self")).andExpect(jsonPath("$.links[0].href").value("http://localhost/api/csars/kubernetes-cluster/transformations/p-a/inputs")).andReturn();
    MockHttpServletResponse response = result.getResponse();
    String responseJson = new String(response.getContentAsByteArray());
    String[] values = JsonPath.parse(responseJson).read("$.inputs[*].value", String[].class);
    long nullCount = Arrays.asList(values).stream().filter(Objects::isNull).count();
    long testCount = Arrays.asList(values).stream().filter(e -> e != null && e.equals(INPUT_TEST_DEFAULT_VALUE)).count();
    assertEquals(8, nullCount);
    assertEquals(1, testCount);
}
Also used : TargetArtifact(org.opentosca.toscana.core.transformation.artifacts.TargetArtifact) Arrays(java.util.Arrays) MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) Random(java.util.Random) TransformationService(org.opentosca.toscana.core.transformation.TransformationService) JSONObject(org.json.JSONObject) PlatformNotFoundException(org.opentosca.toscana.api.exceptions.PlatformNotFoundException) Map(java.util.Map) OutputProperty(org.opentosca.toscana.core.transformation.properties.OutputProperty) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) MockMvcResultHandlers.print(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print) PropertyType(org.opentosca.toscana.core.transformation.properties.PropertyType) MediaType(org.springframework.http.MediaType) PlatformInput(org.opentosca.toscana.core.transformation.properties.PlatformInput) Set(java.util.Set) Csar(org.opentosca.toscana.core.csar.Csar) Objects(java.util.Objects) List(java.util.List) Log(org.opentosca.toscana.core.transformation.logging.Log) LogEntry(org.opentosca.toscana.core.transformation.logging.LogEntry) PlatformService(org.opentosca.toscana.core.transformation.platform.PlatformService) Optional(java.util.Optional) TransformationState(org.opentosca.toscana.core.transformation.TransformationState) TestCsars(org.opentosca.toscana.core.testdata.TestCsars) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Transformation(org.opentosca.toscana.core.transformation.Transformation) HashMap(java.util.HashMap) Mockito.spy(org.mockito.Mockito.spy) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ArrayList(java.util.ArrayList) TransformationImpl(org.opentosca.toscana.core.transformation.TransformationImpl) HashSet(java.util.HashSet) MockMvc(org.springframework.test.web.servlet.MockMvc) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) Lists(com.google.common.collect.Lists) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) Platform(org.opentosca.toscana.core.transformation.platform.Platform) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvcBuilders(org.springframework.test.web.servlet.setup.MockMvcBuilders) CsarImpl(org.opentosca.toscana.core.csar.CsarImpl) Before(org.junit.Before) ByteArrayUtils(org.opentosca.toscana.core.testdata.ByteArrayUtils) Assert.assertTrue(org.junit.Assert.assertTrue) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JsonPath(com.jayway.jsonpath.JsonPath) File(java.io.File) CsarService(org.opentosca.toscana.core.csar.CsarService) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest) Level(ch.qos.logback.classic.Level) HALRelationUtils(org.opentosca.toscana.api.utils.HALRelationUtils) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Collections(java.util.Collections) JSONArray(org.json.JSONArray) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MvcResult(org.springframework.test.web.servlet.MvcResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest)

Example 48 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project TOSCAna by StuPro-TOSCAna.

the class TransformationControllerTest method transformationDetails.

// </editor-fold>
// <editor-fold desc="Transformation Details Test">
@Test
public void transformationDetails() throws Exception {
    preInitNonCreationTests();
    MvcResult result = mvc.perform(get(TRANSFORMATION_DETAILS_VALID_URL).accept(APPLICATION_HAL_JSON_MIME_TYPE)).andDo(print()).andExpect(status().is(200)).andExpect(content().contentType(DEFAULT_CHARSET_HAL_JSON)).andExpect(jsonPath("$.phases").isArray()).andExpect(jsonPath("$.phases").isEmpty()).andExpect(jsonPath("$.platform").value(VALID_PLATFORM_NAME)).andExpect(jsonPath("$.state").value("INPUT_REQUIRED")).andReturn();
    JSONObject object = new JSONObject(result.getResponse().getContentAsString());
    HALRelationUtils.validateRelations(object.getJSONArray("links"), getLinkRelationsForTransformationDetails(), VALID_CSAR_NAME, VALID_PLATFORM_NAME);
}
Also used : JSONObject(org.json.JSONObject) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test) BaseSpringTest(org.opentosca.toscana.core.BaseSpringTest)

Example 49 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project open-kilda by telstra.

the class FlowControllerTest method pathFlow.

@Test
@WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
public void pathFlow() throws Exception {
    MvcResult result = mockMvc.perform(get("/flows/path/{flow-id}", TestMessageMock.FLOW_ID).header(CORRELATION_ID, testCorrelationId()).contentType(APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE)).andReturn();
    FlowPathPayload response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPathPayload.class);
    assertEquals(TestMessageMock.flowPath, response);
}
Also used : FlowPathPayload(org.openkilda.messaging.payload.flow.FlowPathPayload) MvcResult(org.springframework.test.web.servlet.MvcResult) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 50 with MvcResult

use of org.springframework.test.web.servlet.MvcResult in project open-kilda by telstra.

the class FlowControllerTest method deleteFlows.

@Test
@WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
public void deleteFlows() throws Exception {
    MvcResult result = mockMvc.perform(delete("/flows").header(CORRELATION_ID, testCorrelationId()).header(EXTRA_AUTH, System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(119)).contentType(APPLICATION_JSON_VALUE)).andExpect(status().isOk()).andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE)).andReturn();
    FlowPayload[] response = MAPPER.readValue(result.getResponse().getContentAsString(), FlowPayload[].class);
    assertEquals(TestMessageMock.flow, response[0]);
}
Also used : FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) MvcResult(org.springframework.test.web.servlet.MvcResult) WithMockUser(org.springframework.security.test.context.support.WithMockUser) 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