]> git.basschouten.com Git - openhab-addons.git/blob
147613a59a6f041c6a983c85a97f7a687f7d9616
[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.sleepiq.api.model;
14
15 import static org.junit.jupiter.api.Assertions.*;
16
17 import java.io.FileReader;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.sleepiq.api.test.AbstractTest;
24 import org.openhab.binding.sleepiq.internal.api.dto.Bed;
25 import org.openhab.binding.sleepiq.internal.api.dto.BedsResponse;
26 import org.openhab.binding.sleepiq.internal.api.impl.GsonGenerator;
27
28 import com.google.gson.Gson;
29
30 /**
31  * The {@link BedsResponseTest} tests deserialization of a beds response object.
32  *
33  * @author Gregory Moyer - Initial contribution
34  */
35 @NonNullByDefault
36 public class BedsResponseTest extends AbstractTest {
37     private static Gson gson = GsonGenerator.create(true);
38
39     @Test
40     public void testSerializeAllFields() throws Exception {
41         BedsResponse bedsResponse = new BedsResponse()
42                 .withBeds(Arrays.asList(new Bed().withName("Bed1"), new Bed().withName("Bed2")));
43         assertEquals(readJson("beds-response.json"), gson.toJson(bedsResponse));
44     }
45
46     @Test
47     public void testDeserializeAllFields() throws Exception {
48         try (FileReader reader = new FileReader(getTestDataFile("beds-response.json"))) {
49             BedsResponse bedsResponse = gson.fromJson(reader, BedsResponse.class);
50
51             List<Bed> beds = bedsResponse.getBeds();
52             assertNotNull(beds);
53             assertEquals(2, beds.size());
54             assertEquals("Bed1", beds.get(0).getName());
55             assertEquals("Bed2", beds.get(1).getName());
56         }
57     }
58 }