]> git.basschouten.com Git - openhab-addons.git/blob
34f2e1589449f5b1de5f6b8f13bd432846ef7f76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nanoleaf.internal;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
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;
23
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;
28
29 import com.google.gson.Gson;
30
31 /**
32  * Test for the Layout
33  *
34  * @author Stefan Höhn - Initial contribution
35  */
36
37 @NonNullByDefault
38 public class LayoutTest {
39
40     private final Gson gson = new Gson();
41
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));
45
46         String expectedView = Files.readAllLines(viewFile).stream().collect(Collectors.joining(System.lineSeparator()));
47
48         assertThat(layout.getLayoutView(), is(equalTo(expectedView)));
49     }
50
51     @Test
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"));
55     }
56
57     @Test
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"));
61     }
62
63     @Test
64     public void testEffects() {
65         Write write = new Write();
66         write.setCommand("display");
67         write.setAnimType("static");
68         write.setLoop(false);
69         int panelID = 123;
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"))));
76     }
77 }