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

Create service to plan tasks. #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions src/main/java/com/example/demo/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.io.Writer;
import java.net.ServerSocket;
import java.net.Socket;

import com.example.demo.router.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -27,12 +29,12 @@ public void start() throws IOException {
BufferedReader serverReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
Writer serverWriter = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream()));
new OutputStreamWriter(connection.getOutputStream()))
) {
String line = serverReader.readLine();
LOG.debug("Request captured: " + line);
// В реализации по умолчанию в ответе пишется та же строка, которая пришла в запросе
serverWriter.write(line);
serverWriter.write(Router.logicApp(line));
serverWriter.flush();
}
} catch (Exception e) {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/example/demo/commands/Create.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.demo.commands;

import com.example.demo.dto.Data;
import com.example.demo.dto.Request;
import com.example.demo.dto.Status;
import com.example.demo.router.Router;

import java.util.ArrayList;
import java.util.List;

public class Create {
public static String createTask(Request request) {
if (Data.checkAvailabilityTask(request.getArg())) {
return Router.ERROR;
}
List<String> tasks = new ArrayList<>();
if (Data.getUsersTaskData().containsKey(request.getUser())) {
tasks = Data.getUsersTaskData().get(request.getUser());
}

tasks.add(request.getArg());
Data.getUsersTaskData().put(request.getUser(), tasks);
Data.getTaskStatusData().put(request.getArg(), Status.CREATED);
return Router.CREATED;
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/example/demo/commands/Delete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.demo.commands;

import com.example.demo.dto.Data;
import com.example.demo.dto.Request;
import com.example.demo.dto.Status;
import com.example.demo.router.Router;

import java.util.HashMap;
import java.util.List;

public class Delete {
public static String deleteTask(Request request) {
if (!Data.checkAvailabilityTask(request.getArg())) {
return Router.ERROR;
}
if (Data.checkPermissionTask(request.getUser(), request.getArg())) {
return Router.ACCESS_DENIED;
}
if (Data.getTaskStatusData().get(request.getArg()).equals(Status.CREATED)) {
return Router.ERROR;
}
Data.getTaskStatusData().remove(request.getArg());
List<String> tasks = Data.getUsersTaskData().get(request.getUser());
tasks.remove(request.getArg());
Data.getUsersTaskData().put(request.getUser(), tasks);
return Router.DELETED;
}

public static String deleteAll() {
Data.setUsersTaskData(new HashMap<>());
Data.setTaskStatusData(new HashMap<>());
return Router.DELETED;
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/example/demo/commands/Get.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.demo.commands;

import com.example.demo.dto.Data;
import com.example.demo.dto.Request;
import com.example.demo.router.Router;

public class Get {
public static String getListTask(Request request) {
if (Data.getUsersTaskData().containsKey(request.getArg())) {
return "TASKS" + " " + Data.getUsersTaskData().get(request.getArg()).toString();
} else {
return Router.ERROR;
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/example/demo/commands/Update.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.demo.commands;

import com.example.demo.dto.Data;
import com.example.demo.dto.Request;
import com.example.demo.dto.Status;
import com.example.demo.router.Router;

public class Update {
public static String closeTask(Request request) {
if (!Data.checkAvailabilityTask(request.getArg())) {
return Router.ERROR;
}
if (Data.checkPermissionTask(request.getUser(), request.getArg())) {
return Router.ACCESS_DENIED;
}
Data.getTaskStatusData().put(request.getArg(), Status.CLOSED);
return Router.CLOSED;
}

public static String reOpenTask(Request request) {
if (!Data.checkAvailabilityTask(request.getArg())) {
return Router.ERROR;
}
if (Data.checkPermissionTask(request.getUser(), request.getArg())) {
return Router.ACCESS_DENIED;
}
if (Data.getTaskStatusData().get(request.getArg()).equals(Status.CREATED)) {
return Router.ERROR;
}
Data.getTaskStatusData().put(request.getArg(), Status.CREATED);
return Router.REOPENED;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/example/demo/dto/Data.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.demo.dto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Data {

public static Map<String, Status> taskStatusData = new HashMap<>();
public static Map<String, List<String>> usersTaskData = new HashMap<>();

public static Boolean checkAvailabilityTask(String nameTask) {
try {
return taskStatusData.containsKey(nameTask);
} catch (NullPointerException ex) {
return false;
}
}

public static Boolean checkPermissionTask(String user, String task) {
List<String> tasks = Data.getUsersTaskData().get(user);
try {
return !tasks.contains(task);
} catch (NullPointerException ex) {
return true;
}
}

public static Map<String, Status> getTaskStatusData() {
return taskStatusData;
}

public static Map<String, List<String>> getUsersTaskData() {
return usersTaskData;
}

public static void setTaskStatusData(Map<String, Status> taskStatusData) {
Data.taskStatusData = taskStatusData;
}

public static void setUsersTaskData(Map<String, List<String>> usersTaskData) {
Data.usersTaskData = usersTaskData;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/example/demo/dto/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.demo.dto;

import com.example.demo.router.Router;

public class Request {

private String user;
private String command;
private String arg;
private Boolean validate = true;

public Request(String fullCommand) {
String[] arrayFullString = fullCommand.split(" ");
try {
switch (arrayFullString[0]) {
case (Router.__DELETE_ALL):
command = arrayFullString[0];
if (arrayFullString.length > 1) validate = false;
break;
case (Router.LIST_TASK): {
command = arrayFullString[0];
arg = arrayFullString[1];
if (arrayFullString.length > 2) validate = false;
break;
}
default: {
user = arrayFullString[0];
command = arrayFullString[1];
arg = arrayFullString[2];
if ((command.equals(Router.__DELETE_ALL))) validate = false;
if (arrayFullString.length > 3) validate = false;
}
}
} catch (Exception ex) {
validate = false;
}
}

public String getUser() {
return user;
}

public String getCommand() {
return command;
}

public String getArg() {
return arg;
}

public Boolean getValidate() {
return validate;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/demo/dto/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo.dto;

public enum Status {
CREATED,
CLOSED
}
57 changes: 57 additions & 0 deletions src/main/java/com/example/demo/router/Router.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.demo.router;

import com.example.demo.commands.Create;
import com.example.demo.commands.Delete;
import com.example.demo.commands.Get;
import com.example.demo.commands.Update;
import com.example.demo.dto.Request;

public class Router {

public final static String CREATE_TASK = "CREATE_TASK";
public final static String CLOSE_TASK = "CLOSE_TASK";
public final static String DELETE_TASK = "DELETE_TASK";
public final static String REOPEN_TASK = "REOPEN_TASK";
public final static String LIST_TASK = "LIST_TASK";
public final static String __DELETE_ALL = "__DELETE_ALL";

public final static String CREATED = "CREATED";
public final static String DELETED = "DELETED";
public final static String CLOSED = "CLOSED";
public final static String REOPENED = "REOPENED";

public final static String WRONG_FORMAT = "WRONG_FORMAT";
public final static String ACCESS_DENIED = "ACCESS_DENIED";
public final static String ERROR = "ERROR";

public static String logicApp(String fullCommand) {
Request request = new Request(fullCommand);
if (request.getValidate()) {
switch (request.getCommand()) {
case CREATE_TASK: {
return Create.createTask(request);
}
case CLOSE_TASK: {
return Update.closeTask(request);
}
case DELETE_TASK: {
return Delete.deleteTask(request);
}
case REOPEN_TASK: {
return Update.reOpenTask(request);
}
case LIST_TASK: {
return Get.getListTask(request);
}
case __DELETE_ALL: {
return Delete.deleteAll();
}
default:
return Router.WRONG_FORMAT;
}
} else {
return Router.WRONG_FORMAT;
}
}

}
30 changes: 30 additions & 0 deletions src/test/java/com/example/demo/ServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import java.util.List;
import java.util.stream.Stream;

import com.example.demo.dto.Request;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand Down Expand Up @@ -221,6 +224,29 @@ void shouldFailIfUserHasNoRightsToDeleteTask(String user, String task) {
assertListTask(user, List.of(task));
}

/**
* Тест на получение задач не созданного пользователя
*/
@Test
void shouldFailIfUserWithoutTasks() {
assertEquals("ERROR", getTasks("errorUser"));
}

/**
* unittest на проверку присвоение данных класса Request
*/
@ParameterizedTest
@CsvSource({
"__DELETE_ALL user",
"user __DELETE_ALL USER",
"user CREATE_TASK task task",
"CREATE_TASK"
})
void shouldFailClassRequest(String fullCommand) {
Request request = new Request(fullCommand);
Assertions.assertFalse(request.getValidate());
}

private String createTask(String user, String task) {
return sendMessage(format("%s CREATE_TASK %s", user, task));
}
Expand All @@ -237,6 +263,10 @@ private String deleteTask(String user, String task) {
return sendMessage(format("%s DELETE_TASK %s", user, task));
}

private String getTasks(String user) {
return sendMessage(format("LIST_TASK %s", user));
}

private void assertListTask(String user, List<String> expectedTasks) {
String listTaskResponse = sendMessage(user + " LIST_TASK " + user);
assertEquals(format("TASKS %s", expectedTasks), listTaskResponse);
Expand Down