|
|
@@ -0,0 +1,139 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.RelationshipQuestionnaireSnapshot;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.RelationshipQuestionnaireResponseMapper;
|
|
|
+import com.etotem.cfc.mapper.RelationshipQuestionnaireSnapshotMapper;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.math.BigDecimal;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+
|
|
|
+public class RelationshipQuestionnaireServiceTest {
|
|
|
+
|
|
|
+ private RelationshipQuestionnaireService service;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private RelationshipQuestionnaireSnapshotMapper snapshotMapper;
|
|
|
+ @Mock
|
|
|
+ private RelationshipQuestionnaireResponseMapper responseMapper;
|
|
|
+ @Mock
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setup() throws Exception {
|
|
|
+ MockitoAnnotations.openMocks(this);
|
|
|
+ service = new RelationshipQuestionnaireService();
|
|
|
+ setField(service, "snapshotMapper", snapshotMapper);
|
|
|
+ setField(service, "responseMapper", responseMapper);
|
|
|
+ setField(service, "familyMemberMapper", familyMemberMapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void setField(Object target, String fieldName, Object value) throws Exception {
|
|
|
+ java.lang.reflect.Field f = target.getClass().getDeclaredField(fieldName);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BigDecimal[] callCalculate(String answersJson, String questionsJson) throws Exception {
|
|
|
+ Method m = RelationshipQuestionnaireService.class
|
|
|
+ .getDeclaredMethod("calculateScoresFromAnswers", String.class, String.class);
|
|
|
+ m.setAccessible(true);
|
|
|
+ return (BigDecimal[]) m.invoke(service, answersJson, questionsJson);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void calculateScores_positive_allMax_intimacyMin() throws Exception {
|
|
|
+ String questions = "{\"version\":1,\"questions\":[" +
|
|
|
+ "{\"id\":\"q1\",\"dimension\":\"trust\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q2\",\"dimension\":\"trust\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q3\",\"dimension\":\"intimacy\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q4\",\"dimension\":\"communication\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}" +
|
|
|
+ "]}";
|
|
|
+ String answers = "{\"q1\":{\"id\":\"c\",\"score\":2},\"q2\":{\"id\":\"c\",\"score\":2}," +
|
|
|
+ "\"q3\":{\"id\":\"a\",\"score\":0},\"q4\":{\"id\":\"b\",\"score\":1}}";
|
|
|
+
|
|
|
+ BigDecimal[] scores = callCalculate(answers, questions);
|
|
|
+ assertEquals(0, scores[0].compareTo(new BigDecimal("100.00")));
|
|
|
+ assertEquals(0, scores[1].compareTo(new BigDecimal("0.00")));
|
|
|
+ assertEquals(0, scores[2].compareTo(new BigDecimal("50.00")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void calculateScores_negative_direction_reverse() throws Exception {
|
|
|
+ String questions = "{\"version\":1,\"questions\":[" +
|
|
|
+ "{\"id\":\"q1\",\"dimension\":\"trust\",\"direction\":\"negative\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q2\",\"dimension\":\"intimacy\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q3\",\"dimension\":\"communication\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}" +
|
|
|
+ "]}";
|
|
|
+ String answers = "{\"q1\":{\"id\":\"a\",\"score\":0}," +
|
|
|
+ "\"q2\":{\"id\":\"c\",\"score\":2},\"q3\":{\"id\":\"c\",\"score\":2}}";
|
|
|
+
|
|
|
+ BigDecimal[] scores = callCalculate(answers, questions);
|
|
|
+ assertEquals(0, scores[0].compareTo(new BigDecimal("100.00")));
|
|
|
+ assertEquals(0, scores[1].compareTo(new BigDecimal("100.00")));
|
|
|
+ assertEquals(0, scores[2].compareTo(new BigDecimal("100.00")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void calculateScores_weighted_average() throws Exception {
|
|
|
+ String questions = "{\"version\":1,\"questions\":[" +
|
|
|
+ "{\"id\":\"q1\",\"dimension\":\"trust\",\"direction\":\"positive\",\"weight\":2.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q2\",\"dimension\":\"trust\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q3\",\"dimension\":\"intimacy\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q4\",\"dimension\":\"communication\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":1},{\"id\":\"c\",\"score\":2}]}" +
|
|
|
+ "]}";
|
|
|
+ String answers = "{\"q1\":{\"id\":\"c\",\"score\":2},\"q2\":{\"id\":\"a\",\"score\":0}," +
|
|
|
+ "\"q3\":{\"id\":\"b\",\"score\":1},\"q4\":{\"id\":\"b\",\"score\":1}}";
|
|
|
+
|
|
|
+ BigDecimal[] scores = callCalculate(answers, questions);
|
|
|
+ assertEquals(0, scores[0].compareTo(new BigDecimal("66.67")));
|
|
|
+ assertEquals(0, scores[1].compareTo(new BigDecimal("50.00")));
|
|
|
+ assertEquals(0, scores[2].compareTo(new BigDecimal("50.00")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void calculateScores_invalidJson_returnsNegativeOne() throws Exception {
|
|
|
+ String questions = "{\"version\":1,\"questions\":[\"id\":\"q1\",\"dimension\":\"trust\"]}";
|
|
|
+ BigDecimal[] scores1 = callCalculate(null, questions);
|
|
|
+ assertEquals(0, scores1[0].compareTo(new BigDecimal("-1.00")));
|
|
|
+ assertEquals(0, scores1[1].compareTo(new BigDecimal("-1.00")));
|
|
|
+ assertEquals(0, scores1[2].compareTo(new BigDecimal("-1.00")));
|
|
|
+
|
|
|
+ BigDecimal[] scores2 = callCalculate("not-json!", questions);
|
|
|
+ assertEquals(0, scores2[0].compareTo(new BigDecimal("-1.00")));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void calculateScores_missingDimension_onlyAveragesExisting() throws Exception {
|
|
|
+ String questions = "{\"version\":1,\"questions\":[" +
|
|
|
+ "{\"id\":\"q1\",\"dimension\":\"trust\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":2}]}," +
|
|
|
+ "{\"id\":\"q2\",\"dimension\":\"intimacy\",\"direction\":\"positive\",\"weight\":1.0," +
|
|
|
+ " \"options\":[{\"id\":\"a\",\"score\":0},{\"id\":\"b\",\"score\":2}]}" +
|
|
|
+ "]}";
|
|
|
+ String answers = "{\"q1\":{\"id\":\"b\",\"score\":2},\"q2\":{\"id\":\"b\",\"score\":2}}";
|
|
|
+
|
|
|
+ BigDecimal[] scores = callCalculate(answers, questions);
|
|
|
+ assertEquals(0, scores[0].compareTo(new BigDecimal("100.00")));
|
|
|
+ assertEquals(0, scores[1].compareTo(new BigDecimal("100.00")));
|
|
|
+ assertEquals(0, scores[2].compareTo(new BigDecimal("0.00")));
|
|
|
+ }
|
|
|
+}
|