]> git.basschouten.com Git - openhab-addons.git/blob
8b184b6c39b56cdf2965203850f393add899ec32
[openhab-addons.git] /
1 /*
2  * Copyright 2017 Gregory Moyer
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.sleepiq.api.model;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.io.FileReader;
21
22 import org.junit.jupiter.api.BeforeAll;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.sleepiq.api.impl.GsonGenerator;
25 import org.openhab.binding.sleepiq.api.test.AbstractTest;
26
27 import com.google.gson.Gson;
28
29 public class SleeperTest extends AbstractTest {
30     private static Gson gson;
31
32     @BeforeAll
33     public static void setUpBeforeClass() {
34         gson = GsonGenerator.create(true);
35     }
36
37     @Test
38     public void testSerializeAllFields() throws Exception {
39         Sleeper sleeper = new Sleeper().withAccountId("-5555555555555555555").withAccountOwner(true).withActive(true)
40                 .withAvatar("").withBedId("-9999999999999999999").withBirthMonth(6).withBirthYear("1970")
41                 .withChild(false).withDuration("").withEmail("alice@domain.com").withEmailValidated(true)
42                 .withFirstName("Alice").withHeight(64).withLastLogin("2017-02-17 20:19:36 CST").withLicenseVersion(6L)
43                 .withMale(false).withSide(1).withSleeperId("-1111111111111111111").withSleepGoal(450)
44                 .withTimezone("US/Pacific").withUsername("alice@domain.com").withWeight(110).withZipCode("90210");
45         assertEquals(readJson("sleeper.json"), gson.toJson(sleeper));
46     }
47
48     @Test
49     public void testSerializeLastLoginNull() throws Exception {
50         Sleeper sleeper = new Sleeper().withLastLogin("null");
51         assertEquals(readJson("sleeper-lastlogin-null.json"), gson.toJson(sleeper));
52     }
53
54     @Test
55     public void testDeserializeAllFields() throws Exception {
56         try (FileReader reader = new FileReader(getTestDataFile("sleeper.json"))) {
57             Sleeper sleeper = gson.fromJson(reader, Sleeper.class);
58             assertEquals("-5555555555555555555", sleeper.getAccountId());
59             assertEquals(true, sleeper.isAccountOwner());
60             assertEquals(true, sleeper.isActive());
61             assertEquals("", sleeper.getAvatar());
62             assertEquals("-9999999999999999999", sleeper.getBedId());
63             assertEquals(Integer.valueOf(6), sleeper.getBirthMonth());
64             assertEquals("1970", sleeper.getBirthYear());
65             assertEquals(false, sleeper.isChild());
66             assertEquals("", sleeper.getDuration());
67             assertEquals("alice@domain.com", sleeper.getEmail());
68             assertEquals(true, sleeper.isEmailValidated());
69             assertEquals("Alice", sleeper.getFirstName());
70             assertEquals(Integer.valueOf(64), sleeper.getHeight());
71             assertEquals("2017-02-17 20:19:36 CST", sleeper.getLastLogin());
72             assertEquals(Long.valueOf(6L), sleeper.getLicenseVersion());
73             assertEquals(false, sleeper.isMale());
74             assertEquals(Integer.valueOf(1), sleeper.getSide());
75             assertEquals("-1111111111111111111", sleeper.getSleeperId());
76             assertEquals(Integer.valueOf(450), sleeper.getSleepGoal());
77             assertEquals("US/Pacific", sleeper.getTimezone());
78             assertEquals("alice@domain.com", sleeper.getUsername());
79             assertEquals(Integer.valueOf(110), sleeper.getWeight());
80             assertEquals("90210", sleeper.getZipCode());
81         }
82     }
83
84     @Test
85     public void testDeserializeLastLoginNull() throws Exception {
86         try (FileReader reader = new FileReader(getTestDataFile("sleeper-lastlogin-null.json"))) {
87             Sleeper sleeper = gson.fromJson(reader, Sleeper.class);
88             assertEquals("null", sleeper.getLastLogin());
89         }
90     }
91 }