blob: 0783ec7303b47c57a1cb1bc1162a7f0834cfccb7 [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;
19import junit.framework.TestCase;
20import org.junit.Assert;
21import org.junit.Test;
22import org.onlab.packet.Ip4Address;
23
24import java.util.Date;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.containsString;
28import static org.hamcrest.Matchers.is;
29import static org.junit.Assert.fail;
30
31/**
32 * Unit Tests for IPAssignment class.
33 */
34public class IPAssignmentTest extends TestCase {
35
36 private final Date dateNow = new Date();
37
38 private final IPAssignment stats1 = IPAssignment.builder()
39 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
40 .leasePeriod(300)
41 .assignmentStatus(IPAssignment.AssignmentStatus.Option_Expired)
42 .timestamp(dateNow)
43 .build();
44
45 private final IPAssignment stats2 = IPAssignment.builder()
46 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
47 .leasePeriod(300)
48 .assignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned)
49 .timestamp(dateNow)
50 .build();
51
52 private final IPAssignment stats3 = IPAssignment.builder(stats1)
53 .build();
54
55 /**
56 * Tests the constructor for the class.
57 */
58 @Test
59 public void testConstruction() {
60 assertThat(stats3.ipAddress(), is(Ip4Address.valueOf("10.10.10.10")));
61 assertThat(stats3.timestamp(), is(dateNow));
62 assertThat(stats3.leasePeriod(), is(300));
63 assertThat(stats3.assignmentStatus(), is(IPAssignment.AssignmentStatus.Option_Expired));
64 }
65
66 /**
67 * Tests the equality and inequality of objects using Guava EqualsTester.
68 */
69 @Test
70 public void testEquals() {
71 new EqualsTester()
72 .addEqualityGroup(stats1, stats1)
73 .addEqualityGroup(stats2)
74 .testEquals();
75 }
76
77 /**
78 * Tests if the toString method returns a consistent value for hashing.
79 */
80 @Test
81 public void testToString() {
82 assertThat(stats1.toString(), is(stats1.toString()));
83 }
84
85 /**
86 * Tests if the validateInputs method returns an exception for malformed object.
87 */
88 @Test
89 public void testValidateInputs() {
90 try {
91 IPAssignment stats4 = IPAssignment.builder()
92 .ipAddress(Ip4Address.valueOf("10.10.10.10"))
93 .leasePeriod(300)
94 .build();
95
96 fail("Construction of a malformed IPAssignment did not throw an exception");
97 } catch (NullPointerException e) {
98 Assert.assertThat(e.getMessage(), containsString("must be specified"));
99 }
100 }
101}