Skip to content

Commit

Permalink
refactor unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
kkrik-es committed Dec 3, 2024
1 parent d30d7b8 commit 8f9c771
Showing 1 changed file with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.elasticsearch.index.mapper.MapperService.MergeReason;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -54,25 +52,6 @@

public class DocumentMapperTests extends MapperServiceTestCase {

static MockAppender appender;
static Logger testLogger = LogManager.getLogger(DocumentMapper.class);
static Level originalLogLevel = testLogger.getLevel();

@BeforeClass
public static void init() throws IllegalAccessException {
appender = new MockAppender("mock_appender");
appender.start();
Loggers.addAppender(testLogger, appender);
Loggers.setLevel(testLogger, Level.ERROR);
}

@AfterClass
public static void cleanup() {
Loggers.removeAppender(testLogger, appender);
appender.stop();
Loggers.setLevel(testLogger, originalLogLevel);
}

public void testAddFields() throws Exception {
DocumentMapper stage1 = createDocumentMapper(mapping(b -> b.startObject("name").field("type", "text").endObject()));
DocumentMapper stage2 = createDocumentMapper(mapping(b -> {
Expand Down Expand Up @@ -521,14 +500,30 @@ public void testDeeplyNestedMapping() throws Exception {
}

public void testParsingErrorLogging() throws Exception {
DocumentMapper doc = createDocumentMapper(mapping(b -> b.startObject("value").field("type", "integer").endObject()));
MockAppender appender = new MockAppender("mock_appender");
appender.start();
Logger testLogger = LogManager.getLogger(DocumentMapper.class);
Loggers.addAppender(testLogger, appender);
Level originalLogLevel = testLogger.getLevel();
Loggers.setLevel(testLogger, Level.ERROR);

try {
DocumentMapper doc = createDocumentMapper(mapping(b -> b.startObject("value").field("type", "integer").endObject()));

DocumentParsingException e = expectThrows(DocumentParsingException.class, () -> doc.parse(source(b -> b.field("value", "foo"))));
assertThat(e.getMessage(), containsString("failed to parse field [value] of type [integer] in document with id '1'"));
assertThat(appender.getLastEventAndReset().getMessage().getFormattedMessage(), containsString(e.getMessage()));
DocumentParsingException e = expectThrows(
DocumentParsingException.class,
() -> doc.parse(source(b -> b.field("value", "foo")))
);
assertThat(e.getMessage(), containsString("failed to parse field [value] of type [integer] in document with id '1'"));
assertThat(appender.getLastEventAndReset().getMessage().getFormattedMessage(), containsString(e.getMessage()));

e = expectThrows(DocumentParsingException.class, () -> doc.parse(source(b -> b.field("value", "foo"))));
assertThat(e.getMessage(), containsString("failed to parse field [value] of type [integer] in document with id '1'"));
assertThat(appender.getLastEventAndReset(), nullValue());
e = expectThrows(DocumentParsingException.class, () -> doc.parse(source(b -> b.field("value", "foo"))));
assertThat(e.getMessage(), containsString("failed to parse field [value] of type [integer] in document with id '1'"));
assertThat(appender.getLastEventAndReset(), nullValue());
} finally {
Loggers.setLevel(testLogger, originalLogLevel);
Loggers.removeAppender(testLogger, appender);
appender.stop();
}
}
}

0 comments on commit 8f9c771

Please sign in to comment.