12
12
import org .springframework .stereotype .Component ;
13
13
14
14
import com .mongodb .MongoClient ;
15
+ import com .mongodb .MongoClientURI ;
15
16
import com .mongodb .client .MongoCollection ;
16
17
import com .mongodb .client .MongoDatabase ;
17
18
import com .mongodb .client .model .Filters ;
22
23
23
24
@ Component
24
25
public class DataBaseManager {
25
-
26
+ private static final int MAX_WAIT_TIME_MILLISECONDS = 30000 ;
27
+ private static final int RETRY_EVERY_X_MILLISECONDS = 1000 ;
28
+
26
29
@ Value ("${spring.data.mongodb.database}" )
27
30
private String database ;
28
31
29
32
@ Value ("${event_object_map.collection.name}" )
30
- private String eventMapCollection ;
33
+ private String eventObjectCollectionName ;
31
34
32
35
@ Value ("${aggregated.collection.name}" )
33
36
private String aggregatedCollectionName ;
@@ -44,30 +47,22 @@ public class DataBaseManager {
44
47
45
48
private MongoClient mongoClient ;
46
49
47
- public int getMongoDbPort () {
48
- return mongoProperties .getPort ();
49
- }
50
-
51
- public String getMongoDbHost () {
52
- return mongoProperties .getHost ();
53
- }
54
-
55
50
/**
56
51
* Verify that aggregated object contains the expected information.
57
52
*
58
- * @param checklist
59
- * list of checklist to check
53
+ * @param checklist list of checklist to check
60
54
* @return list of missing checklist
61
55
* @throws InterruptedException
62
56
*/
63
- public List <String > verifyAggregatedObjectInDB (List <String > checklist ) throws InterruptedException {
64
- long stopTime = System .currentTimeMillis () + 30000 ;
57
+ public List <String > verifyAggregatedObjectInDB (List <String > checklist )
58
+ throws InterruptedException {
59
+ long stopTime = System .currentTimeMillis () + MAX_WAIT_TIME_MILLISECONDS ;
65
60
while (!checklist .isEmpty () && stopTime > System .currentTimeMillis ()) {
66
- checklist = compareArgumentsWithAggregatedObjectInDB (checklist );
61
+ checklist = getListOfArgumentsNotFoundInDatabase (checklist );
67
62
if (checklist .isEmpty ()) {
68
63
break ;
69
64
}
70
- TimeUnit .MILLISECONDS .sleep (1000 );
65
+ TimeUnit .MILLISECONDS .sleep (RETRY_EVERY_X_MILLISECONDS );
71
66
}
72
67
return checklist ;
73
68
}
@@ -79,82 +74,81 @@ public List<String> verifyAggregatedObjectInDB(List<String> checklist) throws In
79
74
* @throws InterruptedException
80
75
*/
81
76
public boolean verifyAggregatedObjectExistsInDB () throws InterruptedException {
82
- long stopTime = System .currentTimeMillis () + 30000 ;
77
+ long stopTime = System .currentTimeMillis () + MAX_WAIT_TIME_MILLISECONDS ;
83
78
while (stopTime > System .currentTimeMillis ()) {
84
- mongoClient = new MongoClient (getMongoDbHost (), getMongoDbPort ());
85
- MongoDatabase db = mongoClient .getDatabase (database );
86
- MongoCollection <Document > collection = db .getCollection (aggregatedCollectionName );
87
- List <Document > documents = collection .find ().into (new ArrayList <>());
88
- TimeUnit .MILLISECONDS .sleep (1000 );
79
+ List <Document > documents = getDocumentsFromCollection (aggregatedCollectionName );
89
80
if (!documents .isEmpty ()) {
90
81
return true ;
91
82
}
83
+ TimeUnit .MILLISECONDS .sleep (RETRY_EVERY_X_MILLISECONDS );
92
84
}
93
85
return false ;
94
86
}
95
87
96
- /**
97
- * Checks that aggregated object contains specified arguments.
98
- *
99
- * @param checklist
100
- * list of arguments
101
- * @return list of missing arguments
102
- */
103
- private List <String > compareArgumentsWithAggregatedObjectInDB (List <String > checklist ) {
104
- mongoClient = new MongoClient (getMongoDbHost (), getMongoDbPort ());
105
- MongoDatabase db = mongoClient .getDatabase (database );
106
- MongoCollection <Document > collection = db .getCollection (aggregatedCollectionName );
107
- List <Document > documents = collection .find ().into (new ArrayList <>());
108
- for (Document document : documents ) {
109
- for (String expectedValue : new ArrayList <>(checklist )) {
110
- if (document .toString ().contains (expectedValue )) {
111
- checklist .remove (expectedValue );
112
- }
113
- }
114
- }
115
- return checklist ;
116
- }
117
-
118
88
/**
119
89
* Verify that events are located in the database collection.
120
90
*
121
- * @param eventsIdList
122
- * list of events IDs
91
+ * @param eventsIdList list of events IDs
123
92
* @return list of missing events
124
93
* @throws InterruptedException
125
94
*/
126
- public List <String > verifyEventsInDB (List <String > eventsIdList , int extraCheckDelay ) throws InterruptedException {
127
- long stopTime = System .currentTimeMillis () + 30000 + extraCheckDelay ;
95
+ public List <String > verifyEventsInDB (List <String > eventsIdList , int extraCheckDelay )
96
+ throws InterruptedException {
97
+ long stopTime = System .currentTimeMillis () + MAX_WAIT_TIME_MILLISECONDS + extraCheckDelay ;
128
98
while (!eventsIdList .isEmpty () && stopTime > System .currentTimeMillis ()) {
129
99
eventsIdList = compareSentEventsWithEventsInDB (eventsIdList );
130
100
if (eventsIdList .isEmpty ()) {
131
101
break ;
132
102
}
133
- TimeUnit .MILLISECONDS .sleep (1000 );
103
+ TimeUnit .MILLISECONDS .sleep (RETRY_EVERY_X_MILLISECONDS );
134
104
}
135
105
return eventsIdList ;
136
106
}
137
107
108
+ private List <String > getListOfArgumentsNotFoundInDatabase (final List <String > checklist ) {
109
+ final List <Document > documents = getDocumentsFromCollection (aggregatedCollectionName );
110
+
111
+ List <String > foundValues = new ArrayList <>();
112
+ for (Document document : documents ) {
113
+ final List <String > valuesFoundInDocument = getValuesFoundInDocument (checklist ,
114
+ document );
115
+ foundValues .addAll (valuesFoundInDocument );
116
+ }
117
+
118
+ List <String > result = new ArrayList <>(checklist );
119
+ result .removeAll (foundValues );
120
+ return result ;
121
+ }
122
+
123
+ private List <String > getValuesFoundInDocument (final List <String > checklist ,
124
+ final Document document ) {
125
+ List <String > foundValues = new ArrayList <>();
126
+ for (final String expectedValue : checklist ) {
127
+ if (document .toString ().contains (expectedValue )) {
128
+ foundValues .add (expectedValue );
129
+ }
130
+ }
131
+ return foundValues ;
132
+ }
133
+
138
134
/**
139
135
* Checks collection of events against event list.
140
136
*
141
- * @param checklist
142
- * list of event IDs
137
+ * @param checklist list of event IDs
143
138
* @return list of missing events
144
139
*/
145
- private List <String > compareSentEventsWithEventsInDB (List <String > checklist ) {
146
- mongoClient = new MongoClient (getMongoDbHost (), getMongoDbPort ());
147
- MongoDatabase db = mongoClient .getDatabase (database );
148
- MongoCollection <Document > collection = db .getCollection (eventMapCollection );
149
- List <Document > documents = collection .find ().into (new ArrayList <>());
140
+ private List <String > compareSentEventsWithEventsInDB (final List <String > checklist ) {
141
+ final List <Document > documents = getDocumentsFromCollection (eventObjectCollectionName );
142
+
143
+ List <String > foundIDs = new ArrayList <>();
150
144
for (Document document : documents ) {
151
- for (String expectedID : new ArrayList <>(checklist )) {
152
- if (expectedID .equals (document .get ("_id" ).toString ())) {
153
- checklist .remove (expectedID );
154
- }
155
- }
145
+ final String documentId = document .get ("_id" ).toString ();
146
+ foundIDs .add (documentId );
156
147
}
157
- return checklist ;
148
+
149
+ List <String > result = new ArrayList <>(checklist );
150
+ result .removeAll (foundIDs );
151
+ return result ;
158
152
}
159
153
160
154
/**
@@ -171,31 +165,41 @@ public String getValueFromQuery(List<String> databaseQueryResult, String key, in
171
165
return jsonObject .get (key ).toString ();
172
166
}
173
167
174
- /**
175
- * Returns the size of the waitlist.
176
- *
177
- * @return int of the size of the waitlist.
178
- */
179
- public int waitListSize () {
180
- mongoClient = new MongoClient (getMongoDbHost (), getMongoDbPort ());
181
- MongoDatabase db = mongoClient .getDatabase (database );
182
- MongoCollection <Document > collection = db .getCollection (waitlistCollectionName );
183
- List <Document > documents = collection .find ().into (new ArrayList <>());
184
- return documents .size ();
185
- }
186
-
187
168
/**
188
169
* Get a specific subscription from the database based on the subscription name.
189
170
*
190
171
* @param subscriptionName
191
172
* @return the document as JSON string
192
173
*/
193
174
public String getSubscription (String subscriptionName ) {
194
- mongoClient = new MongoClient (getMongoDbHost (), getMongoDbPort ());
195
- MongoDatabase db = mongoClient .getDatabase (database );
196
- MongoCollection <Document > collection = db .getCollection (subscriptionCollectionName );
175
+ MongoCollection <Document > collection = getCollection (subscriptionCollectionName );
197
176
Bson filter = Filters .eq ("subscriptionName" , subscriptionName );
198
177
Document document = collection .find (filter ).first ();
199
178
return document .toJson ();
200
179
}
180
+
181
+ /**
182
+ * Returns the size of the waitlist.
183
+ *
184
+ * @return int of the size of the waitlist.
185
+ */
186
+ public int waitListSize () {
187
+ List <Document > documents = getDocumentsFromCollection (waitlistCollectionName );
188
+ return documents .size ();
189
+ }
190
+
191
+ private List <Document > getDocumentsFromCollection (String collectionName ) {
192
+ MongoCollection <Document > collection = getCollection (collectionName );
193
+ List <Document > documents = collection .find ().into (new ArrayList <>());
194
+ return documents ;
195
+ }
196
+
197
+ private MongoCollection <Document > getCollection (String collectionName ) {
198
+ MongoClientURI uri = new MongoClientURI (mongoProperties .getUri ());
199
+ mongoClient = new MongoClient (uri );
200
+ MongoDatabase db = mongoClient .getDatabase (database );
201
+ MongoCollection <Document > collection = db .getCollection (collectionName );
202
+ return collection ;
203
+ }
204
+
201
205
}
0 commit comments