need to add tests

This commit is contained in:
maxnes3 2023-02-20 16:38:27 +04:00
parent 04f358067f
commit 51b4b1b53e
11 changed files with 237 additions and 27 deletions

View File

@ -0,0 +1,13 @@
package ru.ip.labworks.labworks.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

View File

@ -1,37 +1,35 @@
package ru.ip.labworks.labworks.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import ru.ip.labworks.labworks.service.CalculatorService;
@RestController
public class UserController {
private CalculatorService calculatorService;
@GetMapping("/sub")
public String workSub(HttpServletResponse response, @RequestParam String str1, @RequestParam String str2){
response.addHeader("Access-Control-Allow-Origin", "*");
return str1.substring(Integer.parseInt(str2));
public UserController(CalculatorService calculatorService){
this.calculatorService = calculatorService;
}
@GetMapping("/split")
public String workSplit(HttpServletResponse response, @RequestParam String str1, @RequestParam String str2){
response.addHeader("Access-Control-Allow-Origin", "*");
String[] array = str1.split(str2);
return String.format(String.join(" ", array));
@GetMapping("/+")
public String workPlus(@RequestParam String type, @RequestParam Object arg1, @RequestParam Object arg2){
return (String) calculatorService.Plus(type, arg1, arg2);
}
@GetMapping("/uplow")
public String workUpper(HttpServletResponse response, @RequestParam String str1, @RequestParam String str2){
response.addHeader("Access-Control-Allow-Origin", "*");
return String.format(str1.toUpperCase() + str2.toLowerCase());
@GetMapping("/-")
public String workMinus(@RequestParam String type, @RequestParam Object arg1, @RequestParam Object arg2){
return (String) calculatorService.Minus(type, arg1, arg2);
}
@GetMapping("/concat")
public String workCompare(HttpServletResponse response, @RequestParam String str1, @RequestParam String str2){
response.addHeader("Access-Control-Allow-Origin", "*");
return String.format(str1.concat(str2));
@GetMapping("/*")
public String workMulti(@RequestParam String type, @RequestParam Object arg1, @RequestParam Object arg2){
return (String) calculatorService.Multi(type, arg1, arg2);
}
@GetMapping("/:")
public String workDiv(@RequestParam String type, @RequestParam Object arg1, @RequestParam Object arg2){
return (String) calculatorService.Div(type, arg1, arg2);
}
}

View File

@ -0,0 +1,42 @@
package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component;
@Component(value = "array")
public class ArrayCalculator implements ITypeCalculator{
@Override
public Object Plus(Object arg1, Object arg2) {
String[] result = arg1.toString().split(",");
for (int i = 0; i < result.length; ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) + Integer.parseInt(arg2.toString()));
}
return String.join(", ", result);
}
@Override
public Object Minus(Object arg1, Object arg2) {
String[] result = arg1.toString().split(",");
for (int i = 0; i < result.length; ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) - Integer.parseInt(arg2.toString()));
}
return String.join(", ", result);
}
@Override
public Object Multi(Object arg1, Object arg2) {
String[] result = arg1.toString().split(",");
for (int i = 0; i < result.length; ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) * Integer.parseInt(arg2.toString()));
}
return String.join(", ", result);
}
@Override
public Object Div(Object arg1, Object arg2) {
String[] result = arg1.toString().split(",");
for (int i = 0; i < result.length; ++i){
result[i] = Integer.toString(Integer.parseInt(result[i]) / Integer.parseInt(arg2.toString()));
}
return String.join(", ", result);
}
}

View File

@ -0,0 +1,9 @@
package ru.ip.labworks.labworks.domain;
public interface ITypeCalculator {
Object Plus(Object arg1, Object arg2);
Object Minus(Object arg1, Object arg2);
Object Multi(Object arg1, Object arg2);
Object Div(Object arg1, Object arg2);
}

View File

@ -0,0 +1,30 @@
package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component;
@Component(value = "int")
public class IntCalculator implements ITypeCalculator{
@Override
public Object Plus(Object arg1, Object arg2) {
int result = Integer.parseInt(arg1.toString()) + Integer.parseInt(arg2.toString());
return Integer.toString(result);
}
@Override
public Object Minus(Object arg1, Object arg2) {
int result = Integer.parseInt(arg1.toString()) - Integer.parseInt(arg2.toString());
return Integer.toString(result);
}
@Override
public Object Multi(Object arg1, Object arg2) {
int result = Integer.parseInt(arg1.toString()) * Integer.parseInt(arg2.toString());
return Integer.toString(result);
}
@Override
public Object Div(Object arg1, Object arg2) {
int result = Integer.parseInt(arg1.toString()) / Integer.parseInt(arg2.toString());
return Integer.toString(result);
}
}

View File

