2 * Copyright 2017 Gregory Moyer
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openhab.binding.sleepiq.api.model;
18 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import java.io.FileReader;
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;
27 import com.google.gson.Gson;
29 public class SleeperTest extends AbstractTest {
30 private static Gson gson;
33 public static void setUpBeforeClass() {
34 gson = GsonGenerator.create(true);
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));
49 public void testSerializeLastLoginNull() throws Exception {
50 Sleeper sleeper = new Sleeper().withLastLogin("null");
51 assertEquals(readJson("sleeper-lastlogin-null.json"), gson.toJson(sleeper));
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());
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());