Skip to content

Commit

Permalink
Closes #1176 - Potential NPE in the Configuration Server's FileInfoVi…
Browse files Browse the repository at this point in the history
…sitor if no element with the "type" key exist (#1179)
  • Loading branch information
mariusoe authored Aug 9, 2021
1 parent d19cea6 commit e070b35
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ private FileInfo.Type resolveFileType(File file) throws IOException {

try {
Map<String, String> jsonMap = gson.fromJson(rawJson, TYPE_MAP);
if (jsonMap == null) {
if (jsonMap == null || !jsonMap.containsKey("type")) {
return fileType;
}

// Build a String from the type-value which corresponds to the Enum-Naming scheme.
String typeString = "UI_" + jsonMap.get("type").toUpperCase().replace("-", "_");
if (EnumUtils.isValidEnum(FileInfo.Type.class, typeString)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package rocks.inspectit.ocelot.file;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.jupiter.api.Assertions.*;

public class FileInfoVisitorTest {

private List<Path> testFiles;

@BeforeEach
public void beforeEach() {
testFiles = new LinkedList<>();
}

@AfterEach
public void afterEach() throws IOException {
for (Path file : testFiles) {
Files.delete(file);
}
}

private Path createTestFile(String test) throws IOException {
Path tempFile = Files.createTempFile("ocelot", ".yml");
Files.write(tempFile, test.getBytes(StandardCharsets.UTF_8));
testFiles.add(tempFile);
return tempFile;
}

@Nested
class VisitFile {

@Test
public void visitStandardFile() throws IOException {
Path testFile = createTestFile("test");
FileInfoVisitor visitor = new FileInfoVisitor();

visitor.preVisitDirectory(testFile.getParent(), null);
visitor.visitFile(testFile, null);

List<FileInfo> result = visitor.getFileInfos();

assertThat(result).hasSize(1)
.extracting(FileInfo::getName, FileInfo::getType)
.contains(tuple(testFile.getFileName().toString(), FileInfo.Type.FILE));
}

@Test
public void visitUiFile() throws IOException {
Path testFile = createTestFile("# {\"type\": \"method-configuration\"}");
FileInfoVisitor visitor = new FileInfoVisitor();

visitor.preVisitDirectory(testFile.getParent(), null);
visitor.visitFile(testFile, null);

List<FileInfo> result = visitor.getFileInfos();

assertThat(result).hasSize(1)
.extracting(FileInfo::getName, FileInfo::getType)
.contains(tuple(testFile.getFileName().toString(), FileInfo.Type.UI_METHOD_CONFIGURATION));
}

@Test
public void visitStandardFile_standardComment() throws IOException {
Path testFile = createTestFile("# hello");
FileInfoVisitor visitor = new FileInfoVisitor();

visitor.preVisitDirectory(testFile.getParent(), null);
visitor.visitFile(testFile, null);

List<FileInfo> result = visitor.getFileInfos();

assertThat(result).hasSize(1)
.extracting(FileInfo::getName, FileInfo::getType)
.contains(tuple(testFile.getFileName().toString(), FileInfo.Type.FILE));
}

@Test
public void visitUiFile_emptyMap() throws IOException {
Path testFile = createTestFile("# {}");
FileInfoVisitor visitor = new FileInfoVisitor();

visitor.preVisitDirectory(testFile.getParent(), null);
visitor.visitFile(testFile, null);

List<FileInfo> result = visitor.getFileInfos();

assertThat(result).hasSize(1)
.extracting(FileInfo::getName, FileInfo::getType)
.contains(tuple(testFile.getFileName().toString(), FileInfo.Type.FILE));
}
}

}

0 comments on commit e070b35

Please # to comment.