Catalog alignment
[sdc.git] / common-app-logging / src / test / java / org / openecomp / sdc / common / log / elements / LoggerBaseTest.java
1 package org.openecomp.sdc.common.log.elements;
2
3 import org.junit.Test;
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;
9
10 import javax.servlet.http.HttpServletRequest;
11
12 import static org.assertj.core.api.Assertions.assertThat;
13 import static org.mockito.Mockito.when;
14
15 @RunWith(MockitoJUnitRunner.class)
16 public class LoggerBaseTest {
17
18     @Mock
19     HttpServletRequest httpServletRequest;
20
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";
28
29     @Test
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);
35
36         String requestId = LoggerBase.getRequestId(httpServletRequest);
37         assertThat(requestId).isEqualTo(ONAP_REQ_ID_VALUE);
38     }
39
40     @Test
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);
46
47         String requestId = LoggerBase.getRequestId(httpServletRequest);
48         assertThat(requestId).isEqualTo(REQ_ID_VALUE);
49     }
50
51     @Test
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);
57
58         String requestId = LoggerBase.getRequestId(httpServletRequest);
59         assertThat(requestId).isEqualTo(TRANSACTION_RE_IQ_VALUE);
60     }
61
62     @Test
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);
68
69         String requestId = LoggerBase.getRequestId(httpServletRequest);
70         assertThat(requestId).isEqualTo(ECOMP_REQ_ID_VALUE);
71     }
72
73     @Test
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);
79
80         String requestId = LoggerBase.getRequestId(httpServletRequest);
81         assertThat(requestId).isNotEmpty();
82     }
83
84     @Test
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);
89
90         String partnerName = LoggerBase.getPartnerName(httpServletRequest);
91         assertThat(partnerName).isEqualTo(USER_ID_VALUE);
92     }
93
94     @Test
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);
98
99         String partnerName = LoggerBase.getPartnerName(httpServletRequest);
100         assertThat(partnerName).isEqualTo(ONAP_PARTNER_NAME_VALUE);
101     }
102
103     @Test
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);
108
109         String partnerName = LoggerBase.getPartnerName(httpServletRequest);
110         assertThat(partnerName).isEqualTo(USER_UGENT);
111     }
112
113     @Test
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);
118
119         String partnerName = LoggerBase.getPartnerName(httpServletRequest);
120         assertThat(partnerName).isEqualTo(ConstantsLogging.PartnerName_Unknown);
121     }
122 }