36
36
import org .slf4j .Logger ;
37
37
import org .slf4j .LoggerFactory ;
38
38
import org .springframework .beans .factory .annotation .Autowired ;
39
+ import org .springframework .beans .factory .annotation .Value ;
39
40
import org .springframework .boot .test .context .SpringBootTest ;
40
41
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
41
42
45
46
import com .ericsson .ei .mongodbhandler .MongoDBHandler ;
46
47
import com .ericsson .ei .services .ISubscriptionService ;
47
48
import com .fasterxml .jackson .databind .ObjectMapper ;
49
+ import com .mongodb .BasicDBObject ;
48
50
import com .mongodb .MongoClient ;
49
51
50
52
import de .flapdoodle .embed .mongo .distribution .Version ;
@@ -56,6 +58,10 @@ public class SubscriptionServiceTest {
56
58
57
59
final static Logger LOGGER = (Logger ) LoggerFactory .getLogger (SubscriptionServiceTest .class );
58
60
61
+ @ Value ("${spring.data.mongodb.database}" ) private String dataBaseName ;
62
+
63
+ @ Value ("${subscription.collection.repeatFlagHandlerName}" ) private String repeatFlagHandlerCollection ;
64
+
59
65
String subscriptionName ;
60
66
String userName = "ABC" ; // initialized with "ABC", as test json file has this name as userName
61
67
@@ -226,6 +232,82 @@ public void testDeleteSubscriptionsByName() {
226
232
} catch (IOException | JSONException e ) {
227
233
}
228
234
}
235
+
236
+ @ Test
237
+ public void testDeleteSubscriptionsByNameAndCleanUpOfRepeatHandlerDb () {
238
+ // Insert Subscription
239
+ Subscription subscription2 ;
240
+ try {
241
+ subscription2 = mapper .readValue (jsonArray .getJSONObject (0 ).toString (), Subscription .class );
242
+
243
+
244
+ subscriptionService .addSubscription (subscription2 );
245
+ String expectedSubscriptionName = subscription2 .getSubscriptionName ();
246
+
247
+ // Inserting a matched subscription AggrObjIds document to RepeatHandlerDb database collection.
248
+ BasicDBObject docInput = new BasicDBObject ();
249
+ docInput .put ("subscriptionId" , expectedSubscriptionName );
250
+ mongoDBHandler .insertDocument (dataBaseName , repeatFlagHandlerCollection , docInput .toString ());
251
+
252
+ boolean deleteSubscription = subscriptionService .deleteSubscription (expectedSubscriptionName , "" );
253
+ assertEquals (deleteSubscription , true );
254
+
255
+ // Checking if it removes the Subscription Matched AggrObjIds document from RepeatHandlerDb database collection.
256
+ String subscriptionIdMatchedAggrIdObjQuery = "{ \" subscriptionId\" : \" " + expectedSubscriptionName + "\" }" ;
257
+ ArrayList <String > result = mongoDBHandler .find (dataBaseName , repeatFlagHandlerCollection , subscriptionIdMatchedAggrIdObjQuery );
258
+
259
+ assertEquals ("[]" , result .toString ());
260
+ } catch (IOException | JSONException e ) {
261
+ LOGGER .error (e .getMessage (), e );
262
+ }
263
+ }
264
+
265
+ @ Test
266
+ public void testUpdateSubscriptionAndCleanUpOfRepeatHandlerDb () {
267
+ Subscription subscription ;
268
+ try {
269
+ // Insert Subscription
270
+ Subscription subscription2 = mapper .readValue (jsonArray .getJSONObject (0 ).toString (), Subscription .class );
271
+ String expectedSubscriptionName = subscription2 .getSubscriptionName ();
272
+ subscriptionService .addSubscription (subscription2 );
273
+ // Fetch the inserted subscription
274
+ subscription2 = null ;
275
+ subscription2 = subscriptionService .getSubscription (expectedSubscriptionName , null );
276
+ subscriptionName = subscription2 .getSubscriptionName ();
277
+ String subscriptionUserName = subscription2 .getUserName ();
278
+
279
+ assertEquals (subscriptionName , expectedSubscriptionName );
280
+
281
+ // Inserting a matched subscription AggrObjIds document to RepeatHandlerDb database collection.
282
+ BasicDBObject docInput = new BasicDBObject ();
283
+ docInput .put ("subscriptionId" , subscriptionName );
284
+ mongoDBHandler .insertDocument (dataBaseName , repeatFlagHandlerCollection , docInput .toString ());
285
+
286
+ // Updating subscription2(subscriptionName=Subscription_Test) with
287
+ // the subscription(subscriptionName=Subscription_Test_Modify)
288
+ subscription = mapper .readValue (jsonArray .getJSONObject (1 ).toString (), Subscription .class );
289
+ String expectedModifiedSubscriptionName = subscription2 .getSubscriptionName ();
290
+ boolean addSubscription = subscriptionService .modifySubscription (subscription , subscriptionName , null );
291
+
292
+ // test update done successfully
293
+ assertEquals (addSubscription , true );
294
+ subscription = null ;
295
+ subscription = subscriptionService .getSubscription (expectedModifiedSubscriptionName , null );
296
+ subscriptionName = subscription .getSubscriptionName ();
297
+ assertEquals (subscriptionName , expectedModifiedSubscriptionName );
298
+
299
+ // Checking if it removes the Subscription Matched AggrObjIds document from RepeatHandlerDb database collection.
300
+ String subscriptionIdMatchedAggrIdObjQuery = "{ \" subscriptionId\" : \" " + subscriptionName + "\" }" ;
301
+ List <String > result = mongoDBHandler .find (dataBaseName , repeatFlagHandlerCollection , subscriptionIdMatchedAggrIdObjQuery );
302
+
303
+ assertEquals ("[]" , result .toString ());
304
+
305
+ // deleting the test data
306
+ deleteSubscriptionsByName (subscriptionName , "" );
307
+ } catch (Exception e ) {
308
+ }
309
+ }
310
+
229
311
230
312
@ Test (expected = SubscriptionNotFoundException .class )
231
313
public void testExceptionGetSubscriptionsByName () throws SubscriptionNotFoundException {
0 commit comments