samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 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.onosproject.dhcp; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 19 | import org.junit.Assert; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | |
| 23 | import java.util.Date; |
| 24 | |
| 25 | import static org.hamcrest.MatcherAssert.assertThat; |
| 26 | import static org.hamcrest.Matchers.containsString; |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.junit.Assert.fail; |
| 29 | |
| 30 | /** |
| 31 | * Unit Tests for IPAssignment class. |
| 32 | */ |
Sho SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 33 | public class IpAssignmentTest { |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 34 | |
| 35 | private final Date dateNow = new Date(); |
| 36 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 37 | private final IpAssignment stats1 = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 38 | .ipAddress(Ip4Address.valueOf("10.10.10.10")) |
| 39 | .leasePeriod(300) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 40 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Expired) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 41 | .timestamp(dateNow) |
| 42 | .build(); |
| 43 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 44 | private final IpAssignment stats2 = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 45 | .ipAddress(Ip4Address.valueOf("10.10.10.10")) |
| 46 | .leasePeriod(300) |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 47 | .assignmentStatus(IpAssignment.AssignmentStatus.Option_Assigned) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 48 | .timestamp(dateNow) |
| 49 | .build(); |
| 50 | |
Thomas Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 51 | private final IpAssignment stats3 = IpAssignment.builder(stats1) |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 52 | .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 Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 62 | assertThat(stats3.assignmentStatus(), is(IpAssignment.AssignmentStatus.Option_Expired)); |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 63 | } |
| 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 Vachuska | a1da42e | 2015-09-09 00:45:22 -0700 | [diff] [blame] | 90 | IpAssignment stats4 = IpAssignment.builder() |
samanwita pal | f28207b | 2015-09-04 10:41:56 -0700 | [diff] [blame] | 91 | .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 SHIMIZU | 8dc81ea | 2015-09-10 10:59:43 -0700 | [diff] [blame] | 100 | } |