2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nanoleaf.internal;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import java.io.IOException;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21 import java.util.Objects;
22 import java.util.stream.Collectors;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.openhab.binding.nanoleaf.internal.model.Layout;
27 import org.openhab.binding.nanoleaf.internal.model.Write;
29 import com.google.gson.Gson;
34 * @author Stefan Höhn - Initial contribution
38 public class LayoutTest {
40 private final Gson gson = new Gson();
42 private void assertLayoutFromJson(Path jsonFile, Path viewFile) throws IOException {
43 String json = Files.readString(jsonFile);
44 Layout layout = Objects.requireNonNull(gson.fromJson(json, Layout.class));
46 String expectedView = Files.readAllLines(viewFile).stream().collect(Collectors.joining(System.lineSeparator()));
48 assertThat(layout.getLayoutView(), is(equalTo(expectedView)));
52 public void testTheRightLayoutView() throws IOException {
53 assertLayoutFromJson(Path.of("src/test/resources/right-layout.json"),
54 Path.of("src/test/resources/right-layout-view"));
58 public void testTheInconsistentLayoutView() throws IOException {
59 assertLayoutFromJson(Path.of("src/test/resources/inconsistent-layout.json"),
60 Path.of("src/test/resources/inconsistent-layout-view"));
64 public void testEffects() {
65 Write write = new Write();
66 write.setCommand("display");
67 write.setAnimType("static");
70 int quotient = Integer.divideUnsigned(panelID, 256);
71 int remainder = Integer.remainderUnsigned(panelID, 256);
72 write.setAnimData(String.format("0 1 %d %d %d %d %d 0 0 10", quotient, remainder, 20, 40, 60));
73 String content = gson.toJson(write);
74 assertThat(content, containsStringIgnoringCase("palette"));
75 assertThat(content, is(not(containsStringIgnoringCase("colorType"))));