1 package org.openecomp.sdc.common.log.elements;
4 import org.junit.runner.RunWith;
5 import org.mockito.Mock;
6 import org.mockito.junit.MockitoJUnitRunner;
7 import org.onap.logging.ref.slf4j.ONAPLogConstants;
8 import org.openecomp.sdc.common.log.enums.ConstantsLogging;
10 import javax.servlet.http.HttpServletRequest;
12 import static org.assertj.core.api.Assertions.assertThat;
13 import static org.mockito.Mockito.when;
15 @RunWith(MockitoJUnitRunner.class)
16 public class LoggerBaseTest {
19 HttpServletRequest httpServletRequest;
21 private static final String ONAP_REQ_ID_VALUE = "onapReqId";
22 private static final String REQ_ID_VALUE = "reqId";
23 private static final String TRANSACTION_RE_IQ_VALUE = "transactionReqId";
24 private static final String ECOMP_REQ_ID_VALUE = "ecompReqId";
25 private static final String USER_ID_VALUE = "userId";
26 private static final String ONAP_PARTNER_NAME_VALUE = "partnerName";
27 private static final String USER_UGENT = "userAgent";
30 public void testGetRequestIfFromOnapHeader() {
31 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.REQUEST_ID)).thenReturn(ONAP_REQ_ID_VALUE);
32 when(httpServletRequest.getHeader(ConstantsLogging.X_REQUEST_ID)).thenReturn(REQ_ID_VALUE);
33 when(httpServletRequest.getHeader(ConstantsLogging.X_TRANSACTION_ID_HEADER)).thenReturn(TRANSACTION_RE_IQ_VALUE);
34 when(httpServletRequest.getHeader(ConstantsLogging.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(ECOMP_REQ_ID_VALUE);
36 String requestId = LoggerBase.getRequestId(httpServletRequest);
37 assertThat(requestId).isEqualTo(ONAP_REQ_ID_VALUE);
41 public void testGetRequestIfFromReqIdHeader() {
42 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.REQUEST_ID)).thenReturn(null);
43 when(httpServletRequest.getHeader(ConstantsLogging.X_REQUEST_ID)).thenReturn(REQ_ID_VALUE);
44 when(httpServletRequest.getHeader(ConstantsLogging.X_TRANSACTION_ID_HEADER)).thenReturn(TRANSACTION_RE_IQ_VALUE);
45 when(httpServletRequest.getHeader(ConstantsLogging.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(ECOMP_REQ_ID_VALUE);
47 String requestId = LoggerBase.getRequestId(httpServletRequest);
48 assertThat(requestId).isEqualTo(REQ_ID_VALUE);
52 public void testGetRequestIfFromTransactionIdHeader() {
53 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.REQUEST_ID)).thenReturn(null);
54 when(httpServletRequest.getHeader(ConstantsLogging.X_REQUEST_ID)).thenReturn(null);
55 when(httpServletRequest.getHeader(ConstantsLogging.X_TRANSACTION_ID_HEADER)).thenReturn(TRANSACTION_RE_IQ_VALUE);
56 when(httpServletRequest.getHeader(ConstantsLogging.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(ECOMP_REQ_ID_VALUE);
58 String requestId = LoggerBase.getRequestId(httpServletRequest);
59 assertThat(requestId).isEqualTo(TRANSACTION_RE_IQ_VALUE);
63 public void testGetRequestIfFromEcompIdHeader() {
64 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.REQUEST_ID)).thenReturn(null);
65 when(httpServletRequest.getHeader(ConstantsLogging.X_REQUEST_ID)).thenReturn(null);
66 when(httpServletRequest.getHeader(ConstantsLogging.X_TRANSACTION_ID_HEADER)).thenReturn(null);
67 when(httpServletRequest.getHeader(ConstantsLogging.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(ECOMP_REQ_ID_VALUE);
69 String requestId = LoggerBase.getRequestId(httpServletRequest);
70 assertThat(requestId).isEqualTo(ECOMP_REQ_ID_VALUE);
74 public void testGetRequestIfFromRandonHeader() {
75 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.REQUEST_ID)).thenReturn(null);
76 when(httpServletRequest.getHeader(ConstantsLogging.X_REQUEST_ID)).thenReturn(null);
77 when(httpServletRequest.getHeader(ConstantsLogging.X_TRANSACTION_ID_HEADER)).thenReturn(null);
78 when(httpServletRequest.getHeader(ConstantsLogging.X_ECOMP_REQUEST_ID_HEADER)).thenReturn(null);
80 String requestId = LoggerBase.getRequestId(httpServletRequest);
81 assertThat(requestId).isNotEmpty();
85 public void testPartnerNameFromUserHeader() {
86 when(httpServletRequest.getHeader(ConstantsLogging.USER_ID_HEADER)).thenReturn(USER_ID_VALUE);
87 when(httpServletRequest.getHeader(ConstantsLogging.USER_AGENT_HEADER)).thenReturn(USER_UGENT);
88 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.PARTNER_NAME)).thenReturn(ONAP_PARTNER_NAME_VALUE);
90 String partnerName = LoggerBase.getPartnerName(httpServletRequest);
91 assertThat(partnerName).isEqualTo(USER_ID_VALUE);
95 public void testPartnerNameFromOnapPartnerNameHeader() {
96 when(httpServletRequest.getHeader(ConstantsLogging.USER_ID_HEADER)).thenReturn(null);
97 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.PARTNER_NAME)).thenReturn(ONAP_PARTNER_NAME_VALUE);
99 String partnerName = LoggerBase.getPartnerName(httpServletRequest);
100 assertThat(partnerName).isEqualTo(ONAP_PARTNER_NAME_VALUE);
104 public void testPartnerNameFromReqUriHeader() {
105 when(httpServletRequest.getHeader(ConstantsLogging.USER_ID_HEADER)).thenReturn(null);
106 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.PARTNER_NAME)).thenReturn(null);
107 when(httpServletRequest.getHeader(ConstantsLogging.USER_AGENT_HEADER)).thenReturn(USER_UGENT);
109 String partnerName = LoggerBase.getPartnerName(httpServletRequest);
110 assertThat(partnerName).isEqualTo(USER_UGENT);
114 public void testPartnerNameUnknown() {
115 when(httpServletRequest.getHeader(ConstantsLogging.USER_ID_HEADER)).thenReturn(null);
116 when(httpServletRequest.getHeader(ONAPLogConstants.Headers.PARTNER_NAME)).thenReturn(null);
117 when(httpServletRequest.getHeader(ConstantsLogging.USER_AGENT_HEADER)).thenReturn(null);
119 String partnerName = LoggerBase.getPartnerName(httpServletRequest);
120 assertThat(partnerName).isEqualTo(ConstantsLogging.PartnerName_Unknown);