]> git.basschouten.com Git - openhab-addons.git/blob
386b1c3b7a85ec3db9a1cd53204328ac994d4be7
[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.Assert.assertEquals;
19
20 import java.io.FileReader;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.openhab.binding.sleepiq.api.impl.GsonGenerator;
27 import org.openhab.binding.sleepiq.api.model.Bed;
28 import org.openhab.binding.sleepiq.api.test.AbstractTest;
29
30 import com.google.gson.Gson;
31
32 public class BedTest extends AbstractTest
33 {
34     private static Gson gson;
35
36     @BeforeClass
37     public static void setUpBeforeClass()
38     {
39         gson = GsonGenerator.create(true);
40     }
41
42     @Test
43     public void testSerializeAllFields() throws Exception
44     {
45         Bed bed = new Bed().withAccountId("-8888888888888888888")
46                            .withBase("MODULAR")
47                            .withBedId("-9999999999999999999")
48                            .withDualSleep(true)
49                            .withKidsBed(false)
50                            .withMacAddress("AABBCCDDEEFF")
51                            .withModel("P5")
52                            .withName("Bed")
53                            .withPurchaseDate(ZonedDateTime.of(2017,
54                                                               2,
55                                                               2,
56                                                               0,
57                                                               0,
58                                                               1,
59                                                               0,
60                                                               ZoneId.of("Z").normalized()))
61                            .withReference("55555555555-5")
62                            .withRegistrationDate(ZonedDateTime.of(2017,
63                                                                   2,
64                                                                   17,
65                                                                   2,
66                                                                   14,
67                                                                   10,
68                                                                   0,
69                                                                   ZoneId.of("Z").normalized()))
70                            .withReturnRequestStatus(0L)
71                            .withSerial("")
72                            .withSize("QUEEN")
73                            .withSku("QP5")
74                            .withSleeperLeftId("-2222222222222222222")
75                            .withSleeperRightId("-1111111111111111111")
76                            .withStatus(1L)
77                            .withTimezone("US/Pacific")
78                            .withVersion("")
79                            .withZipCode("90210");
80         assertEquals(readJson("bed.json"), gson.toJson(bed));
81     }
82
83     @Test
84     public void testDeserializeAllFields() throws Exception
85     {
86         try (FileReader reader = new FileReader(getTestDataFile("bed.json")))
87         {
88             Bed bed = gson.fromJson(reader, Bed.class);
89             assertEquals("-8888888888888888888", bed.getAccountId());
90             assertEquals("MODULAR", bed.getBase());
91             assertEquals("-9999999999999999999", bed.getBedId());
92             assertEquals(true, bed.isDualSleep());
93             assertEquals(false, bed.isKidsBed());
94             assertEquals("AABBCCDDEEFF", bed.getMacAddress());
95             assertEquals("P5", bed.getModel());
96             assertEquals("Bed", bed.getName());
97             assertEquals(ZonedDateTime.of(2017, 2, 2, 0, 0, 1, 0, ZoneId.of("Z").normalized()),
98                          bed.getPurchaseDate());
99             assertEquals("55555555555-5", bed.getReference());
100             assertEquals(ZonedDateTime.of(2017, 2, 17, 2, 14, 10, 0, ZoneId.of("Z").normalized()),
101                          bed.getRegistrationDate());
102             assertEquals(Long.valueOf(0L), bed.getReturnRequestStatus());
103             assertEquals("", bed.getSerial());
104             assertEquals("QUEEN", bed.getSize());
105             assertEquals("QP5", bed.getSku());
106             assertEquals("-2222222222222222222", bed.getSleeperLeftId());
107             assertEquals("-1111111111111111111", bed.getSleeperRightId());
108             assertEquals(Long.valueOf(1L), bed.getStatus());
109             assertEquals("US/Pacific", bed.getTimezone());
110             assertEquals("", bed.getVersion());
111             assertEquals("90210", bed.getZipCode());
112         }
113     }
114 }