16
16
*/
17
17
package org .apache .logging .log4j .core .appender ;
18
18
19
+ import static org .assertj .core .api .AssertionsForInterfaceTypes .assertThat ;
20
+ import static org .junit .jupiter .api .Assertions .*;
21
+ import static org .mockito .Mockito .*;
22
+
23
+ import java .net .MalformedURLException ;
24
+ import java .net .URL ;
19
25
import org .apache .logging .log4j .Level ;
20
26
import org .apache .logging .log4j .core .Layout ;
21
27
import org .apache .logging .log4j .core .config .Configuration ;
22
28
import org .apache .logging .log4j .core .config .DefaultConfiguration ;
23
29
import org .apache .logging .log4j .core .config .Property ;
24
- import org .apache .logging .log4j .core .net .ssl .SslConfiguration ;
25
30
import org .apache .logging .log4j .core .layout .JsonLayout ;
31
+ import org .apache .logging .log4j .core .net .ssl .SslConfiguration ;
26
32
import org .apache .logging .log4j .status .StatusLogger ;
27
33
import org .apache .logging .log4j .test .ListStatusListener ;
28
34
import org .apache .logging .log4j .test .junit .UsingStatusListener ;
29
35
import org .junit .jupiter .api .Test ;
30
36
import org .mockito .Mockito ;
31
37
32
- import java .net .MalformedURLException ;
33
- import java .net .URL ;
34
-
35
- import static org .assertj .core .api .AssertionsForInterfaceTypes .assertThat ;
36
- import static org .junit .jupiter .api .Assertions .*;
37
- import static org .mockito .Mockito .*;
38
-
39
38
class HttpAppenderBuilderTest {
40
39
41
40
// Mock the LOGGER to verify error logs
42
41
private final StatusLogger mockStatusLogger = Mockito .mock (StatusLogger .class );
43
42
44
43
private HttpAppender .Builder <?> getBuilder () {
45
44
Configuration mockConfig = new DefaultConfiguration ();
46
- return HttpAppender .newBuilder ()
47
- .setConfiguration (mockConfig )
48
- .setName ("TestHttpAppender" ); // Name is required
45
+ return HttpAppender .newBuilder ().setConfiguration (mockConfig ).setName ("TestHttpAppender" ); // Name is required
49
46
}
50
47
51
48
@ Test
@@ -55,12 +52,13 @@ void testBuilderWithoutUrl(final ListStatusListener listener) throws Exception {
55
52
HttpAppender appender = HttpAppender .newBuilder ()
56
53
.setConfiguration (new DefaultConfiguration ())
57
54
.setName ("TestAppender" )
58
- .setLayout (JsonLayout .createDefaultLayout ()) // Providing a layout here
55
+ .setLayout (JsonLayout .createDefaultLayout ()) // Providing a layout here
59
56
.build ();
60
57
61
58
// Verify that the error message for missing URL is captured
62
59
assertThat (listener .findStatusData (Level .ERROR ))
63
- .anyMatch (statusData -> statusData .getMessage ().getFormattedMessage ().contains ("HttpAppender requires URL to be set." ));
60
+ .anyMatch (statusData ->
61
+ statusData .getMessage ().getFormattedMessage ().contains ("HttpAppender requires URL to be set." ));
64
62
}
65
63
66
64
@ Test
@@ -70,23 +68,22 @@ void testBuilderWithUrlAndWithoutLayout(final ListStatusListener listener) throw
70
68
HttpAppender appender = HttpAppender .newBuilder ()
71
69
.setConfiguration (new DefaultConfiguration ())
72
70
.setName ("TestAppender" )
73
- .setUrl (new URL ("http://localhost:8080/logs" )) // Providing the URL
71
+ .setUrl (new URL ("http://localhost:8080/logs" )) // Providing the URL
74
72
.build ();
75
73
76
74
// Verify that the error message for missing layout is captured
77
- assertThat (listener .findStatusData (Level .ERROR ))
78
- .anyMatch (statusData -> statusData .getMessage ().getFormattedMessage ().contains ("HttpAppender requires a layout to be set." ));
75
+ assertThat (listener .findStatusData (Level .ERROR )).anyMatch (statusData -> statusData
76
+ .getMessage ()
77
+ .getFormattedMessage ()
78
+ .contains ("HttpAppender requires a layout to be set." ));
79
79
}
80
80
81
-
82
81
@ Test
83
82
void testBuilderWithValidConfiguration () throws Exception {
84
83
URL url = new URL ("http://example.com" );
85
84
Layout <?> layout = JsonLayout .createDefaultLayout (); // Valid layout
86
85
87
- HttpAppender .Builder <?> builder = getBuilder ()
88
- .setUrl (url )
89
- .setLayout (layout );
86
+ HttpAppender .Builder <?> builder = getBuilder ().setUrl (url ).setLayout (layout );
90
87
91
88
HttpAppender appender = builder .build ();
92
89
assertNotNull (appender , "HttpAppender should be created with valid configuration." );
@@ -98,10 +95,8 @@ void testBuilderWithCustomMethod() throws Exception {
98
95
Layout <?> layout = JsonLayout .createDefaultLayout ();
99
96
String customMethod = "PUT" ;
100
97
101
- HttpAppender .Builder <?> builder = getBuilder ()
102
- .setUrl (url )
103
- .setLayout (layout )
104
- .setMethod (customMethod );
98
+ HttpAppender .Builder <?> builder =
99
+ getBuilder ().setUrl (url ).setLayout (layout ).setMethod (customMethod );
105
100
106
101
HttpAppender appender = builder .build ();
107
102
assertNotNull (appender , "HttpAppender should be created with a custom HTTP method." );
@@ -111,15 +106,12 @@ void testBuilderWithCustomMethod() throws Exception {
111
106
void testBuilderWithHeaders () throws Exception {
112
107
URL url = new URL ("http://example.com" );
113
108
Layout <?> layout = JsonLayout .createDefaultLayout ();
114
- Property [] headers = new Property []{
115
- Property .createProperty ("Header1" , "Value1" ),
116
- Property .createProperty ("Header2" , "Value2" )
109
+ Property [] headers = new Property [] {
110
+ Property .createProperty ("Header1" , "Value1" ), Property .createProperty ("Header2" , "Value2" )
117
111
};
118
112
119
- HttpAppender .Builder <?> builder = getBuilder ()
120
- .setUrl (url )
121
- .setLayout (layout )
122
- .setHeaders (headers );
113
+ HttpAppender .Builder <?> builder =
114
+ getBuilder ().setUrl (url ).setLayout (layout ).setHeaders (headers );
123
115
124
116
HttpAppender appender = builder .build ();
125
117
assertNotNull (appender , "HttpAppender should be created with headers." );
@@ -131,10 +123,8 @@ void testBuilderWithSslConfiguration() throws Exception {
131
123
Layout <?> layout = JsonLayout .createDefaultLayout ();
132
124
SslConfiguration sslConfig = mock (SslConfiguration .class );
133
125
134
- HttpAppender .Builder <?> builder = getBuilder ()
135
- .setUrl (url )
136
- .setLayout (layout )
137
- .setSslConfiguration (sslConfig );
126
+ HttpAppender .Builder <?> builder =
127
+ getBuilder ().setUrl (url ).setLayout (layout ).setSslConfiguration (sslConfig );
138
128
139
129
HttpAppender appender = builder .build ();
140
130
assertNotNull (appender , "HttpAppender should be created with SSL configuration." );
@@ -144,4 +134,4 @@ void testBuilderWithSslConfiguration() throws Exception {
144
134
void testBuilderWithInvalidUrl () {
145
135
assertThrows (MalformedURLException .class , () -> new URL ("invalid-url" ));
146
136
}
147
- }
137
+ }
0 commit comments