1 |
|
package org.europa.together.application; |
2 |
|
|
3 |
|
import static org.junit.jupiter.api.Assertions.*; |
4 |
|
import static com.google.code.beanmatchers.BeanMatchers.*; |
5 |
|
import java.sql.SQLException; |
6 |
|
import java.util.ArrayList; |
7 |
|
import java.util.List; |
8 |
|
import org.europa.together.JUnit5Preperator; |
9 |
|
import org.europa.together.business.DatabaseActions; |
10 |
|
import org.europa.together.business.JsonWebToken; |
11 |
|
import org.europa.together.business.Logger; |
12 |
|
import org.europa.together.domain.ConfigurationDO; |
13 |
|
import org.europa.together.domain.LogLevel; |
14 |
|
import org.europa.together.exceptions.JsonProcessingException; |
15 |
|
import static org.hamcrest.MatcherAssert.assertThat; |
16 |
|
import org.junit.jupiter.api.AfterAll; |
17 |
|
import org.junit.jupiter.api.AfterEach; |
18 |
|
import org.junit.jupiter.api.Assumptions; |
19 |
|
import org.junit.jupiter.api.BeforeAll; |
20 |
|
import org.junit.jupiter.api.BeforeEach; |
21 |
|
import org.junit.jupiter.api.Test; |
22 |
|
import org.junit.jupiter.api.extension.ExtendWith; |
23 |
|
import org.springframework.beans.factory.annotation.Autowired; |
24 |
|
import org.springframework.test.context.ContextConfiguration; |
25 |
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
26 |
|
|
27 |
|
@SuppressWarnings("unchecked") |
28 |
|
@ExtendWith({JUnit5Preperator.class}) |
29 |
|
@ExtendWith(SpringExtension.class) |
30 |
|
@ContextConfiguration(locations = {"/applicationContext.xml"}) |
|
|
| 100% |
Uncovered Elements: 0 (43) |
Complexity: 11 |
Complexity Density: 0.34 |
|
31 |
|
public class NimbusJwtTest { |
32 |
|
|
33 |
|
private static final Logger LOGGER = new LogbackLogger(NimbusJwtTest.class); |
34 |
|
|
35 |
|
private static final String FLUSH_TABLE |
36 |
|
= "TRUNCATE TABLE " + ConfigurationDO.TABLE_NAME + ";"; |
37 |
|
private static final String FILE |
38 |
|
= "org/europa/together/sql/jwt-test.sql"; |
39 |
|
|
40 |
|
private static DatabaseActions jdbcActions = new JdbcActions(); |
41 |
|
|
42 |
|
private String jws |
43 |
|
= "eyJhbGciOiJIUzUxMiJ9.e0hhbGxvIFdvcmxkIEpTT059.H4_JPE4135eUQUnAJrQD9nkLavNuEBYxxp4FnU7k7FOZbjWtiE-F6DjnThaxBpTRzuGpck0H3lud_H0U7_mHbQ"; |
44 |
|
|
45 |
|
@Autowired |
46 |
|
JsonWebToken jsonWebToken; |
47 |
|
|
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
49 |
1 |
@BeforeAll... |
50 |
|
static void setUp() { |
51 |
1 |
Assumptions.assumeTrue(jdbcActions.connect("test"), "JDBC DBMS Connection failed."); |
52 |
1 |
jdbcActions.executeSqlFromClasspath(FILE); |
53 |
|
|
54 |
1 |
LOGGER.log("Assumptions passed ...\n\n", LogLevel.DEBUG); |
55 |
|
} |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
1 |
@AfterAll... |
58 |
|
static void tearDown() throws SQLException { |
59 |
1 |
jdbcActions.executeQuery(FLUSH_TABLE); |
60 |
|
} |
61 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
62 |
7 |
@BeforeEach... |
63 |
|
void testCaseInitialization() { |
64 |
|
} |
65 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
66 |
7 |
@AfterEach... |
67 |
|
void testCaseTermination() { |
68 |
|
} |
69 |
|
|
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
71 |
1 |
@Test... |
72 |
|
void constructor() { |
73 |
1 |
LOGGER.log("TEST CASE: constructor", LogLevel.DEBUG); |
74 |
1 |
assertThat(NimbusJwt.class, hasValidBeanConstructor()); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
77 |
1 |
@Test... |
78 |
|
void buildHMAC512SignedJws() throws Exception { |
79 |
1 |
LOGGER.log("TEST CASE: buildHMAC512SignedJws", LogLevel.DEBUG); |
80 |
|
|
81 |
1 |
assertEquals(jws, jsonWebToken.buildHMAC512SignedJws("{Hallo World JSON}")); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
void buildHMAC512SignedJwt() throws Exception { |
86 |
1 |
LOGGER.log("TEST CASE: buildHMAC512SignedJwt", LogLevel.DEBUG); |
87 |
|
|
88 |
1 |
String issuer = "https://togehter-platform.org"; |
89 |
1 |
String subject = "togetherPlatform-API"; |
90 |
1 |
List<String> audience = new ArrayList<>(); |
91 |
1 |
audience.add("https://app.togehter-platform.org"); |
92 |
|
|
93 |
1 |
assertEquals(383, jsonWebToken.buildHMAC512SignedJwt(issuer, subject, audience).length()); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
96 |
1 |
@Test... |
97 |
|
void parseHMAC512SingedJws() throws Exception { |
98 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJws", LogLevel.DEBUG); |
99 |
|
|
100 |
1 |
assertEquals("{Hallo World JSON}", jsonWebToken.parseHMAC512SingedJws(jws)); |
101 |
|
} |
102 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
103 |
1 |
@Test... |
104 |
|
void failParseHMAC512SingedJws() throws Exception { |
105 |
1 |
LOGGER.log("TEST CASE: failParseHMAC512SingedJws", LogLevel.DEBUG); |
106 |
|
|
107 |
1 |
assertThrows(Exception.class, () -> { |
108 |
1 |
jsonWebToken.parseHMAC512SingedJws(jws.substring(1)); |
109 |
|
}); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
void parseHMAC512SingedJwt() throws Exception { |
114 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG); |
115 |
|
|
116 |
1 |
String issuer = "https://togehter-platform.org"; |
117 |
1 |
String subject = "togetherPlatform-API"; |
118 |
1 |
List<String> audience = new ArrayList<>(); |
119 |
1 |
audience.add("https://app.togehter-platform.org"); |
120 |
1 |
String jwt = jsonWebToken.buildHMAC512SignedJwt(issuer, subject, audience); |
121 |
|
|
122 |
1 |
String payload = jsonWebToken.parseHMAC512SingedJwt(jwt); |
123 |
1 |
LOGGER.log("JWT Payload: " + payload, LogLevel.DEBUG); |
124 |
1 |
assertEquals("{\"sub\":\"togetherPlatform-API\",", payload.substring(0, 30)); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
127 |
1 |
@Test... |
128 |
|
void failparseHMAC512SingedJwt() throws Exception { |
129 |
1 |
LOGGER.log("TEST CASE: parseHMAC512SingedJwt", LogLevel.DEBUG); |
130 |
|
|
131 |
1 |
assertThrows(Exception.class, () -> { |
132 |
1 |
String jwt = jsonWebToken.buildHMAC512SignedJwt("", "", null); |
133 |
1 |
jsonWebToken.parseHMAC512SingedJwt(jwt.substring(1)); |
134 |
|
}); |
135 |
|
} |
136 |
|
} |