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

SEE-3: Implement hyperrectangle volume computation #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 54 additions & 0 deletions src/main/java/ch/ipt/see/playground/MathController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.ipt.see.playground;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
public class MathController {
private final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

@PostMapping(
path = "/math/area_computation/hyperrectangle",
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved
consumes = MediaType.APPLICATION_XML_VALUE,
produces = MediaType.APPLICATION_XML_VALUE
)
public ResponseEntity<String> calculateRectangleArea(@RequestBody String input) {
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved
Document document;
try {
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
document = documentBuilder.parse(new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)));
} catch (ParserConfigurationException | SAXException | IOException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("<error>Could not parse input</error>");
}

NodeList sideElements = document.getElementsByTagName("side");
int numberOfSides = sideElements.getLength();

if (numberOfSides < 2) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("<error>Needs at least two sides</error>");
}

Long area = null;
for (int i = 0; i < numberOfSides; i++) {
Element sideElement = (Element) sideElements.item(i);
Long length = Long.valueOf(sideElement.getAttribute("length"));
area = area == null ? length : area * length;
}
return ResponseEntity.ok("<calculation><area>"+area+"</area></calculation>");
}
}
40 changes: 40 additions & 0 deletions src/test/java/ch/ipt/see/playground/MathControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ch.ipt.see.playground;
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import 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;

@SpringBootTest
@AutoConfigureMockMvc
class MathControllerTest {

@Autowired
private MockMvc mvc;

@Test
void calculateRectangleArea_twoDimensional() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/math/area_computation/hyperrectangle")
.content("<calculation><side length=\"4\"/><side length=\"8\"/></calculation>")
.contentType(MediaType.APPLICATION_XML)
)
.andExpect(status().isOk())
.andExpect(content().xml("<calculation><area>32</area></calculation>"));
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void calculateRectangleArea_threeDimensional() throws Exception {
mvc.perform(MockMvcRequestBuilders.post("/math/area_computation/hyperrectangle")
.content("<calculation><side length=\"4\"/><side length=\"8\"/><side length=\"2\"/></calculation>")
.contentType(MediaType.APPLICATION_XML)
)
.andExpect(status().isOk())
.andExpect(content().xml("<calculation><area>64</area></calculation>"));
}
gill-bates-1337 marked this conversation as resolved.
Show resolved Hide resolved
}