[nanoleaf] Fix LayoutTest failing on Windows (#12563)

On Windows the view has different line endings causing some tests to fail.
For readability the big strings have also been moved into files.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born 2022-04-03 10:44:28 +02:00 committed by GitHub
parent 5712de5e63
commit 0f56369b2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 65 deletions

View File

@ -15,9 +15,13 @@ package org.openhab.binding.nanoleaf.internal;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.openhab.binding.nanoleaf.internal.model.Layout; import org.openhab.binding.nanoleaf.internal.model.Layout;
import org.openhab.binding.nanoleaf.internal.model.Write; import org.openhab.binding.nanoleaf.internal.model.Write;
@ -34,74 +38,26 @@ import com.google.gson.Gson;
public class LayoutTest { public class LayoutTest {
private final Gson gson = new Gson(); private final Gson gson = new Gson();
String layout1Json = "";
String layoutInconsistentPanelNoJson = "";
@BeforeEach private void assertLayoutFromJson(Path jsonFile, Path viewFile) throws IOException {
public void setup() { String json = Files.readString(jsonFile);
layout1Json = "{\n" + " \"numPanels\": 14,\n" + " \"sideLength\": 0,\n" Layout layout = Objects.requireNonNull(gson.fromJson(json, Layout.class));
+ " \"positionData\": [\n" + " {\n" + " \"panelId\": 60147,\n"
+ " \"x\": 199,\n" + " \"y\": 99,\n" + " \"o\": 0,\n" String expectedView = Files.readAllLines(viewFile).stream().collect(Collectors.joining(System.lineSeparator()));
+ " \"shapeType\": 3\n" + " },\n" + " {\n" + " \"panelId\": 61141,\n"
+ " \"x\": 200,\n" + " \"y\": 199,\n" + " \"o\": 90,\n" assertThat(layout.getLayoutView(), is(equalTo(expectedView)));
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 42064,\n"
+ " \"x\": 100,\n" + " \"y\": 200,\n" + " \"o\": 180,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 186,\n"
+ " \"x\": 0,\n" + " \"y\": 200,\n" + " \"o\": 180,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 19209,\n"
+ " \"x\": 0,\n" + " \"y\": 100,\n" + " \"o\": 270,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 36604,\n"
+ " \"x\": 300,\n" + " \"y\": 99,\n" + " \"o\": 0,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 37121,\n"
+ " \"x\": 400,\n" + " \"y\": 99,\n" + " \"o\": 270,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 45187,\n"
+ " \"x\": 400,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 33626,\n"
+ " \"x\": 500,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 10523,\n"
+ " \"x\": 600,\n" + " \"y\": 199,\n" + " \"o\": 270,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 54086,\n"
+ " \"x\": 599,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 3512,\n"
+ " \"x\": 699,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 16398,\n"
+ " \"x\": 799,\n" + " \"y\": 99,\n" + " \"o\": 540,\n"
+ " \"shapeType\": 2\n" + " },\n" + " {\n" + " \"panelId\": 39163,\n"
+ " \"x\": 800,\n" + " \"y\": 199,\n" + " \"o\": 630,\n"
+ " \"shapeType\": 2\n" + " }\n" + " ]\n" + " }";
layoutInconsistentPanelNoJson = "{\"numPanels\":15,\"sideLength\":100,\"positionData\":[{\"panelId\":41451,\"x\":350,\"y\":0,\"o\":0,\"shapeType\":3},{\"panelId\":8134,\"x\":350,\"y\":150,\"o\":0,\"shapeType\":2},{\"panelId\":58086,\"x\":200,\"y\":100,\"o\":270,\"shapeType\":2},{\"panelId\":38724,\"x\":300,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":48111,\"x\":200,\"y\":200,\"o\":270,\"shapeType\":2},{\"panelId\":56093,\"x\":100,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":55836,\"x\":0,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":31413,\"x\":100,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":9162,\"x\":300,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":13276,\"x\":400,\"y\":300,\"o\":90,\"shapeType\":2},{\"panelId\":17870,\"x\":400,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":5164,\"x\":500,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":64279,\"x\":600,\"y\":200,\"o\":0,\"shapeType\":2},{\"panelId\":39755,\"x\":500,\"y\":100,\"o\":90,\"shapeType\":2}]}";
} }
@Test @Test
public void testTheRightLayoutView() { public void testTheRightLayoutView() throws IOException {
@Nullable assertLayoutFromJson(Path.of("src/test/resources/right-layout.json"),
Layout layout = gson.fromJson(layout1Json, Layout.class); Path.of("src/test/resources/right-layout-view"));
if (layout == null) {
layout = new Layout();
}
String layoutView = layout.getLayoutView();
assertThat(layoutView, is(equalTo(
" 186 42064 61141 45187 33626 10523 39163 \n"
+ " \n"
+ "19209 60147 36604 37121 54086 3512 16398 \n")));
} }
@Test @Test
public void testTheInconsistentLayoutView() { public void testTheInconsistentLayoutView() throws IOException {
@Nullable assertLayoutFromJson(Path.of("src/test/resources/inconsistent-layout.json"),
Layout layout = gson.fromJson(layoutInconsistentPanelNoJson, Layout.class); Path.of("src/test/resources/inconsistent-layout-view"));
if (layout == null) {
layout = new Layout();
}
String layoutView = layout.getLayoutView();
assertThat(layoutView,
is(equalTo(" 31413 9162 13276 \n"
+ " \n"
+ "55836 56093 48111 38724 17870 5164 64279 \n"
+ " 8134 \n"
+ " 58086 39755 \n"
+ " \n"
+ " 41451 \n")));
} }
@Test @Test
@ -111,8 +67,8 @@ public class LayoutTest {
write.setAnimType("static"); write.setAnimType("static");
write.setLoop(false); write.setLoop(false);
int panelID = 123; int panelID = 123;
int quotient = Integer.divideUnsigned(Integer.valueOf(panelID), 256); int quotient = Integer.divideUnsigned(panelID, 256);
int remainder = Integer.remainderUnsigned(Integer.valueOf(panelID), 256); int remainder = Integer.remainderUnsigned(panelID, 256);
write.setAnimData(String.format("0 1 %d %d %d %d %d 0 0 10", quotient, remainder, 20, 40, 60)); write.setAnimData(String.format("0 1 %d %d %d %d %d 0 0 10", quotient, remainder, 20, 40, 60));
String content = gson.toJson(write); String content = gson.toJson(write);
assertThat(content, containsStringIgnoringCase("palette")); assertThat(content, containsStringIgnoringCase("palette"));

View File

@ -0,0 +1,8 @@
31413 9162 13276
55836 56093 48111 38724 17870 5164 64279
8134
58086 39755
41451

View File

@ -0,0 +1 @@
{"numPanels":15,"sideLength":100,"positionData":[{"panelId":41451,"x":350,"y":0,"o":0,"shapeType":3},{"panelId":8134,"x":350,"y":150,"o":0,"shapeType":2},{"panelId":58086,"x":200,"y":100,"o":270,"shapeType":2},{"panelId":38724,"x":300,"y":200,"o":0,"shapeType":2},{"panelId":48111,"x":200,"y":200,"o":270,"shapeType":2},{"panelId":56093,"x":100,"y":200,"o":0,"shapeType":2},{"panelId":55836,"x":0,"y":200,"o":0,"shapeType":2},{"panelId":31413,"x":100,"y":300,"o":90,"shapeType":2},{"panelId":9162,"x":300,"y":300,"o":90,"shapeType":2},{"panelId":13276,"x":400,"y":300,"o":90,"shapeType":2},{"panelId":17870,"x":400,"y":200,"o":0,"shapeType":2},{"panelId":5164,"x":500,"y":200,"o":0,"shapeType":2},{"panelId":64279,"x":600,"y":200,"o":0,"shapeType":2},{"panelId":39755,"x":500,"y":100,"o":90,"shapeType":2}]}

View File

@ -0,0 +1,4 @@
186 42064 61141 45187 33626 10523 39163
19209 60147 36604 37121 54086 3512 16398

View File

@ -0,0 +1,104 @@
{
"numPanels":14,
"sideLength":0,
"positionData":[
{
"panelId":60147,
"x":199,
"y":99,
"o":0,
"shapeType":3
},
{
"panelId":61141,
"x":200,
"y":199,
"o":90,
"shapeType":2
},
{
"panelId":42064,
"x":100,
"y":200,
"o":180,
"shapeType":2
},
{
"panelId":186,
"x":0,
"y":200,
"o":180,
"shapeType":2
},
{
"panelId":19209,
"x":0,
"y":100,
"o":270,
"shapeType":2
},
{
"panelId":36604,
"x":300,
"y":99,
"o":0,
"shapeType":2
},
{
"panelId":37121,
"x":400,
"y":99,
"o":270,
"shapeType":2
},
{
"panelId":45187,
"x":400,
"y":199,
"o":270,
"shapeType":2
},
{
"panelId":33626,
"x":500,
"y":199,
"o":270,
"shapeType":2
},
{
"panelId":10523,
"x":600,
"y":199,
"o":270,
"shapeType":2
},
{
"panelId":54086,
"x":599,
"y":99,
"o":540,
"shapeType":2
},
{
"panelId":3512,
"x":699,
"y":99,
"o":540,
"shapeType":2
},
{
"panelId":16398,
"x":799,
"y":99,
"o":540,
"shapeType":2
},
{
"panelId":39163,
"x":800,
"y":199,
"o":630,
"shapeType":2
}
]
}