Browse Source

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

master
DemiSel 2 years ago
parent
commit
4763e97044

+ 38
- 38
backend/src/main/java/Application.java View File

12
 
12
 
13
     public static void main(String [] args)
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
         // Define questionnaire
39
         // Define questionnaire
21
         //------------------------------------
40
         //------------------------------------
22
-        Questionnaire vQuestionnaire = new Questionnaire(
41
+        return new Questionnaire(
23
                 "Introvert or extrovert ?",
42
                 "Introvert or extrovert ?",
24
                 Arrays.asList(
43
                 Arrays.asList(
25
                         //------------------------------------
44
                         //------------------------------------
26
                         // First question
45
                         // First question
27
                         //------------------------------------
46
                         //------------------------------------
28
                         new Question(
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
                                 Arrays.asList(
49
                                 Arrays.asList(
31
                                         new Answer("Don't dare to interrupt them", vIntrovertTrait, 0.5f),
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
                                         new Answer("Try to show interest despite being busy chasing your tail", vDogTrait, 0.8f),
53
                                         new Answer("Try to show interest despite being busy chasing your tail", vDogTrait, 0.8f),
35
                                         new Answer("Interrupt and explain that you are really busy at the moment", vExtrovertTrait, 0.5f)
54
                                         new Answer("Interrupt and explain that you are really busy at the moment", vExtrovertTrait, 0.5f)
36
                                 )
55
                                 )
39
                         // Second question
58
                         // Second question
40
                         //------------------------------------
59
                         //------------------------------------
41
                         new Question(
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
                                 Arrays.asList(
62
                                 Arrays.asList(
44
                                         new Answer("Look at your watch every two minutes", vIntrovertTrait, 0.2f),
63
                                         new Answer("Look at your watch every two minutes", vIntrovertTrait, 0.2f),
45
                                         new Answer("Bubble with inner anger, but keep quiet ", vIntrovertTrait, 0.5f),
64
                                         new Answer("Bubble with inner anger, but keep quiet ", vIntrovertTrait, 0.5f),
46
                                         new Answer("Explain to other equally impatient people in the room that the doctor is always running late ", vExtrovertTrait, 0.3f),
65
                                         new Answer("Explain to other equally impatient people in the room that the doctor is always running late ", vExtrovertTrait, 0.3f),
47
                                         new Answer("Complain in a loud voice, while tapping your foot impatiently", vExtrovertTrait, 0.7f),
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
                         // Third question
71
                         // Third question
53
                         //------------------------------------
72
                         //------------------------------------
54
                         new Question(
73
                         new Question(
55
-                                "Blah blah blah ? Blah.",
74
+                                "How do you usually answer affirmatively ?",
56
                                 Arrays.asList(
75
                                 Arrays.asList(
57
                                         new Answer("Yes.", vIntrovertTrait, 0.2f),
76
                                         new Answer("Yes.", vIntrovertTrait, 0.2f),
58
                                         new Answer("Woof", vDogTrait, 0.7f),
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
                         // Fourth question
82
                         // Fourth question
66
                         //------------------------------------
83
                         //------------------------------------
67
                         new Question(
84
                         new Question(
68
-                                "Blah blah blah ? Blah.",
85
+                                "When receiving guests, you don't appreciate :",
69
                                 Arrays.asList(
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
                         // Fifth question
93
                         // Fifth question
79
                         //------------------------------------
94
                         //------------------------------------
80
                         new Question(
95
                         new Question(
81
-                                "Blah blah blah ? Blah.",
96
+                                "You can't find the keys to your car.",
82
                                 Arrays.asList(
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 View File

1
 package controller;
1
 package controller;
2
 
2
 
3
-import com.github.cliftonlabs.json_simple.JsonKey;
4
 import com.github.cliftonlabs.json_simple.JsonObject;
3
 import com.github.cliftonlabs.json_simple.JsonObject;
5
 import model.questionnaire.Answer;
4
 import model.questionnaire.Answer;
6
 import model.questionnaire.Question;
5
 import model.questionnaire.Question;
21
         return vSession.asJsonObject();
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
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
30
     public JsonObject updateEntry(String iID, JsonObject iBody) throws Exception{
25
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
31
         Session vSession = Session.getSessionForUUID(UUID.fromString(iID));
26
         if(null == vSession)
32
         if(null == vSession)
28
 
34
 
29
         String vQuestionId = iBody.getString(KEY_QUESTION);
35
         String vQuestionId = iBody.getString(KEY_QUESTION);
30
         String vAnswerId   = iBody.getString(KEY_ANSWER);
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
         Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
39
         Question vQuestion = Question.getForUUID(UUID.fromString(vQuestionId));
32
         if(null == vQuestion)
40
         if(null == vQuestion)
33
             throw new Exception(String.format("No question for UUID %s",vQuestionId));
41
             throw new Exception(String.format("No question for UUID %s",vQuestionId));
34
         Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
42
         Answer vAnswer = vQuestion.getAnswerForUUID(UUID.fromString(vAnswerId));
35
         if(null == vAnswer)
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
         vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
46
         vSession.getSessionAnswers().setAnswer(vQuestion, vAnswer);
39
         System.out.println(vSession.asJsonObject().toString());
47
         System.out.println(vSession.asJsonObject().toString());

+ 0
- 13
backend/src/main/java/model/personnality/Personality.java View File

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 View File

13
 
13
 
14
 public class Trait implements Jsonable {
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
     public Trait(String iName, String iDescription){
19
     public Trait(String iName, String iDescription){
20
         fName           = iName;
20
         fName           = iName;

+ 4
- 4
backend/src/main/java/model/questionnaire/Answer.java View File

14
 
14
 
15
 public class Answer implements Jsonable {
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
     public Answer(String iDescription, Trait iTrait, float iWeight)
23
     public Answer(String iDescription, Trait iTrait, float iWeight)

+ 3
- 3
backend/src/main/java/model/questionnaire/Question.java View File

16
 public class Question implements Jsonable {
16
 public class Question implements Jsonable {
17
 
17
 
18
     private static final HashMap<UUID, Question> sQuestions = new HashMap<>();
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
     public Question(String iName, List<Answer> iAnswers)
23
     public Question(String iName, List<Answer> iAnswers)
24
     {
24
     {

+ 4
- 3
backend/src/main/java/model/questionnaire/Questionnaire.java View File

10
 
10
 
11
 public class Questionnaire{
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
     public Questionnaire(String iName, List<Question> iQuestions)
17
     public Questionnaire(String iName, List<Question> iQuestions)
18
     {
18
     {
21
         sQuestionnaires.add(this);
21
         sQuestionnaires.add(this);
22
     }
22
     }
23
 
23
 
24
+    // quick and dirty for now
24
     public static Questionnaire getTheOnlyExistingQuestionnaire(){
25
     public static Questionnaire getTheOnlyExistingQuestionnaire(){
25
         return sQuestionnaires.stream().findFirst().get();
26
         return sQuestionnaires.stream().findFirst().get();
26
     }
27
     }

+ 3
- 6
backend/src/main/java/model/questionnaire/Session.java View File

2
 
2
 
3
 import com.github.cliftonlabs.json_simple.JsonObject;
3
 import com.github.cliftonlabs.json_simple.JsonObject;
4
 import com.github.cliftonlabs.json_simple.Jsonable;
4
 import com.github.cliftonlabs.json_simple.Jsonable;
5
-import model.personnality.Personality;
6
 
5
 
7
 import java.io.IOException;
6
 import java.io.IOException;
8
 import java.io.StringWriter;
7
 import java.io.StringWriter;
15
 
14
 
16
 public class Session implements Jsonable {
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
     public Session(){
21
     public Session(){
24
         fId = UUID.randomUUID();
22
         fId = UUID.randomUUID();
25
         fAnswers = new SessionAnswers();
23
         fAnswers = new SessionAnswers();
26
-        fPersonality = new Personality();
27
         sSessions.put(fId, this);
24
         sSessions.put(fId, this);
28
     }
25
     }
29
 
26
 

+ 4
- 10
backend/src/main/java/model/questionnaire/SessionAnswers.java View File

3
 import com.github.cliftonlabs.json_simple.JsonArray;
3
 import com.github.cliftonlabs.json_simple.JsonArray;
4
 import com.github.cliftonlabs.json_simple.JsonObject;
4
 import com.github.cliftonlabs.json_simple.JsonObject;
5
 import com.github.cliftonlabs.json_simple.Jsonable;
5
 import com.github.cliftonlabs.json_simple.Jsonable;
6
-import model.personnality.Personality;
7
 import model.personnality.Trait;
6
 import model.personnality.Trait;
8
 
7
 
9
 import java.io.IOException;
8
 import java.io.IOException;
10
 import java.io.StringWriter;
9
 import java.io.StringWriter;
11
 import java.io.Writer;
10
 import java.io.Writer;
12
 import java.nio.charset.StandardCharsets;
11
 import java.nio.charset.StandardCharsets;
13
-import java.util.ArrayList;
14
 import java.util.HashMap;
12
 import java.util.HashMap;
15
 
13
 
16
 import static api.JsonKeys.*;
14
 import static api.JsonKeys.*;
20
  */
18
  */
21
 public class SessionAnswers implements Jsonable {
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
     public SessionAnswers() {
24
     public SessionAnswers() {
27
         fTraits = new HashMap<>();
25
         fTraits = new HashMap<>();
64
     {
62
     {
65
         JsonObject vResult = new JsonObject();
63
         JsonObject vResult = new JsonObject();
66
         JsonArray vAnswers = new JsonArray();
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
         vResult.put(KEY_ANSWERS, vAnswers);
66
         vResult.put(KEY_ANSWERS, vAnswers);
71
         JsonArray vTraits = new JsonArray();
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
         vResult.put(KEY_TRAITS, vTraits);
69
         vResult.put(KEY_TRAITS, vTraits);
76
         return vResult;
70
         return vResult;
77
     }
71
     }

+ 26
- 0
backend/src/test/java/api/ResponseHandlerTest.java View File

1
 package api;
1
 package api;
2
 
2
 
3
 import com.github.cliftonlabs.json_simple.JsonObject;
3
 import com.github.cliftonlabs.json_simple.JsonObject;
4
+import controller.ModelController;
5
+import controller.SessionController;
4
 import org.junit.jupiter.api.Test;
6
 import org.junit.jupiter.api.Test;
5
 
7
 
6
 import static org.junit.jupiter.api.Assertions.*;
8
 import static org.junit.jupiter.api.Assertions.*;
25
         vErrorObject.put("error", "DELETE with 2 subdirectories not implemented");
27
         vErrorObject.put("error", "DELETE with 2 subdirectories not implemented");
26
         assertEquals(vErrorObject, vResponseHandler.getResponse("DELETE", vPath, "Yo", null));
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 View File

7
     <meta name="theme-color" content="#000000" />
7
     <meta name="theme-color" content="#000000" />
8
     <meta
8
     <meta
9
       name="description"
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
     <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
12
     <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13
     <!--
13
     <!--
24
       work correctly both with client-side routing and a non-root public URL.
24
       work correctly both with client-side routing and a non-root public URL.
25
       Learn how to configure a non-root public URL by running `npm run build`.
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
   </head>
28
   </head>
29
   <body>
29
   <body>
30
     <noscript>You need to enable JavaScript to run this app.</noscript>
30
     <noscript>You need to enable JavaScript to run this app.</noscript>

+ 1
- 1
frontend/src/App.js View File

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

+ 1
- 1
frontend/src/component/questionnaire.js View File

52
     )
52
     )
53
     else
53
     else
54
     return(
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 View File

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

Loading…
Cancel
Save