Skip to content

Commit

Permalink
Added tests for logic in MultipartFileResourceResolverTest and test t…
Browse files Browse the repository at this point in the history
…o ensure the endpoint was called in SPipesServiceControllerTest
  • Loading branch information
Zdenek authored and blcham committed Mar 9, 2022
1 parent c210206 commit 591e5a2
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cz.cvut.spipes.rest.util;

import cz.cvut.spipes.config.WebAppConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
@ContextConfiguration(classes = WebAppConfig.class)
class MultipartFileResourceResolverTest {

@Autowired
private MultipartFileResourceResolver resolver;

@BeforeEach
void setUp() {
}

@Test
void testResolveResourcesReplacesFilenamesWithUrisInParameters() {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("testKey1", "@testFile1.txt");
parameters.add("testKey2", "@testFile2.txt");

MockMultipartFile testFile1
= new MockMultipartFile(
"testFile1",
"testFile1.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 1".getBytes()
);
MockMultipartFile testFile2
= new MockMultipartFile(
"testFile2",
"testFile2.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 2".getBytes()
);

MultipartFile[] files = new MultipartFile[]{testFile1, testFile2};

MultiValueMap<String, String> newParameters = resolver.resolveResources(parameters, files);

assertEquals(parameters.keySet(), newParameters.keySet());
assertEquals(1, newParameters.get("testKey1").size());
assertEquals(1, newParameters.get("testKey2").size());
assertTrue(isValidUri(newParameters.get("testKey1").get(0)));
assertTrue(isValidUri(newParameters.get("testKey2").get(0)));
}

@Test
void testResolveResourcesDoesNotReplaceWrongFilenames() {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("testKey1", "@WRONG_FILENAME.txt");
parameters.add("testKey2", "testFile2.txt");

MockMultipartFile testFile1
= new MockMultipartFile(
"testFile1",
"testFile1.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 1".getBytes()
);
MockMultipartFile testFile2
= new MockMultipartFile(
"testFile2",
"testFile2.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 2".getBytes()
);

MultipartFile[] files = new MultipartFile[]{testFile1, testFile2};

MultiValueMap<String, String> newParameters = resolver.resolveResources(parameters, files);

assertEquals(parameters, newParameters);
}

private boolean isValidUri(String uriToCheck) {
try {
new URI(uriToCheck);
return true;
} catch (Exception e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartFile;

@ExtendWith(SpringExtension.class)
@WebAppConfiguration
Expand Down Expand Up @@ -187,6 +191,34 @@ public void testRunApplyConstructNotReplace() throws Exception {
2);
}

@Test
public void testProcessServicePostRequestGoesThroughWithFiles() throws Exception {
MockMultipartFile testFile1
= new MockMultipartFile(
"testFile1",
"testFile1.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 1".getBytes()
);
MockMultipartFile testFile2
= new MockMultipartFile(
"testFile2",
"testFile2.txt",
MediaType.TEXT_PLAIN_VALUE,
"Hello 2".getBytes()
);

MockHttpServletRequestBuilder rb = multipart("/service")
.file(testFile1)
.file(testFile2);
rb.param("testKey1", "@testFile1.txt")
.param("testKey2", "@testFile2.txt");

mockMvc.perform(rb)
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(RDFMimeType.LD_JSON_STRING));
}

@Disabled // works only within fell vpn
@Test
public void testRunApplyConstructQueryWithVariable() throws Exception {
Expand Down

0 comments on commit 591e5a2

Please sign in to comment.