Skip to content

Commit

Permalink
Add tests for CRLFLogConverter.java
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Dec 30, 2023
1 parent 7445e9a commit 4ec970a
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 1 deletion.
12 changes: 12 additions & 0 deletions generators/server/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ exports[`generator - server composing databaseType option no with jwt should mat
"src/test/java/com/mycompany/myapp/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/SpringBootTestClassOrderer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -421,6 +424,9 @@ exports[`generator - server composing databaseType option no with oauth2 should
"src/test/java/com/mycompany/myapp/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/SpringBootTestClassOrderer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -636,6 +642,9 @@ exports[`generator - server composing databaseType option no with session should
"src/test/java/com/mycompany/myapp/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/SpringBootTestClassOrderer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1082,6 +1091,9 @@ exports[`generator - server with entities should match files snapshot 1`] = `
"src/test/java/com/mycompany/myapp/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/EmbeddedSQL.java": {
"stateCleared": "modified",
},
Expand Down
2 changes: 1 addition & 1 deletion generators/server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const imperativeConfigFiles = {
{
path: `${SERVER_TEST_SRC_DIR}_package_/`,
renameTo: moveToJavaPackageTestDir,
templates: ['config/WebConfigurerTest.java', 'config/WebConfigurerTestController.java'],
templates: ['config/CRLFLogConverterTest.java', 'config/WebConfigurerTest.java', 'config/WebConfigurerTestController.java'],
},
],
};
Expand Down
3 changes: 3 additions & 0 deletions generators/server/support/__snapshots__/needles.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ exports[`generator - server - support - needles generated project should match s
"src/test/java/com/mycompany/myapp/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/com/mycompany/myapp/config/EmbeddedSQL.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<%#
Copyright 2013-2023 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%=packageName%>.config;

import ch.qos.logback.classic.spi.ILoggingEvent;
import org.junit.jupiter.api.Test;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiElement;

import java.util.Collections;
import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class CRLFLogConverterTest {

@Test
void transformShouldReturnInputStringWhenMarkerListIsEmpty() {
ILoggingEvent event = mock(ILoggingEvent.class);
when(event.getMarkerList()).thenReturn(null);
when(event.getLoggerName()).thenReturn("org.hibernate.example.Logger");
String input = "Test input string";
CRLFLogConverter converter = new CRLFLogConverter();

String result = converter.transform(event, input);

assertEquals(input, result);
}

@Test
void transformShouldReturnInputStringWhenMarkersContainCRLFSafeMarker() {
ILoggingEvent event = mock(ILoggingEvent.class);
Marker marker = MarkerFactory.getMarker("CRLF_SAFE");
List<Marker> markers = Collections.singletonList(marker);
when(event.getMarkerList()).thenReturn(markers);
String input = "Test input string";
CRLFLogConverter converter = new CRLFLogConverter();

String result = converter.transform(event, input);

assertEquals(input, result);
}

@Test
void transformShouldReturnInputStringWhenMarkersNotContainCRLFSafeMarker() {
ILoggingEvent event = mock(ILoggingEvent.class);
Marker marker = MarkerFactory.getMarker("CRLF_NOT_SAFE");
List<Marker> markers = Collections.singletonList(marker);
when(event.getMarkerList()).thenReturn(markers);
when(event.getLoggerName()).thenReturn("org.hibernate.example.Logger");
String input = "Test input string";
CRLFLogConverter converter = new CRLFLogConverter();

String result = converter.transform(event, input);

assertEquals(input, result);
}

@Test
void transformShouldReturnInputStringWhenLoggerIsSafe() {
ILoggingEvent event = mock(ILoggingEvent.class);
when(event.getLoggerName()).thenReturn("org.hibernate.example.Logger");
String input = "Test input string";
CRLFLogConverter converter = new CRLFLogConverter();

String result = converter.transform(event, input);

assertEquals(input, result);
}

@Test
void transformShouldReplaceNewlinesAndCarriageReturnsWithUnderscoreWhenMarkersDoNotContainCRLFSafeMarkerAndLoggerIsNotSafe() {
ILoggingEvent event = mock(ILoggingEvent.class);
List<Marker> markers = Collections.emptyList();
when(event.getMarkerList()).thenReturn(markers);
when(event.getLoggerName()).thenReturn("com.mycompany.myapp.example.Logger");
String input = "Test\ninput\rstring";
CRLFLogConverter converter = new CRLFLogConverter();

String result = converter.transform(event, input);

assertEquals("Test_input_string", result);
}

@Test
void transformShouldReplaceNewlinesAndCarriageReturnsWithAnsiStringWhenMarkersDoNotContainCRLFSafeMarkerAndLoggerIsNotSafeAndAnsiElementIsNotNull() {
ILoggingEvent event = mock(ILoggingEvent.class);
List<Marker> markers = Collections.emptyList();
when(event.getMarkerList()).thenReturn(markers);
when(event.getLoggerName()).thenReturn("com.mycompany.myapp.example.Logger");
String input = "Test\ninput\rstring";
CRLFLogConverter converter = new CRLFLogConverter();
converter.setOptionList(List.of("red"));

String result = converter.transform(event, input);

assertEquals("Test_input_string", result);
}

@Test
void isLoggerSafeShouldReturnTrueWhenLoggerNameStartsWithSafeLogger() {
ILoggingEvent event = mock(ILoggingEvent.class);
when(event.getLoggerName()).thenReturn("org.springframework.boot.autoconfigure.example.Logger");
CRLFLogConverter converter = new CRLFLogConverter();

boolean result = converter.isLoggerSafe(event);

assertTrue(result);
}

@Test
void isLoggerSafeShouldReturnFalseWhenLoggerNameDoesNotStartWithSafeLogger() {
ILoggingEvent event = mock(ILoggingEvent.class);
when(event.getLoggerName()).thenReturn("com.mycompany.myapp.example.Logger");
CRLFLogConverter converter = new CRLFLogConverter();

boolean result = converter.isLoggerSafe(event);

assertFalse(result);
}

@Test
void testToAnsiString() {
CRLFLogConverter cut = new CRLFLogConverter();
AnsiElement ansiElement = AnsiColor.RED;

String result = cut.toAnsiString("input", ansiElement);

assertThat(result).isEqualTo("input");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,9 @@ exports[`generator - cassandra microservice-jwt-reactive(false)-maven-enableTran
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CassandraTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1834,6 +1837,9 @@ exports[`generator - cassandra monolith-jwt-reactive(false)-maven-enableTranslat
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CassandraTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -2600,6 +2606,9 @@ exports[`generator - cassandra monolith-oauth2-reactive(false)-maven-enableTrans
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CassandraTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -3402,6 +3411,9 @@ exports[`generator - cassandra monolith-session-reactive(false)-maven-enableTran
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CassandraTestContainer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,9 @@ exports[`generator - couchbase microservice-jwt-reactive(false)-maven-enableTran
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CouchbaseTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1873,6 +1876,9 @@ exports[`generator - couchbase monolith-jwt-reactive(false)-maven-enableTranslat
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CouchbaseTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -2645,6 +2651,9 @@ exports[`generator - couchbase monolith-oauth2-reactive(false)-maven-enableTrans
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CouchbaseTestContainer.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -3468,6 +3477,9 @@ exports[`generator - couchbase monolith-session-reactive(false)-maven-enableTran
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CouchbaseTestContainer.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,9 @@ exports[`generator - elasticsearch microservice-jwt-reactive(false)-maven-enable
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/ElasticsearchTestConfiguration.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -2083,6 +2086,9 @@ exports[`generator - elasticsearch monolith-jwt-reactive(false)-maven-enableTran
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/ElasticsearchTestConfiguration.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -2987,6 +2993,9 @@ exports[`generator - elasticsearch monolith-oauth2-reactive(false)-maven-enableT
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/ElasticsearchTestConfiguration.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -3942,6 +3951,9 @@ exports[`generator - elasticsearch monolith-session-reactive(false)-maven-enable
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/ElasticsearchTestConfiguration.java": {
"stateCleared": "modified",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ exports[`generator - mongodb microservice-jwt-reactive(false)-maven-enableTransl
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/EmbeddedMongo.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -1780,6 +1783,9 @@ exports[`generator - mongodb monolith-jwt-reactive(false)-maven-enableTranslatio
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/EmbeddedMongo.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -2522,6 +2528,9 @@ exports[`generator - mongodb monolith-oauth2-reactive(false)-maven-enableTransla
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/EmbeddedMongo.java": {
"stateCleared": "modified",
},
Expand Down Expand Up @@ -3303,6 +3312,9 @@ exports[`generator - mongodb monolith-session-reactive(false)-maven-enableTransl
"src/test/java/tech/jhipster/config/AsyncSyncConfiguration.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/CRLFLogConverterTest.java": {
"stateCleared": "modified",
},
"src/test/java/tech/jhipster/config/EmbeddedMongo.java": {
"stateCleared": "modified",
},
Expand Down
Loading

0 comments on commit 4ec970a

Please sign in to comment.