|
1 | 1 | package com.sap.hcp.cf.logging.servlet.dynlog;
|
2 | 2 |
|
3 |
| -import static org.junit.Assert.assertEquals; |
| 3 | +import static org.junit.Assert.*; |
4 | 4 |
|
5 | 5 | import java.security.KeyPair;
|
6 | 6 | import java.security.KeyPairGenerator;
|
7 | 7 | import java.security.NoSuchAlgorithmException;
|
8 | 8 | import java.security.NoSuchProviderException;
|
| 9 | +import java.security.PublicKey; |
| 10 | +import java.security.interfaces.RSAPublicKey; |
9 | 11 | import java.util.Date;
|
10 | 12 |
|
11 |
| -import javax.xml.bind.DatatypeConverter; |
12 |
| - |
13 | 13 | import org.junit.After;
|
14 | 14 | import org.junit.Before;
|
15 | 15 | import org.junit.Test;
|
16 | 16 | import org.junit.runner.RunWith;
|
17 |
| -import org.mockito.Mock; |
18 | 17 | import org.mockito.Mockito;
|
19 | 18 | import org.mockito.runners.MockitoJUnitRunner;
|
20 | 19 | import org.slf4j.MDC;
|
21 | 20 |
|
| 21 | +import com.auth0.jwt.interfaces.DecodedJWT; |
22 | 22 | import com.sap.hcp.cf.logging.common.helper.DynamicLogLevelHelper;
|
23 |
| -import com.sap.hcp.cf.logging.common.helper.Environment; |
24 | 23 |
|
25 | 24 | @RunWith(MockitoJUnitRunner.class)
|
26 | 25 | public class DynamicLogLevelProcessorTest extends Mockito {
|
27 | 26 |
|
28 |
| - @Mock |
29 |
| - private Environment environment; |
30 |
| - |
31 | 27 | private DynamicLogLevelProcessor processor;
|
32 | 28 |
|
33 | 29 | private String token;
|
34 | 30 |
|
| 31 | + private KeyPair keyPair; |
| 32 | + |
35 | 33 | @Before
|
36 | 34 | public void setup() throws NoSuchAlgorithmException, NoSuchProviderException, DynamicLogLevelException {
|
37 |
| - KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
38 |
| - String keyBase64 = DatatypeConverter.printBase64Binary(keyPair.getPublic().getEncoded()); |
| 35 | + this.keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
39 | 36 | Date issuedAt = new Date();
|
40 | 37 | Date expiresAt = new Date(new Date().getTime() + 10000);
|
41 |
| - this.token = TokenCreator.createToken(keyPair, "issuer", issuedAt, expiresAt, "TRACE", "myPrefix"); |
42 |
| - when(environment.getVariable("DYN_LOG_LEVEL_KEY")).thenReturn(keyBase64); |
43 |
| - when(environment.getVariable("DYN_LOG_HEADER")).thenReturn("SAP-LOG-LEVEL"); |
44 |
| - processor = new DynamicLogLevelProcessor(new DynLogEnvironment(environment)); |
| 38 | + this.token = TokenCreator.createToken(keyPair, "issuer", issuedAt, expiresAt, "TRACE", "myPrefix"); |
| 39 | + this.processor = new DynamicLogLevelProcessor(getRSAPublicKey(keyPair)); |
| 40 | + } |
| 41 | + |
| 42 | + private static RSAPublicKey getRSAPublicKey(KeyPair keyPair) { |
| 43 | + PublicKey publicKey = keyPair.getPublic(); |
| 44 | + if (publicKey instanceof RSAPublicKey) { |
| 45 | + return (RSAPublicKey) publicKey; |
| 46 | + } |
| 47 | + return null; |
45 | 48 | }
|
46 | 49 |
|
47 | 50 | @After
|
48 |
| - public void tearDown() { |
| 51 | + public void removeDynamicLogLevelFromMDC() { |
| 52 | + processor.removeDynamicLogLevelFromMDC(); |
49 | 53 | }
|
50 | 54 |
|
51 | 55 | @Test
|
52 |
| - public void testCopyDynamicLogLevelToMDC() throws Exception { |
| 56 | + public void copiesDynamicLogLevelToMDC() throws Exception { |
53 | 57 | processor.copyDynamicLogLevelToMDC(token);
|
54 | 58 | assertEquals("TRACE", MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY));
|
55 | 59 | }
|
56 | 60 |
|
57 | 61 | @Test
|
58 |
| - public void testDeleteDynamicLogLevelFromMDC() throws Exception { |
| 62 | + public void deletesDynamicLogLevelFromMDC() throws Exception { |
59 | 63 | processor.copyDynamicLogLevelToMDC(token);
|
60 | 64 | processor.removeDynamicLogLevelFromMDC();
|
61 | 65 | assertEquals(null, MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY));
|
62 | 66 | }
|
63 | 67 |
|
64 |
| - @Test |
65 |
| - public void testCopyDynamicLogPackagesToMDC() throws Exception { |
66 |
| - processor.copyDynamicLogLevelToMDC(token); |
67 |
| - assertEquals("myPrefix", MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES)); |
| 68 | + @Test |
| 69 | + public void copiesDynamicLogPackagesToMDC() throws Exception { |
| 70 | + processor.copyDynamicLogLevelToMDC(token); |
| 71 | + assertEquals("myPrefix", MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_PREFIXES)); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void doesNotCopyDynamicLogLevelOnInvalidJwt() throws Exception { |
| 76 | + KeyPair invalidKeyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair(); |
| 77 | + new DynamicLogLevelProcessor(getRSAPublicKey(invalidKeyPair)).copyDynamicLogLevelToMDC(token); |
| 78 | + assertEquals(null, MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY)); |
| 79 | + } |
68 | 80 |
|
69 |
| - } |
| 81 | + @Test |
| 82 | + public void doesNotCopyDynamicLogLevelOnCustomException() throws Exception { |
| 83 | + DynamicLogLevelProcessor myProcessor = new DynamicLogLevelProcessor(getRSAPublicKey(keyPair)) { |
| 84 | + @Override |
| 85 | + protected void processJWT(DecodedJWT jwt) throws DynamicLogLevelException { |
| 86 | + throw new DynamicLogLevelException("Always fail in this test-case."); |
| 87 | + } |
| 88 | + }; |
| 89 | + myProcessor.copyDynamicLogLevelToMDC(token); |
| 90 | + assertEquals(null, MDC.get(DynamicLogLevelHelper.MDC_DYNAMIC_LOG_LEVEL_KEY)); |
| 91 | + } |
70 | 92 | }
|
0 commit comments