blob: 3ecc5cfa1a8411435d1f8a884fd1df0087ff8e75 [file] [log] [blame]
samanwita palf28207b2015-09-04 10:41:56 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
16package org.onosproject.dhcp;
17
18import com.google.common.testing.EqualsTester;
samanwita palf28207b2015-09-04 10:41:56 -070019import org.junit.Assert;
20import org.junit.Test;
21import org.onlab.packet.Ip4Address;
22
23import java.util.Date;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.containsString;
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.fail;
29
30/**
31 * Unit Tests for IPAssignment class.
32 */
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -070033public class IpAssignmentTest {
samanwita palf28207b2015-09-04 10:41:56 -070034
35 private final Date dateNow = new Date();
36
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070037 private final IpAssignment stats1 = IpAssignment.builder()
samanwita palf28207b2015-09-04 10:41:56 -070038 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
39 .leasePeriod(300)
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070040 .assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired)
samanwita palf28207b2015-09-04 10:41:56 -070041 .timestamp(dateNow)
42 .build();
43
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070044 private final IpAssignment stats2 = IpAssignment.builder()
samanwita palf28207b2015-09-04 10:41:56 -070045 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
46 .leasePeriod(300)
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070047 .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned)
samanwita palf28207b2015-09-04 10:41:56 -070048 .timestamp(dateNow)
49 .build();
50
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070051 private final IpAssignment stats3 = IpAssignment.builder(stats1)
samanwita palf28207b2015-09-04 10:41:56 -070052 .build();
53
54 /**
55 * Tests the constructor for the class.
56 */
57 @Test
58 public void testConstruction() {
59 assertThat(stats3.ipAddress(), is(Ip4Address.valueOf("10.10.10.10")));
60 assertThat(stats3.timestamp(), is(dateNow));
61 assertThat(stats3.leasePeriod(), is(300));
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070062 assertThat(stats3.assignmentStatus(), is(IpAssignment.AssignmentStatus.Option_Expired));
samanwita palf28207b2015-09-04 10:41:56 -070063 }
64
65 /**
66 * Tests the equality and inequality of objects using Guava EqualsTester.
67 */
68 @Test
69 public void testEquals() {
70 new EqualsTester()
71 .addEqualityGroup(stats1, stats1)
72 .addEqualityGroup(stats2)
73 .testEquals();
74 }
75
76 /**
77 * Tests if the toString method returns a consistent value for hashing.
78 */
79 @Test
80 public void testToString() {
81 assertThat(stats1.toString(), is(stats1.toString()));
82 }
83
84 /**
85 * Tests if the validateInputs method returns an exception for malformed object.
86 */
87 @Test
88 public void testValidateInputs() {
89 try {
Thomas Vachuskaa1da42e2015-09-09 00:45:22 -070090 IpAssignment stats4 = IpAssignment.builder()
samanwita palf28207b2015-09-04 10:41:56 -070091 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
92 .leasePeriod(300)
93 .build();
94
95 fail("Construction of a malformed IPAssignment did not throw an exception");
96 } catch (NullPointerException e) {
97 Assert.assertThat(e.getMessage(), containsString("must be specified"));
98 }
99 }
Sho SHIMIZU8dc81ea2015-09-10 10:59:43 -0700100}