Просмотр исходного кода

cleanup , add tests , add 'return to start' button

master
DemiSel 2 лет назад
Родитель
Сommit
4763e97044

+ 38
- 38
backend/src/main/java/Application.java Просмотреть файл

@@ -12,25 +12,44 @@ public class Application {
12 12
 
13 13
     public static void main(String [] args)
14 14
     {
15
-        Trait vExtrovertTrait = new Trait("Extrovert", "You're an extrovert");
16
-        Trait vIntrovertTrait = new Trait("Introvert", "You're an introvert");
17
-        Trait vDogTrait = new Trait("Dog", "You're a dog");
15
+
16
+        Questionnaire vQuestionnaire = getQuestionnaire();
17
+        //------------------------------------
18
+        // Start server
19
+        //------------------------------------
20
+        try {
21
+            HttpServer vServer = HttpServer.create(new InetSocketAddress(8080), 0);
22
+            vServer.createContext("/", new ResponseHandler());
23
+            vServer.setExecutor(null);
24
+            vServer.start();
25
+        }
26
+        catch(Exception ve)
27
+        {
28
+            ve.printStackTrace();
29
+        }
30
+    }
31
+
32
+    public static Questionnaire getQuestionnaire()
33
+    {
34
+        Trait vExtrovertTrait = new Trait("Extrovert", "You're an extrovert.");
35
+        Trait vIntrovertTrait = new Trait("Introvert", "You're an introvert.");
36
+        Trait vDogTrait = new Trait("Dog", "You're a dog that appears to know how to read.");
18 37
 
19 38
         //------------------------------------
20 39
         // Define questionnaire
21 40
         //------------------------------------
22
-        Questionnaire vQuestionnaire = new Questionnaire(
41
+        return new Questionnaire(
23 42
                 "Introvert or extrovert ?",
24 43
                 Arrays.asList(
25 44
                         //------------------------------------
26 45
                         // First question
27 46
                         //------------------------------------
28 47
                         new Question(
29
-                                "You're really busy at work and a colleague is telling you their life story and personal woes.",
48
+                                "You're really busy at work and a colleague is telling you their life story and personal woes, you :",
30 49
                                 Arrays.asList(
31 50
                                         new Answer("Don't dare to interrupt them", vIntrovertTrait, 0.5f),
32
-                                        new Answer("Think it's more important to give them some of your time; work can wait", vIntrovertTrait, 0.2f),
33
-                                        new Answer("Listen, but with only with half an ear ", vIntrovertTrait, 0.1f),
51
+                                        new Answer("Think it's more important to give them some of your time; work can wait", vExtrovertTrait, 0.2f),
52
+                                        new Answer("Listen, but with only with half an ear ", vIntrovertTrait, 0.2f),
34 53
                                         new Answer("Try to show interest despite being busy chasing your tail", vDogTrait, 0.8f),
35 54
                                         new Answer("Interrupt and explain that you are really busy at the moment", vExtrovertTrait, 0.5f)
36 55
                                 )
@@ -39,70 +58,51 @@ public class Application {
39 58
                         // Second question
40 59
                         //------------------------------------
41 60
                         new Question(
42
-                                "You've been sitting in the doctor's waiting room for more than 25 minutes.",
61
+                                "You've been sitting in the doctor's waiting room for more than 25 minutes, you :",
43 62
                                 Arrays.asList(
44 63
                                         new Answer("Look at your watch every two minutes", vIntrovertTrait, 0.2f),
45 64
                                         new Answer("Bubble with inner anger, but keep quiet ", vIntrovertTrait, 0.5f),
46 65
                                         new Answer("Explain to other equally impatient people in the room that the doctor is always running late ", vExtrovertTrait, 0.3f),
47 66
                                         new Answer("Complain in a loud voice, while tapping your foot impatiently", vExtrovertTrait, 0.7f),
48
-                                        new Answer("Befriend the teckel sitting next to you", vExtrovertTrait, 0.7f)
67
+                                        new Answer("Befriend the teckel sitting next to you", vDogTrait, 0.7f)
49 68
                                 )
50 69
                         ),
51 70
                         //------------------------------------
52 71
                         // Third question
53 72
                         //------------------------------------
54 73
                         new Question(
55
-                                "Blah blah blah ? Blah.",
74
+                                "How do you usually answer affirmatively ?",
56 75
                                 Arrays.asList(
57 76
                                         new Answer("Yes.", vIntrovertTrait, 0.2f),
58 77
                                         new Answer("Woof", vDogTrait, 0.7f),
59
-                                        new Answer("Sure", vExtrovertTrait, 0.3f),
60
-                                        new Answer("Meh", vExtrovertTrait, 0.7f)
61
-
78
+                                        new Answer("Absolutely !", vExtrovertTrait, 0.3f)
62 79
                                 )
63 80
                         ),
64 81
                         //------------------------------------
65 82
                         // Fourth question
66 83
                         //------------------------------------
67 84
                         new Question(
68
-                                "Blah blah blah ? Blah.",
85
+                                "When receiving guests, you don't appreciate :",
69 86
                                 Arrays.asList(
70
-                                        new Answer("Yes.", vIntrovertTrait, 0.2f),
71
-                                        new Answer("Woof", vDogTrait, 0.7f),
72
-                                        new Answer("Sure", vExtrovertTrait, 0.3f),
73
-                                        new Answer("Meh", vExtrovertTrait, 0.7f)
74
-
87
+                                        new Answer("People that don't feed the conversation.", vExtrovertTrait, 0.2f),
88
+                                        new Answer("Cats.", vDogTrait, 0.7f),
89
+                                        new Answer("Being the center of attention.", vIntrovertTrait, 0.3f)
75 90
                                 )
76 91
                         ),
77 92
                         //------------------------------------
78 93
                         // Fifth question
79 94
                         //------------------------------------
80 95
                         new Question(
81
-                                "Blah blah blah ? Blah.",
96
+                                "You can't find the keys to your car.",
82 97
                                 Arrays.asList(
83
-                                        new Answer("Yes.", vIntrovertTrait, 0.2f),
84
-                                        new Answer("Woof", vDogTrait, 0.7f),
85
-                                        new Answer("Sure", vExtrovertTrait, 0.3f),
86
-                                        new Answer("Meh", vExtrovertTrait, 0.7f)
98
+                                        new Answer("You search desperately without asking for help", vIntrovertTrait, 0.2f),
99
+                                        new Answer("You get everyone here to look with you.", vExtrovertTrait, 0.2f),
100
+                                        new Answer("You don't need to worry about car keys because you can't drive a car with your paws.", vDogTrait, 0.7f)
87 101
 
88 102
                                 )
89 103
                         )
90 104
                 )
91 105
         );
92
-
93
-        //------------------------------------
94
-        // Start server
95
-        //------------------------------------
96
-        try {
97
-            HttpServer vServer = HttpServer.create(new InetSocketAddress(8080), 0);
98
-            vServer.createContext("/", new ResponseHandler());
99
-            vServer.setExecutor(null);
100
-            vServer.start();
101
-        }
102
-        catch(Exception ve)
103
-        {
104
-            ve.printStackTrace();
105
-        }
106 106
     }
107 107
 
108 108
 }

+ 10
- 2
backend/src/main/java/controller/SessionController.java Просмотреть файл

@@ -1,6 +1,5 @@
1 1
 package controller;
2 2
 
3
-import com.github.cliftonlabs.json_simple.JsonKey;
4 3
 import com.github.cliftonlabs.json_simple.JsonObject;
5 4
 import model.questionnaire.Answer;
6 5
 import model.questionnaire.Question;
@@ -21,6 +20,13 @@ public class SessionController extends ModelController {
21 20
         return vSession.asJsonObject();
22 21
     }
23 22
 
23
+    /**
24
+     *  For a specific question in a session, sets the given answer according to data in the json request body
25
+     * @param iID the session UUID
26
+     * @param iBody JSON Body containing tag 'question' and 'answer' with their respective UUIDs
27
+     * @return an empty JsonObject
28
+     * @throws Exception if the session, question or answer can't be found
29
+     */
24 30
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
25 31
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
26 32
         if(null == vSession)
@@ -28,12 +34,14 @@ public class SessionController extends ModelController {
28 34
 
29 35
         String vQuestionId = iBody.getString(KEY_QUESTION);
30 36
         String vAnswerId   = iBody.getString(KEY_ANSWER);
37
+        if(null == vQuestionId || null == vAnswerId )
38
+            throw new Exception(String.format("Tags '%s' & '%s' expected in request body",KEY_QUESTION.getKey(), KEY_ANSWER.getKey()));
31 39
         Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
32 40
         if(null == vQuestion)
33 41
             throw new Exception(String.format("No question for UUID %s",vQuestionId));
34 42
         Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
35 43
         if(null == vAnswer)
36
-            throw new Exception(String.format("No answer at index %d", vAnswerId));
44
+            throw new Exception(String.format("No answer for UUID %s", vAnswerId));
37 45
 
38 46
         vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
39 47
         System.out.println(vSession.asJsonObject().toString());

+ 0
- 13
backend/src/main/java/model/personnality/Personality.java Просмотреть файл

@@ -1,13 +0,0 @@
1
-package model.personnality;
2
-
3
-import java.util.HashMap;
4
-
5
-public class Personality {
6
-
7
-    private HashMap<Trait, Float> fTraits;
8
-
9
-    public Personality()
10
-    {
11
-        fTraits = new HashMap<>();
12
-    }
13
-}

+ 2
- 2
backend/src/main/java/model/personnality/Trait.java Просмотреть файл

@@ -13,8 +13,8 @@ import static api.JsonKeys.KEY_NAME;
13 13
 
14 14
 public class Trait implements Jsonable {
15 15
 
16
-    private String fName;
17
-    private String fDescription;
16
+    private final String fName;
17
+    private final String fDescription;
18 18
 
19 19
     public Trait(String iName, String iDescription){
20 20
         fName           = iName;

+ 4
- 4
backend/src/main/java/model/questionnaire/Answer.java Просмотреть файл

@@ -14,10 +14,10 @@ import static api.JsonKeys.*;
14 14
 
15 15
 public class Answer implements Jsonable {
16 16
 
17
-    private UUID fId;
18
-    private float   fWeight;
19
-    private Trait   fTrait;
20
-    private String  fDescription;
17
+    private final UUID fId;
18
+    private final float   fWeight;
19
+    private final Trait   fTrait;
20
+    private final String  fDescription;
21 21
 
22 22
 
23 23
     public Answer(String iDescription, Trait iTrait, float iWeight)

+ 3
- 3
backend/src/main/java/model/questionnaire/Question.java Просмотреть файл

@@ -16,9 +16,9 @@ import static api.JsonKeys.*;
16 16
 public class Question implements Jsonable {
17 17
 
18 18
     private static final HashMap<UUID, Question> sQuestions = new HashMap<>();
19
-    private UUID fId;
20
-    private List<Answer> fAnswers;
21
-    private String fName;
19
+    private final UUID fId;
20
+    private final List<Answer> fAnswers;
21
+    private final String fName;
22 22
 
23 23
     public Question(String iName, List<Answer> iAnswers)
24 24
     {

+ 4
- 3
backend/src/main/java/model/questionnaire/Questionnaire.java Просмотреть файл

@@ -10,9 +10,9 @@ import static api.JsonKeys.KEY_QUESTIONS;
10 10
 
11 11
 public class Questionnaire{
12 12
 
13
-    private static List<Questionnaire> sQuestionnaires = new ArrayList<>();
14
-    private List<Question> fQuestions;
15
-    private String fName;
13
+    private static final List<Questionnaire> sQuestionnaires = new ArrayList<>();
14
+    private final List<Question> fQuestions;
15
+    private final String fName;
16 16
 
17 17
     public Questionnaire(String iName, List<Question> iQuestions)
18 18
     {
@@ -21,6 +21,7 @@ public class Questionnaire{
21 21
         sQuestionnaires.add(this);
22 22
     }
23 23
 
24
+    // quick and dirty for now
24 25
     public static Questionnaire getTheOnlyExistingQuestionnaire(){
25 26
         return sQuestionnaires.stream().findFirst().get();
26 27
     }

+ 3
- 6
backend/src/main/java/model/questionnaire/Session.java Просмотреть файл

@@ -2,7 +2,6 @@ package model.questionnaire;
2 2
 
3 3
 import com.github.cliftonlabs.json_simple.JsonObject;
4 4
 import com.github.cliftonlabs.json_simple.Jsonable;
5
-import model.personnality.Personality;
6 5
 
7 6
 import java.io.IOException;
8 7
 import java.io.StringWriter;
@@ -15,15 +14,13 @@ import static api.JsonKeys.*;
15 14
 
16 15
 public class Session implements Jsonable {
17 16
 
18
-    private static HashMap<UUID, Session> sSessions = new HashMap<UUID, Session>();
19
-    private UUID fId;
20
-    private SessionAnswers fAnswers;
21
-    private Personality    fPersonality;
17
+    private static final HashMap<UUID, Session> sSessions = new HashMap<>();
18
+    private final UUID fId;
19
+    private final SessionAnswers fAnswers;
22 20
 
23 21
     public Session(){
24 22
         fId = UUID.randomUUID();
25 23
         fAnswers = new SessionAnswers();
26
-        fPersonality = new Personality();
27 24
         sSessions.put(fId, this);
28 25
     }
29 26
 

+ 4
- 10
backend/src/main/java/model/questionnaire/SessionAnswers.java Просмотреть файл

@@ -3,14 +3,12 @@ package model.questionnaire;
3 3
 import com.github.cliftonlabs.json_simple.JsonArray;
4 4
 import com.github.cliftonlabs.json_simple.JsonObject;
5 5
 import com.github.cliftonlabs.json_simple.Jsonable;
6
-import model.personnality.Personality;
7 6
 import model.personnality.Trait;
8 7
 
9 8
 import java.io.IOException;
10 9
 import java.io.StringWriter;
11 10
 import java.io.Writer;
12 11
 import java.nio.charset.StandardCharsets;
13
-import java.util.ArrayList;
14 12
 import java.util.HashMap;
15 13
 
16 14
 import static api.JsonKeys.*;
@@ -20,8 +18,8 @@ import static api.JsonKeys.*;
20 18
  */
21 19
 public class SessionAnswers implements Jsonable {
22 20
 
23
-    private HashMap<Question, Answer> fAnswers;
24
-    private HashMap<Trait, Float> fTraits;
21
+    private final HashMap<Question, Answer> fAnswers;
22
+    private final HashMap<Trait, Float> fTraits;
25 23
 
26 24
     public SessionAnswers() {
27 25
         fTraits = new HashMap<>();
@@ -64,14 +62,10 @@ public class SessionAnswers implements Jsonable {
64 62
     {
65 63
         JsonObject vResult = new JsonObject();
66 64
         JsonArray vAnswers = new JsonArray();
67
-        fAnswers.forEach( (question, answer) -> {
68
-            vAnswers.add(answer.asJsonObject().putChain(KEY_QUESTION, question.asJsonObject()));
69
-        });
65
+        fAnswers.forEach( (question, answer) -> vAnswers.add(answer.asJsonObject().putChain(KEY_QUESTION, question.asJsonObject())));
70 66
         vResult.put(KEY_ANSWERS, vAnswers);
71 67
         JsonArray vTraits = new JsonArray();
72
-        fTraits.forEach( (trait, weight) -> {
73
-            vTraits.add(trait.asJsonObject().putChain(KEY_WEIGHT, weight));
74
-        });
68
+        fTraits.forEach( (trait, weight) -> vTraits.add(trait.asJsonObject().putChain(KEY_WEIGHT, weight)));
75 69
         vResult.put(KEY_TRAITS, vTraits);
76 70
         return vResult;
77 71
     }

+ 26
- 0
backend/src/test/java/api/ResponseHandlerTest.java Просмотреть файл

@@ -1,6 +1,8 @@
1 1
 package api;
2 2
 
3 3
 import com.github.cliftonlabs.json_simple.JsonObject;
4
+import controller.ModelController;
5
+import controller.SessionController;
4 6
 import org.junit.jupiter.api.Test;
5 7
 
6 8
 import static org.junit.jupiter.api.Assertions.*;
@@ -25,5 +27,29 @@ class ResponseHandlerTest {
25 27
         vErrorObject.put("error", "DELETE with 2 subdirectories not implemented");
26 28
         assertEquals(vErrorObject, vResponseHandler.getResponse("DELETE", vPath, "Yo", null));
27 29
 
30
+        //Get a valid session id
31
+        SessionController vSessionController = new SessionController();
32
+        JsonObject vSessionObject = vResponseHandler.getResponse("GET",new String[]{},"", vSessionController);
33
+        assertTrue(vSessionObject.containsKey("id"));
34
+        String vSessionId = (String) vSessionObject.get("id");
35
+
36
+        //Verify error management in various cases
37
+        String[] vPathSession = new String[]{vSessionId};
38
+        JsonObject vResponseObject;
39
+        Object[][] vTestCases = new Object[][]{
40
+                {"PUT", vPathSession, "Yo", vSessionController},
41
+                {"PUT", new String[]{"a"}, "{}", vSessionController},
42
+                {"PUT", new String[]{"ff95a8b5-736a-4198-acaa-1fecdaa94c14"}, "{}", vSessionController},
43
+                {"PUT", vPathSession, "{\"question\":\"a\",\"answer\":\"a\"}", vSessionController},
44
+                {"PUT", vPathSession, "{\"question\":\"ff95a8b5-736a-4198-acaa-1fecdaa94c14\",\"answer\":\"ff95a8b5-736a-4198-acaa-1fecdaa94c14\"}", vSessionController},
45
+        };
46
+        for(Object [] vTestCase : vTestCases)
47
+        {
48
+            vResponseObject = vResponseHandler.getResponse((String) vTestCase[0],(String[]) vTestCase[1],(String) vTestCase[2],(ModelController) vTestCase[3]);
49
+            assertTrue(vResponseObject.containsKey("error"));
50
+            assertNotEquals(null, vResponseObject.get("error"));
51
+        }
52
+
53
+
28 54
     }
29 55
 }

+ 2
- 2
frontend/public/index.html Просмотреть файл

@@ -7,7 +7,7 @@
7 7
     <meta name="theme-color" content="#000000" />
8 8
     <meta
9 9
       name="description"
10
-      content="Web site created using create-react-app"
10
+      content="Totally legit personnality test built as a test to join Teamway"
11 11
     />
12 12
     <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13 13
     <!--
@@ -24,7 +24,7 @@
24 24
       work correctly both with client-side routing and a non-root public URL.
25 25
       Learn how to configure a non-root public URL by running `npm run build`.
26 26
     -->
27
-    <title>React App</title>
27
+    <title>Elias Sebbar - Personnality Test</title>
28 28
   </head>
29 29
   <body>
30 30
     <noscript>You need to enable JavaScript to run this app.</noscript>

+ 1
- 1
frontend/src/App.js Просмотреть файл

@@ -52,7 +52,7 @@ function App() {
52 52
         </div>
53 53
       }
54 54
       { questionnaireStarted &&
55
-        <Questionnaire sessionId={sessionIdReceived}/>}
55
+        <Questionnaire sessionId={sessionIdReceived} onStartQuestionnaire={onStartQuestionnaire}/>}
56 56
     </div>
57 57
   );
58 58
 }

+ 1
- 1
frontend/src/component/questionnaire.js Просмотреть файл

@@ -52,7 +52,7 @@ function Questionnaire(props){
52 52
     )
53 53
     else
54 54
     return(
55
-        <QuestionnaireResult sessionId={props.sessionId}/>
55
+        <QuestionnaireResult onStartQuestionnaire={props.onStartQuestionnaire} sessionId={props.sessionId}/>
56 56
     )
57 57
 }
58 58
 

+ 3
- 0
frontend/src/component/questionnaire_result.js Просмотреть файл

@@ -87,6 +87,9 @@ function QuestionnaireResult(props){
87 87
             Approximately {dominant_weight.toFixed(1)}% : 🎉{dominant.description} 
88 88
         </div>
89 89
         }
90
+        <div onClick={() => props.onStartQuestionnaire(false)} className="interactible">
91
+            Restart
92
+        </div>
90 93
         </React.Fragment>
91 94
     )
92 95
 }

Загрузка…
Отмена
Сохранить