@ -0,0 +1,31 @@
package ru.ip.labworks.labworks.domain;
import org.springframework.stereotype.Component;
@Component(value = "string")
public class StringCalculator implements ITypeCalculator{
@Override
public Object Plus(Object arg1, Object arg2) {
return arg1.toString().concat(arg2.toString());
}
@Override
public Object Minus(Object arg1, Object arg2) {
return arg1.toString().replaceAll(arg2.toString(), "");
}
@Override
public Object Multi(Object arg1, Object arg2) {
String result = "";
for (int i = 0; i < Integer.parseInt(arg2.toString()); ++i){
result += arg1.toString();
}
return result;
}
@Override
public Object Div(Object arg1, Object arg2) {
String result = arg1.toString().substring(0, arg1.toString().length() / Integer.parseInt(arg2.toString()));
return result;
}
}

View File

@ -0,0 +1,34 @@
package ru.ip.labworks.labworks.service;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import ru.ip.labworks.labworks.domain.ITypeCalculator;
@Service
public class CalculatorService {
private final ApplicationContext applicationContext;
public CalculatorService(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
public Object Plus(String type, Object arg1, Object arg2){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type);
return calculator.Plus(arg1, arg2);
}
public Object Minus(String type, Object arg1, Object arg2){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type);
return calculator.Minus(arg1, arg2);
}
public Object Multi(String type, Object arg1, Object arg2){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type);
return calculator.Multi(arg1, arg2);
}
public Object Div(String type, Object arg1, Object arg2){
final ITypeCalculator calculator = (ITypeCalculator)applicationContext.getBean(type);
return calculator.Div(arg1, arg2);
}
}

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<title>String Compiler</title>
<title>Data Type Calculator</title>
</head>
<body>
<div id="app"></div>

View File

@ -3,7 +3,7 @@
<nav class="navbar navbar-expand-lg bg-primary" data-bs-theme="dark">
<div class="container">
<a class="navbar-brand" href="#">
<strong>{ S } Strings Compiler</strong>
<strong>{ DTC } Data Type Calculator</strong>
</a>
</div>
</nav>

View File

@ -9,6 +9,7 @@ export default{
data(){
return {
action: "",
type: "",
str_1: "",
str_2: "",
result: ""
@ -19,8 +20,8 @@ export default{
if (this.action.length == 0 || this.str_1.length == 0 || this.str_2.length == 0){
console.warn("Invalid input to fetch!");
}
console.log("http://localhost:8080/" + this.action + "?str1=" + this.str_1 + "&str2=" + this.str_2)
fetch("http://localhost:8080/" + this.action + "?str1=" + this.str_1 + "&str2=" + this.str_2)
console.log("http://localhost:8080/" + this.action + "?type=" + this.type + "&arg1=" + this.str_1 + "&arg2=" + this.str_2)
fetch("http://localhost:8080/" + this.action + "?type=" + this.type + "&arg1=" + this.str_1 + "&arg2=" + this.str_2)
.then(res => res.text())
.then(res => {
this.result = res;
@ -38,11 +39,15 @@ export default{
<input type="text" class="form-control" placeholder="Выберите действие" v-model="action">
</div>
<div class="input-group mb-2">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Строка1:</strong></span>
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Тип данных:</strong></span>
<input type="text" class="form-control" placeholder="Введите тип данных" v-model="type">
</div>
<div class="input-group mb-2">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Аргумент1:</strong></span>
<input type="text" class="form-control" placeholder="Введите превую строку" v-model="str_1">
</div>
<div class="input-group mb-2">
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Строка2:</strong></span>
<span class="input-group-text bg-primary text-white" id="basic-addon1"><strong>Аргумент2:</strong></span>
<input type="text" class="form-control" placeholder="Введите вторую строку" v-model="str_2">
</div>
<div class="input-group mb-2">

View File

@ -1,13 +1,61 @@
package ru.ip.labworks.labworks;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import ru.ip.labworks.labworks.service.CalculatorService;
@SpringBootTest
class LabworksApplicationTests {
@Autowired
CalculatorService calculatorService;
@Test
void contextLoads() {
void IntPlus() {
final String res = calculatorService.Plus("int", 10, 2).toString();
Assertions.assertEquals("12", res);
}
@Test
void IntMinus() {
final String res = calculatorService.Minus("int", 10, 2).toString();
Assertions.assertEquals("8", res);
}
@Test
void IntMulti() {
final String res = calculatorService.Multi("int", 10, 2).toString();
Assertions.assertEquals("20", res);
}
@Test
void IntDiv() {
final String res = calculatorService.Div("int", 10, 2).toString();
Assertions.assertEquals("5", res);
}
@Test
void StrPlus() {
final String res = calculatorService.Plus("string", "10", "2").toString();
Assertions.assertEquals("102", res);
}
@Test
void StrMinus() {
final String res = calculatorService.Minus("string", 10, 2).toString();
Assertions.assertEquals("8", res);
}
@Test
void StrMulti() {
final String res = calculatorService.Multi("string", 10, 2).toString();
Assertions.assertEquals("20", res);
}
@Test
void StrDiv() {
final String res = calculatorService.Div("string", 10, 2).toString();
Assertions.assertEquals("5", res);
}
}