xref: /haiku/src/tests/apps/haikudepot/ValidationFailureTest.cpp (revision 835e7239d03992421ebb46ba5d152f2d889dbf80)
1 /*
2  * Copyright 2019, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "ValidationFailureTest.h"
7 
8 #include <cppunit/TestCaller.h>
9 #include <cppunit/TestSuite.h>
10 
11 #include "ValidationFailure.h"
12 
13 
ValidationFailureTest()14 ValidationFailureTest::ValidationFailureTest()
15 {
16 }
17 
18 
~ValidationFailureTest()19 ValidationFailureTest::~ValidationFailureTest()
20 {
21 }
22 
23 
24 void
TestDearchive()25 ValidationFailureTest::TestDearchive()
26 {
27 	BMessage nicknameMessage;
28 	nicknameMessage.AddString("property", "nickname");
29 	nicknameMessage.AddString("message_0", "malformed");
30 	nicknameMessage.AddString("message_1", "required");
31 
32 	BMessage passwordClearMessage;
33 	passwordClearMessage.AddString("property", "passwordClear");
34 	passwordClearMessage.AddString("message_0", "required");
35 
36 	BMessage validationFailuresMessage;
37 	validationFailuresMessage.AddMessage("item_0", &nicknameMessage);
38 	validationFailuresMessage.AddMessage("item_1", &passwordClearMessage);
39 
40 // ----------------------
41 	ValidationFailures validationFailures(&validationFailuresMessage);
42 // ----------------------
43 
44 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Count failures", (int32) 2,
45 		validationFailures.CountFailures());
46 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Contains 'nickname'", true,
47 		validationFailures.Contains("nickname"));
48 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Contains 'nickname:required'", true,
49 		validationFailures.Contains("nickname", "required"));
50 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Contains 'nickname:malformed'", true,
51 		validationFailures.Contains("nickname", "malformed"));
52 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Contains 'passwordClear:required'", true,
53 		validationFailures.Contains("passwordClear", "required"));
54 }
55 
56 
57 void
TestArchive()58 ValidationFailureTest::TestArchive()
59 {
60 	ValidationFailures failures;
61 	failures.AddFailure("nickname", "malformed");
62 	failures.AddFailure("nickname", "required");
63 	failures.AddFailure("passwordClear", "required");
64 	BMessage validationFailuresMessage;
65 
66 // ----------------------
67 	status_t archiveResult = failures.Archive(&validationFailuresMessage);
68 // ----------------------
69 
70 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Archive failure", B_OK, archiveResult);
71 	BMessage validationFailureNicknameMessage;
72 	BMessage validationFailurePasswordClearMessage;
73 
74 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unable to find 'nickname'", B_OK,
75 		FindMessageWithProperty("nickname", validationFailuresMessage,
76 			validationFailureNicknameMessage));
77 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unable to find 'passwordClear'", B_OK,
78 		FindMessageWithProperty("passwordClear", validationFailuresMessage,
79 			validationFailurePasswordClearMessage));
80 
81 	BStringList validationFailureMessagesNickname;
82 	BStringList validationFailureMessagesPasswordClear;
83 	FindValidationMessages(validationFailureNicknameMessage,
84 		validationFailureMessagesNickname);
85 	FindValidationMessages(validationFailurePasswordClearMessage,
86 		validationFailureMessagesPasswordClear);
87 
88 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unable to find 'nickname:malformed'",
89 		true, validationFailureMessagesNickname.HasString("malformed"));
90 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unable to find 'nickname:required'",
91 		true, validationFailureMessagesNickname.HasString("required"));
92 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unexpected validation messages 'nickname'",
93 		(int32) 2, validationFailureMessagesNickname.CountStrings());
94 
95 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unable to find 'passwordClear:required'",
96 		true, validationFailureMessagesPasswordClear.HasString("required"));
97 	CPPUNIT_ASSERT_EQUAL_MESSAGE("!Unexpected validation messages 'nickname'",
98 		(int32) 1, validationFailureMessagesPasswordClear.CountStrings());
99 }
100 
101 
102 /*static*/ void
AddTests(BTestSuite & parent)103 ValidationFailureTest::AddTests(BTestSuite& parent)
104 {
105 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite(
106 		"ValidationFailureTest");
107 
108 	suite.addTest(
109 		new CppUnit::TestCaller<ValidationFailureTest>(
110 			"ValidationFailureTest::TestArchive",
111 			&ValidationFailureTest::TestArchive));
112 	suite.addTest(
113 		new CppUnit::TestCaller<ValidationFailureTest>(
114 			"ValidationFailureTest::TestDearchive",
115 			&ValidationFailureTest::TestDearchive));
116 
117 	parent.addTest("ValidationFailureTest", &suite);
118 }
119 
120 
121 /*static*/ status_t
FindMessageWithProperty(const char * property,BMessage & validationFailuresMessage,BMessage & validationFailureMessage)122 ValidationFailureTest::FindMessageWithProperty(
123 	const char* property, BMessage& validationFailuresMessage,
124 	BMessage& validationFailureMessage)
125 {
126 	status_t result = B_OK;
127 
128 	for (int32 i = 0; result == B_OK; i++) {
129 		BString name = "item_";
130 		name << i;
131 		result = validationFailuresMessage.FindMessage(name,
132 			&validationFailureMessage);
133 
134 		if (result == B_OK) {
135 			BString messageProperty;
136 			result = validationFailureMessage.FindString("property",
137 				&messageProperty);
138 
139 			if (result == B_OK && messageProperty == property)
140 				return result;
141 		}
142 	}
143 
144 	return result;
145 }
146 
147 
148 /*static*/ void
FindValidationMessages(BMessage & validationFailureMessage,BStringList & validationMessages)149 ValidationFailureTest::FindValidationMessages(
150 	BMessage& validationFailureMessage, BStringList& validationMessages)
151 {
152 	status_t result = B_OK;
153 
154 	for (int32 i = 0; result == B_OK; i++) {
155 		BString validationMessage;
156 		BString name = "message_";
157 		name << i;
158 		result = validationFailureMessage.FindString(name,
159 			&validationMessage);
160 
161 		if (result == B_OK) {
162 			validationMessages.Add(validationMessage);
163 		}
164 	}
165 }