51
51
import org .springframework .test .web .servlet .request .MockMvcRequestBuilders ;
52
52
import org .springframework .util .LinkedMultiValueMap ;
53
53
import org .springframework .util .MultiValueMap ;
54
-
55
54
import com .ericsson .ei .App ;
56
55
import com .ericsson .ei .controller .model .QueryResponse ;
57
56
import com .ericsson .ei .exception .SubscriptionValidationException ;
71
70
import de .flapdoodle .embed .mongo .tests .MongodForTestsFactory ;
72
71
73
72
@ RunWith (SpringJUnit4ClassRunner .class )
74
- @ SpringBootTest (classes = {
75
- App .class
76
- })
73
+ @ SpringBootTest (classes = { App .class })
77
74
@ AutoConfigureMockMvc
78
75
public class SubscriptionHandlerTest {
79
76
80
77
private static final Logger LOGGER = (Logger ) LoggerFactory .getLogger (SubscriptionHandlerTest .class );
81
78
private static final String aggregatedPath = "src/test/resources/AggregatedObject.json" ;
82
79
private static final String subscriptionPath = "src/test/resources/SubscriptionObject.json" ;
80
+ private static final String subscriptionPathForAuthorization = "src/test/resources/SubscriptionObjectForAuthorization.json" ;
83
81
private static final String DB_NAME = "MissedNotification" ;
84
82
private static final String COLLECTION_NAME = "Notification" ;
85
83
private static final String REGEX = "^\" |\" $" ;
86
84
private static final String MISSED_NOTIFICATION_URL = "/queryMissedNotifications" ;
87
85
private static final int STATUS_OK = 200 ;
88
86
private static String aggregatedObject ;
89
87
private static String subscriptionData ;
88
+ private static String subscriptionDataForAuthorization ;
90
89
private static String url ;
91
90
private static String headerContentMediaType ;
91
+ private static String urlAuthorization ;
92
+ private static String headerContentMediaTypeAuthorization ;
92
93
private static MongodForTestsFactory testsFactory ;
93
94
private static MongoClient mongoClient = null ;
95
+ private static final String formkey = "Authorization" ;
96
+ private static final String formvalue = "Basic XX0=" ;
94
97
95
98
@ Autowired
96
99
private RunSubscription runSubscription ;
@@ -144,6 +147,7 @@ public static void setUpEmbeddedMongo() throws JSONException, IOException {
144
147
subscriptionData = FileUtils .readFileToString (new File (subscriptionPath ), "UTF-8" );
145
148
subscriptionRepeatFlagTrueData = FileUtils .readFileToString (new File (subscriptionRepeatFlagTruePath ),
146
149
"UTF-8" );
150
+ subscriptionDataForAuthorization = FileUtils .readFileToString (new File (subscriptionPathForAuthorization ), "UTF-8" );
147
151
subscriptionDataEmail = FileUtils .readFileToString (new File (subscriptionPathForEmail ), "UTF-8" );
148
152
} catch (Exception e ) {
149
153
LOGGER .error (e .getMessage (), e );
@@ -152,6 +156,8 @@ public static void setUpEmbeddedMongo() throws JSONException, IOException {
152
156
153
157
url = new JSONObject (subscriptionData ).getString ("notificationMeta" ).replaceAll (REGEX , "" );
154
158
headerContentMediaType = new JSONObject (subscriptionData ).getString ("restPostBodyMediaType" );
159
+ urlAuthorization = new JSONObject (subscriptionDataForAuthorization ).getString ("notificationMeta" ).replaceAll (REGEX , "" );
160
+ headerContentMediaTypeAuthorization = new JSONObject (subscriptionDataForAuthorization ).getString ("restPostBodyMediaType" );
155
161
}
156
162
157
163
@ BeforeClass
@@ -288,16 +294,27 @@ public void sendMailTest() {
288
294
289
295
@ Test
290
296
public void testRestPostTrigger () throws IOException {
291
- when (springRestTemplate .postDataMultiValue (url , mapNotificationMessage (), headerContentMediaType ))
292
- .thenReturn (STATUS_OK );
297
+ when (springRestTemplate .postDataMultiValue (url , mapNotificationMessage (subscriptionData ),
298
+ headerContentMediaType )) .thenReturn (STATUS_OK );
293
299
subscription .informSubscriber (aggregatedObject , new ObjectMapper ().readTree (subscriptionData ));
294
- verify (springRestTemplate , times (1 )).postDataMultiValue (url , mapNotificationMessage (), headerContentMediaType );
300
+ verify (springRestTemplate , times (1 )).postDataMultiValue (url , mapNotificationMessage (subscriptionData ),
301
+ headerContentMediaType );
302
+ }
303
+
304
+ @ Test
305
+ public void testRestPostTriggerForAuthorization () throws IOException {
306
+ when (springRestTemplate .postDataMultiValue (urlAuthorization , mapNotificationMessage (subscriptionDataForAuthorization ),
307
+ headerContentMediaTypeAuthorization , formkey , formvalue )).thenReturn (STATUS_OK );
308
+ subscription .informSubscriber (aggregatedObject , new ObjectMapper ().readTree (subscriptionDataForAuthorization ));
309
+ verify (springRestTemplate , times (1 )).postDataMultiValue (urlAuthorization ,
310
+ mapNotificationMessage (subscriptionDataForAuthorization ), headerContentMediaTypeAuthorization , formkey , formvalue );
295
311
}
296
312
297
313
@ Test
298
314
public void testRestPostTriggerFailure () throws IOException {
299
315
subscription .informSubscriber (aggregatedObject , new ObjectMapper ().readTree (subscriptionData ));
300
- verify (springRestTemplate , times (4 )).postDataMultiValue (url , mapNotificationMessage (), headerContentMediaType );
316
+ verify (springRestTemplate , times (4 )).postDataMultiValue (url , mapNotificationMessage (subscriptionData ),
317
+ headerContentMediaType );
301
318
assertFalse (mongoDBHandler .getAllDocuments (DB_NAME , COLLECTION_NAME ).isEmpty ());
302
319
}
303
320
@@ -315,16 +332,18 @@ public void testQueryMissedNotificationEndPoint() throws Exception {
315
332
assertEquals (HttpStatus .OK .value (), result .getResponse ().getStatus ());
316
333
}
317
334
318
- private MultiValueMap <String , String > mapNotificationMessage () throws IOException {
335
+ private MultiValueMap <String , String > mapNotificationMessage (String data ) throws IOException {
319
336
MultiValueMap <String , String > mapNotificationMessage = new LinkedMultiValueMap <>();
320
337
321
- ArrayNode arrNode = (ArrayNode ) new ObjectMapper ().readTree (subscriptionData )
322
- .get ("notificationMessageKeyValues" );
338
+ ArrayNode arrNode = (ArrayNode ) new ObjectMapper ().readTree (data ).get ("notificationMessageKeyValues" );
323
339
if (arrNode .isArray ()) {
324
340
for (final JsonNode objNode : arrNode ) {
325
- mapNotificationMessage .add (objNode .get ("formkey" ).toString ().replaceAll (REGEX , "" ), jmespath
326
- .runRuleOnEvent (objNode .get ("formvalue" ).toString ().replaceAll (REGEX , "" ), aggregatedObject )
327
- .toString ().replaceAll (REGEX , "" ));
341
+ if (!objNode .get ("formkey" ).toString ().replaceAll (REGEX , "" ).equals ("Authorization" )) {
342
+
343
+ mapNotificationMessage .add (objNode .get ("formkey" ).toString ().replaceAll (REGEX , "" ), jmespath
344
+ .runRuleOnEvent (objNode .get ("formvalue" ).toString ().replaceAll (REGEX , "" ), aggregatedObject )
345
+ .toString ().replaceAll (REGEX , "" ));
346
+ }
328
347
}
329
348
}
330
349
return mapNotificationMessage ;
0 commit comments