diff --git a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/GiantModel.java b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/GiantModel.java
index d61c2abf68b7..a7c27398f0fb 100644
--- a/model-view-controller/src/main/java/com/iluwatar/model/view/controller/GiantModel.java
+++ b/model-view-controller/src/main/java/com/iluwatar/model/view/controller/GiantModel.java
@@ -32,7 +32,10 @@ public class GiantModel {
private Fatigue fatigue;
private Nourishment nourishment;
- GiantModel(Health health, Fatigue fatigue, Nourishment nourishment) {
+ /**
+ * Instantiates a new GiantModel.
+ */
+ public GiantModel(Health health, Fatigue fatigue, Nourishment nourishment) {
this.health = health;
this.fatigue = fatigue;
this.nourishment = nourishment;
diff --git a/pom.xml b/pom.xml
index 064e7c1a7eff..e74701d56132 100644
--- a/pom.xml
+++ b/pom.xml
@@ -231,6 +231,7 @@
domain-model
composite-view
metadata-mapping
+ service-to-worker
diff --git a/service-to-worker/README.md b/service-to-worker/README.md
new file mode 100644
index 000000000000..db25ac69f56a
--- /dev/null
+++ b/service-to-worker/README.md
@@ -0,0 +1,117 @@
+---
+layout: pattern
+title: Service to Worker
+folder: service-to-worker
+permalink: /patterns/service-to-worker/
+categories: Architectural
+tags:
+- Decoupling
+---
+
+## Intent
+
+Combine a controller and dispatcher with views and helpers to handle client requests and prepare a dynamic presentation as the response. Controllers delegate content retrieval to helpers, which manage the population of the intermediate model for the view. A dispatcher is responsible for view management and navigation and can be encapsulated either within a controller or a separate component.
+
+## Explanation
+
+Real world example
+
+> In the classic MVC pattern, M refers to the business model, V refers to the user interface, and C is the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same program can use different forms of expression. In the Service to Worker pattern, the C directly controls the display of the V and can receive commands to control the dispatcher indirectly. The dispatcher stores different commands that can be used to modify the `model` with `action`s or to modify the display in the `view`s.
+
+In plain words
+
+> Service to Worker Pattern uses Dispatcher to combine the controller and the view to handle client requests and prepare a dynamic presentation as the response.
+
+**Programmatic Example**
+
+We modified this pattern based on a classic design patterns [Model View Controller Pattern](https://github.com/iluwatar/java-design-patterns/tree/master/model-view-controller) as the Class Diagram and two main classes `Dispatcher` and `Action` have been added.
+
+The Dispatcher, which encapsulates worker and view selection based on request information and/or an internal navigation model.
+
+```java
+public class Dispatcher {
+
+ private final GiantView giantView;
+ private final List actions;
+
+ /**
+ * Instantiates a new Dispatcher.
+ *
+ * @param giantView the giant view
+ */
+ public Dispatcher(GiantView giantView) {
+ this.giantView = giantView;
+ this.actions = new ArrayList<>();
+ }
+
+ /**
+ * Add an action.
+ *
+ * @param action the action
+ */
+ void addAction(Action action) {
+ actions.add(action);
+ }
+
+ /**
+ * Perform an action.
+ *
+ * @param s the s
+ * @param actionIndex the action index
+ */
+ public void performAction(Command s, int actionIndex) {
+ actions.get(actionIndex).updateModel(s);
+ }
+
+ /**
+ * Update view.
+ *
+ * @param giantModel the giant model
+ */
+ public void updateView(GiantModel giantModel) {
+ giantView.displayGiant(giantModel);
+ }
+}
+```
+
+The Action (Worker), which can process user input and perform a specific update on the model.
+
+```java
+public class Action {
+
+ private final GiantModel giant;
+
+ /**
+ * Instantiates a new Action.
+ *
+ * @param giant the giant
+ */
+ public Action(GiantModel giant) {
+ this.giant = giant;
+ }
+
+ /**
+ * Update model based on command.
+ *
+ * @param command the command
+ */
+ public void updateModel(Command command) {
+ setFatigue(command.getFatigue());
+ setHealth(command.getHealth());
+ setNourishment(command.getNourishment());
+ }
+}
+```
+
+Therefore, this example leverages the Service to Worker pattern to increase functionality cohesion and improve the business logic.
+
+
+## Class diagram
+
+
+## Applicability
+- For the business logic of web development, the responsibility of a dispatcher component may be to translate the logical name login into the resource name of an appropriate view, such as login.jsp, and dispatch to that view. To accomplish this translation, the dispatcher may access resources such as an XML configuration file that specifies the appropriate view to display.
+
+## Credits
+* [J2EE Design Patterns](https://www.oreilly.com/library/view/j2ee-design-patterns/0596004273/re05.html)
+* [Core J2EE Patterns](http://corej2eepatterns.com/Patterns/ServiceToWorker.htm)
diff --git a/service-to-worker/etc/service-to-worker.png b/service-to-worker/etc/service-to-worker.png
new file mode 100644
index 000000000000..864af5b38968
Binary files /dev/null and b/service-to-worker/etc/service-to-worker.png differ
diff --git a/service-to-worker/etc/service-to-worker.puml b/service-to-worker/etc/service-to-worker.puml
new file mode 100644
index 000000000000..c96c337a8bd8
--- /dev/null
+++ b/service-to-worker/etc/service-to-worker.puml
@@ -0,0 +1,86 @@
+@startuml
+package com.iluwatar.servicetoworker {
+ class App {
+ + App()
+ + main(args : String[]) {static}
+ }
+ enum Health {
+ + DEAD {static}
+ + HEALTHY {static}
+ + WOUNDED {static}
+ - title : String
+ + toString() : String
+ }
+ enum Nourishment {
+ + HUNGRY {static}
+ + SATURATED {static}
+ + STARVING {static}
+ - title : String
+ + toString() : String
+ }
+ enum Fatigue {
+ + ALERT {static}
+ + SLEEPING {static}
+ + TIRED {static}
+ - title : String
+ + toString() : String
+ }
+
+ class GiantController {
+ - dispatcher : Dispatcher
+ + GiantController(dispatcher : Dispatcher)
+ + setCommand(s : Command, index : int)
+ + updateView(giantModel : GiantModel)
+ }
+
+ class GiantModel {
+ - fatigue : Fatigue
+ - health : Health
+ - nourishment : Nourishment
+ # GiantModel(health : Health, fatigue : Fatigue, nourishment : Nourishment)
+ + getFatigue() : Fatigue
+ + getHealth() : Health
+ + getNourishment() : Nourishment
+ + setFatigue(fatigue : Fatigue)
+ + setHealth(health : Health)
+ + setNourishment(nourishment : Nourishment)
+ + toString() : String
+ }
+ class GiantView {
+ - LOGGER : Logger {static}
+ + GiantView()
+ + displayGiant(giant : GiantModel)
+ }
+
+ class Action{
+ - giant : GiantModel
+ + Action(giant: GiantModel)
+ + updateModel(command: Command)
+ + setHealth(health : Health)
+ + setFatigue(fatigue : Fatigue)
+ + setNourishment(nourishment : Nourishment)
+ }
+
+ class Dispatcher{
+ - giantView : GiantView
+ - actions : ArrayList
+ + Dispatcher(giantView : GiantView)
+ + addAction(action: Action)
+ + performAction(s: Command, actionIndex: int)
+ + updateView(giant : GiantModel)
+ }
+}
+GiantModel --> Nourishment
+GiantModel --> Fatigue
+GiantModel --> Health
+
+GiantView ..> GiantModel
+Dispatcher o-up- Action
+GiantController -up-> Dispatcher
+
+GiantController .up.> GiantModel
+Action --> GiantModel
+Dispatcher -up-> GiantView
+Dispatcher .left.> GiantModel
+
+@enduml
\ No newline at end of file
diff --git a/service-to-worker/pom.xml b/service-to-worker/pom.xml
new file mode 100644
index 000000000000..29f7fe81f1f4
--- /dev/null
+++ b/service-to-worker/pom.xml
@@ -0,0 +1,46 @@
+
+
+
+ java-design-patterns
+ com.iluwatar
+ 1.26.0-SNAPSHOT
+
+ 4.0.0
+
+ service-to-worker
+
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ test
+
+
+ com.iluwatar
+ model-view-controller
+ 1.26.0-SNAPSHOT
+ compile
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-assembly-plugin
+
+
+
+
+
+ com.iluwatar.servicetoworker.App
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Action.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Action.java
new file mode 100644
index 000000000000..9f885757a851
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Action.java
@@ -0,0 +1,61 @@
+package com.iluwatar.servicetoworker;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+
+/**
+ * The type Action (Worker), which can process user input and perform a specific update on the
+ * model.
+ */
+public class Action {
+
+ public GiantModel giant;
+
+ /**
+ * Instantiates a new Action.
+ *
+ * @param giant the giant
+ */
+ public Action(GiantModel giant) {
+ this.giant = giant;
+ }
+
+ /**
+ * Update model based on command.
+ *
+ * @param command the command
+ */
+ public void updateModel(Command command) {
+ setFatigue(command.getFatigue());
+ setHealth(command.getHealth());
+ setNourishment(command.getNourishment());
+ }
+
+ /**
+ * Sets health.
+ *
+ * @param health the health
+ */
+ public void setHealth(Health health) {
+ giant.setHealth(health);
+ }
+
+ /**
+ * Sets fatigue.
+ *
+ * @param fatigue the fatigue
+ */
+ public void setFatigue(Fatigue fatigue) {
+ giant.setFatigue(fatigue);
+ }
+
+ /**
+ * Sets nourishment.
+ *
+ * @param nourishment the nourishment
+ */
+ public void setNourishment(Nourishment nourishment) {
+ giant.setNourishment(nourishment);
+ }
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/App.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/App.java
new file mode 100644
index 000000000000..2d41dba87196
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/App.java
@@ -0,0 +1,47 @@
+package com.iluwatar.servicetoworker;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+
+/**
+ * The front controller intercepts all requests and performs common functions using decorators. The
+ * front controller passes request information to the dispatcher, which uses the request and an
+ * internal model to chooses and execute appropriate actions. The actions process user input,
+ * translating it into appropriate updates to the model. The model applies business rules and stores
+ * the data persistently. Based on the user input and results of the actions, the dispatcher chooses
+ * a view. The view transforms the updated model data into a form suitable for the user.
+ */
+public class App {
+
+ /**
+ * Program entry point.
+ *
+ * @param args command line args
+ */
+ public static void main(String[] args) {
+ // create model, view and controller
+ var giant1 = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT, Nourishment.SATURATED);
+ var giant2 = new GiantModel("giant2", Health.DEAD, Fatigue.SLEEPING, Nourishment.STARVING);
+ var action1 = new Action(giant1);
+ var action2 = new Action(giant2);
+ var view = new GiantView();
+ var dispatcher = new Dispatcher(view);
+ dispatcher.addAction(action1);
+ dispatcher.addAction(action2);
+ var controller = new GiantController(dispatcher);
+
+ // initial display
+ controller.updateView(giant1);
+ controller.updateView(giant2);
+
+ // controller receives some interactions that affect the giant
+ controller.setCommand(new Command(Fatigue.SLEEPING, Health.HEALTHY, Nourishment.STARVING), 0);
+ controller.setCommand(new Command(Fatigue.ALERT, Health.HEALTHY, Nourishment.HUNGRY), 1);
+
+ // redisplay
+ controller.updateView(giant1);
+ controller.updateView(giant2);
+ // controller receives some interactions that affect the giant
+ }
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Command.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Command.java
new file mode 100644
index 000000000000..e58cf85cb331
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Command.java
@@ -0,0 +1,32 @@
+package com.iluwatar.servicetoworker;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import lombok.Getter;
+
+/**
+ * The type Command.
+ */
+public class Command {
+
+ @Getter
+ private final Fatigue fatigue;
+ @Getter
+ private final Health health;
+ @Getter
+ private final Nourishment nourishment;
+
+ /**
+ * Instantiates a new Command.
+ *
+ * @param fatigue the fatigue
+ * @param health the health
+ * @param nourishment the nourishment
+ */
+ public Command(Fatigue fatigue, Health health, Nourishment nourishment) {
+ this.fatigue = fatigue;
+ this.health = health;
+ this.nourishment = nourishment;
+ }
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Dispatcher.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Dispatcher.java
new file mode 100644
index 000000000000..511e08b11301
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/Dispatcher.java
@@ -0,0 +1,55 @@
+package com.iluwatar.servicetoworker;
+
+import java.util.ArrayList;
+import java.util.List;
+import lombok.Getter;
+
+/**
+ * The type Dispatcher, which encapsulates worker and view selection based on request information
+ * and/or an internal navigation model.
+ */
+public class Dispatcher {
+
+ @Getter
+ private final GiantView giantView;
+ private final List actions;
+
+ /**
+ * Instantiates a new Dispatcher.
+ *
+ * @param giantView the giant view
+ */
+ public Dispatcher(GiantView giantView) {
+ this.giantView = giantView;
+ this.actions = new ArrayList<>();
+ }
+
+ /**
+ * Add an action.
+ *
+ * @param action the action
+ */
+ void addAction(Action action) {
+ actions.add(action);
+ }
+
+ /**
+ * Perform an action.
+ *
+ * @param s the s
+ * @param actionIndex the action index
+ */
+ public void performAction(Command s, int actionIndex) {
+ actions.get(actionIndex).updateModel(s);
+ }
+
+ /**
+ * Update view.
+ *
+ * @param giantModel the giant model
+ */
+ public void updateView(GiantModel giantModel) {
+ giantView.displayGiant(giantModel);
+ }
+
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantController.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantController.java
new file mode 100644
index 000000000000..6db186004c1f
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantController.java
@@ -0,0 +1,41 @@
+package com.iluwatar.servicetoworker;
+
+import lombok.Getter;
+
+/**
+ * GiantController can update the giant data and redraw it using the view. Singleton object that
+ * intercepts all requests and performs common functions.
+ */
+public class GiantController {
+
+ public Dispatcher dispatcher;
+
+ /**
+ * Instantiates a new Giant controller.
+ *
+ * @param dispatcher the dispatcher
+ */
+ public GiantController(Dispatcher dispatcher) {
+ this.dispatcher = dispatcher;
+ }
+
+ /**
+ * Sets command to control the dispatcher.
+ *
+ * @param s the s
+ * @param index the index
+ */
+ public void setCommand(Command s, int index) {
+ dispatcher.performAction(s, index);
+ }
+
+ /**
+ * Update view. This is a simple implementation, in fact, View can be implemented in a concrete
+ * way
+ *
+ * @param giantModel the giant model
+ */
+ public void updateView(GiantModel giantModel) {
+ dispatcher.updateView(giantModel);
+ }
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantModel.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantModel.java
new file mode 100644
index 000000000000..e0e67997588c
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantModel.java
@@ -0,0 +1,86 @@
+package com.iluwatar.servicetoworker;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import lombok.Getter;
+
+/**
+ * GiantModel contains the giant data.
+ */
+public class GiantModel {
+
+ private final com.iluwatar.model.view.controller.GiantModel model;
+ @Getter
+ private final String name;
+
+ /**
+ * Instantiates a new Giant model.
+ *
+ * @param name the name
+ * @param health the health
+ * @param fatigue the fatigue
+ * @param nourishment the nourishment
+ */
+ GiantModel(String name, Health health, Fatigue fatigue, Nourishment nourishment) {
+ this.name = name;
+ this.model = new com.iluwatar.model.view.controller.GiantModel(health, fatigue,
+ nourishment);
+ }
+
+ /**
+ * Gets health.
+ *
+ * @return the health
+ */
+ Health getHealth() {
+ return model.getHealth();
+ }
+
+ /**
+ * Sets health.
+ *
+ * @param health the health
+ */
+ void setHealth(Health health) {
+ model.setHealth(health);
+ }
+
+ /**
+ * Gets fatigue.
+ *
+ * @return the fatigue
+ */
+ Fatigue getFatigue() {
+ return model.getFatigue();
+ }
+
+ void setFatigue(Fatigue fatigue) {
+ model.setFatigue(fatigue);
+ }
+
+ /**
+ * Gets nourishment.
+ *
+ * @return the nourishment
+ */
+ Nourishment getNourishment() {
+ return model.getNourishment();
+ }
+
+ /**
+ * Sets nourishment.
+ *
+ * @param nourishment the nourishment
+ */
+ void setNourishment(Nourishment nourishment) {
+ model.setNourishment(nourishment);
+ }
+
+ @Override
+ public String toString() {
+ return String
+ .format("Giant %s, The giant looks %s, %s and %s.", name,
+ model.getHealth(), model.getFatigue(), model.getNourishment());
+ }
+}
diff --git a/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantView.java b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantView.java
new file mode 100644
index 000000000000..cebe03a94ce5
--- /dev/null
+++ b/service-to-worker/src/main/java/com/iluwatar/servicetoworker/GiantView.java
@@ -0,0 +1,19 @@
+package com.iluwatar.servicetoworker;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * GiantView displays the giant.
+ */
+@Slf4j
+public class GiantView {
+
+ /**
+ * Display the GiantModel simply.
+ *
+ * @param giant the giant
+ */
+ public void displayGiant(GiantModel giant) {
+ LOGGER.info(giant.toString());
+ }
+}
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/ActionTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/ActionTest.java
new file mode 100644
index 000000000000..7c332f522426
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/ActionTest.java
@@ -0,0 +1,87 @@
+package com.iluwatar.servicetoworker;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The type Action test.
+ */
+class ActionTest {
+
+ /**
+ * Verify if the health value is set properly though the constructor and setter
+ */
+ @Test
+ void testSetHealth() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ assertEquals(Health.HEALTHY, model.getHealth());
+ var messageFormat = "Giant giant1, The giant looks %s, alert and saturated.";
+ for (final var health : Health.values()) {
+ action.setHealth(health);
+ assertEquals(health, model.getHealth());
+ assertEquals(String.format(messageFormat, health), model.toString());
+ }
+ }
+
+ /**
+ * Verify if the fatigue level is set properly though the constructor and setter
+ */
+ @Test
+ void testSetFatigue() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ assertEquals(Fatigue.ALERT, model.getFatigue());
+ var messageFormat = "Giant giant1, The giant looks healthy, %s and saturated.";
+ for (final var fatigue : Fatigue.values()) {
+ action.setFatigue(fatigue);
+ assertEquals(fatigue, model.getFatigue());
+ assertEquals(String.format(messageFormat, fatigue), model.toString());
+ }
+ }
+
+ /**
+ * Verify if the nourishment level is set properly though the constructor and setter
+ */
+ @Test
+ void testSetNourishment() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ assertEquals(Nourishment.SATURATED, model.getNourishment());
+ var messageFormat = "Giant giant1, The giant looks healthy, alert and %s.";
+ for (final var nourishment : Nourishment.values()) {
+ action.setNourishment(nourishment);
+ assertEquals(nourishment, model.getNourishment());
+ assertEquals(String.format(messageFormat, nourishment), model.toString());
+ }
+ }
+
+ /**
+ * Test update model.
+ */
+ @Test
+ void testUpdateModel() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ assertEquals(Nourishment.SATURATED, model.getNourishment());
+ for (final var nourishment : Nourishment.values()) {
+ for (final var fatigue : Fatigue.values()) {
+ for (final var health : Health.values()) {
+ Command cmd = new Command(fatigue, health, nourishment);
+ action.updateModel(cmd);
+ assertEquals(nourishment, model.getNourishment());
+ assertEquals(fatigue, model.getFatigue());
+ assertEquals(health, model.getHealth());
+ }
+ }
+ }
+ }
+}
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/AppTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/AppTest.java
new file mode 100644
index 000000000000..6611b0ab388d
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/AppTest.java
@@ -0,0 +1,16 @@
+package com.iluwatar.servicetoworker;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+/**
+ * Application test
+ */
+class AppTest {
+
+ @Test
+ void shouldExecuteApplicationWithoutException() {
+ assertDoesNotThrow(() -> App.main(new String[]{}));
+ }
+}
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/DispatcherTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/DispatcherTest.java
new file mode 100644
index 000000000000..d8f6063db24e
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/DispatcherTest.java
@@ -0,0 +1,52 @@
+package com.iluwatar.servicetoworker;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The type Dispatcher test.
+ */
+class DispatcherTest {
+
+ /**
+ * Test perform action.
+ */
+ @Test
+ void testPerformAction() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ GiantView giantView = new GiantView();
+ Dispatcher dispatcher = new Dispatcher(giantView);
+ assertEquals(Nourishment.SATURATED, model.getNourishment());
+ dispatcher.addAction(action);
+ for (final var nourishment : Nourishment.values()) {
+ for (final var fatigue : Fatigue.values()) {
+ for (final var health : Health.values()) {
+ Command cmd = new Command(fatigue, health, nourishment);
+ dispatcher.performAction(cmd, 0);
+ assertEquals(nourishment, model.getNourishment());
+ assertEquals(fatigue, model.getFatigue());
+ assertEquals(health, model.getHealth());
+ }
+ }
+ }
+ }
+
+ @Test
+ void testUpdateView() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ GiantView giantView = new GiantView();
+ Dispatcher dispatcher = new Dispatcher(giantView);
+ assertDoesNotThrow(() -> dispatcher.updateView(model));
+ }
+}
+
+
+
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantControllerTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantControllerTest.java
new file mode 100644
index 000000000000..e9a982ce9454
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantControllerTest.java
@@ -0,0 +1,49 @@
+package com.iluwatar.servicetoworker;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import org.junit.jupiter.api.Test;
+
+
+/**
+ * The type Giant controller test.
+ */
+class GiantControllerTest {
+
+ /**
+ * Test set command.
+ */
+ @Test
+ void testSetCommand() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ Action action = new Action(model);
+ GiantView giantView = new GiantView();
+ Dispatcher dispatcher = new Dispatcher(giantView);
+ assertEquals(Nourishment.SATURATED, model.getNourishment());
+ dispatcher.addAction(action);
+ GiantController controller = new GiantController(dispatcher);
+ controller.setCommand(new Command(Fatigue.ALERT, Health.HEALTHY, Nourishment.HUNGRY), 0);
+ assertEquals(Fatigue.ALERT, model.getFatigue());
+ assertEquals(Health.HEALTHY, model.getHealth());
+ assertEquals(Nourishment.HUNGRY, model.getNourishment());
+ }
+
+ /**
+ * Test update view.
+ */
+ @Test
+ void testUpdateView() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ GiantView giantView = new GiantView();
+ Dispatcher dispatcher = new Dispatcher(giantView);
+ GiantController giantController = new GiantController(dispatcher);
+ assertDoesNotThrow(() -> giantController.updateView(model));
+ }
+
+}
\ No newline at end of file
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantModelTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantModelTest.java
new file mode 100644
index 000000000000..d2416701dd54
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantModelTest.java
@@ -0,0 +1,62 @@
+package com.iluwatar.servicetoworker;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The type Giant model test.
+ */
+class GiantModelTest {
+
+ /**
+ * Verify if the health value is set properly though the constructor and setter
+ */
+ @Test
+ void testSetHealth() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ assertEquals(Health.HEALTHY, model.getHealth());
+ var messageFormat = "Giant giant1, The giant looks %s, alert and saturated.";
+ for (final var health : Health.values()) {
+ model.setHealth(health);
+ assertEquals(health, model.getHealth());
+ assertEquals(String.format(messageFormat, health), model.toString());
+ }
+ }
+
+ /**
+ * Verify if the fatigue level is set properly though the constructor and setter
+ */
+ @Test
+ void testSetFatigue() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ assertEquals(Fatigue.ALERT, model.getFatigue());
+ var messageFormat = "Giant giant1, The giant looks healthy, %s and saturated.";
+ for (final var fatigue : Fatigue.values()) {
+ model.setFatigue(fatigue);
+ assertEquals(fatigue, model.getFatigue());
+ assertEquals(String.format(messageFormat, fatigue), model.toString());
+ }
+ }
+
+ /**
+ * Verify if the nourishment level is set properly though the constructor and setter
+ */
+ @Test
+ void testSetNourishment() {
+ final var model = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ assertEquals(Nourishment.SATURATED, model.getNourishment());
+ var messageFormat = "Giant giant1, The giant looks healthy, alert and %s.";
+ for (final var nourishment : Nourishment.values()) {
+ model.setNourishment(nourishment);
+ assertEquals(nourishment, model.getNourishment());
+ assertEquals(String.format(messageFormat, nourishment), model.toString());
+ }
+ }
+}
diff --git a/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantViewTest.java b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantViewTest.java
new file mode 100644
index 000000000000..a33adf6198c6
--- /dev/null
+++ b/service-to-worker/src/test/java/com/iluwatar/servicetoworker/GiantViewTest.java
@@ -0,0 +1,25 @@
+package com.iluwatar.servicetoworker;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+
+import com.iluwatar.model.view.controller.Fatigue;
+import com.iluwatar.model.view.controller.Health;
+import com.iluwatar.model.view.controller.Nourishment;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The type Giant view test.
+ */
+class GiantViewTest {
+
+ /**
+ * Test dispaly giant.
+ */
+ @Test
+ void testDispalyGiant() {
+ GiantModel giantModel = new GiantModel("giant1", Health.HEALTHY, Fatigue.ALERT,
+ Nourishment.SATURATED);
+ GiantView giantView = new GiantView();
+ assertDoesNotThrow(() -> giantView.displayGiant(giantModel));
+ }
+}