Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add online generator tests #1759

Merged
merged 1 commit into from
Dec 28, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.openapitools.codegen.online.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openapitools.codegen.online.model.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
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.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@WebMvcTest(GenApiController.class)
public class GenApiControllerTest {

private static final String OPENAPI_URL = "http://petstore.swagger.io/v2/swagger.json";
private static final String UUID_REGEX = "[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[89aAbB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}";

@Autowired
private MockMvc mockMvc;


@Test
public void clientLanguages() throws Exception {
getLanguages("clients", "java");
}

@Test
public void serverFrameworks() throws Exception {
getLanguages("servers", "spring");
}


public void getLanguages(String type, String expected) throws Exception {
mockMvc.perform(get("/api/gen/" + type))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.[*]").value(hasItem(expected)));
}

@Test
public void clientOptions() throws Exception {
getOptions("clients", "java");
}

@Test
public void clientOptionsUnknown() throws Exception {
mockMvc.perform(get("/api/gen/clients/unknown"))
.andExpect(status().isNotFound());
}

@Test
public void serverOptions() throws Exception {
getOptions("servers", "spring");
}

@Test
public void serverOptionsUnknown() throws Exception {
mockMvc.perform(get("/api/gen/servers/unknown"))
.andExpect(status().isNotFound());
}

private void getOptions(String type, String name) throws Exception {
mockMvc.perform(get("/api/gen/" + type + "/" + name))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.sortParamsByRequiredFlag.opt").value("sortParamsByRequiredFlag"));
}

@Test
public void generateClient() throws Exception {
generateAndDownload("clients", "java");
}

@Test
public void generateServer() throws Exception {
generateAndDownload("servers", "spring");
}

private void generateAndDownload(String type, String name) throws Exception {
String result = mockMvc.perform(post("http://test.com:1234/api/gen/" + type + "/" + name)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.code").value(matchesPattern(UUID_REGEX)))
.andExpect(jsonPath("$.link").value(matchesPattern("http\\:\\/\\/test.com\\:1234\\/api\\/gen\\/download\\/" + UUID_REGEX)))
.andReturn().getResponse().getContentAsString();

String code = new ObjectMapper().readValue(result, ResponseCode.class).getCode();

mockMvc.perform(get("http://test.com:1234/api/gen/download/" + code))
.andExpect(content().contentType("application/zip"))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_LENGTH, not(0)));
}

@Test
public void generateWIthForwardedHeaders() throws Exception {
String result = mockMvc.perform(post("http://test.com:1234/api/gen/clients/java")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.header("X-Forwarded-Proto", "https")
.header("X-Forwarded-Host", "forwarded.com")
.header("X-Forwarded-Port", "5678")
.content("{\"openAPIUrl\": \"" + OPENAPI_URL + "\"}"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.code").value(matchesPattern(UUID_REGEX)))
.andExpect(jsonPath("$.link").value(matchesPattern("https\\:\\/\\/forwarded.com\\:5678\\/api\\/gen\\/download\\/" + UUID_REGEX)))
.andReturn().getResponse().getContentAsString();

String code = new ObjectMapper().readValue(result, ResponseCode.class).getCode();

mockMvc.perform(get("http://test.com:1234/api/gen/download/" + code))
.andExpect(content().contentType("application/zip"))
.andExpect(status().isOk())
.andExpect(header().string(HttpHeaders.CONTENT_LENGTH, not(0)));
}

}