Ray Milkey | 8010bb4 | 2015-02-12 11:53:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
| 16 | package org.onosproject.net.intent; |
| 17 | |
| 18 | import java.util.Collections; |
| 19 | import java.util.concurrent.atomic.AtomicLong; |
| 20 | |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.core.IdGenerator; |
| 25 | import org.onosproject.net.NetTestTools; |
| 26 | import org.onosproject.store.Timestamp; |
| 27 | |
| 28 | import com.google.common.testing.EqualsTester; |
| 29 | |
| 30 | import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | import static org.hamcrest.Matchers.is; |
| 32 | |
| 33 | /** |
| 34 | * Unit tests for intent data objects. |
| 35 | */ |
| 36 | public class IntentDataTest { |
| 37 | private static class MockIntent extends Intent { |
| 38 | private static AtomicLong counter = new AtomicLong(0); |
| 39 | |
| 40 | private final Long number; |
| 41 | |
| 42 | public MockIntent(Long number) { |
| 43 | super(NetTestTools.APP_ID, Collections.emptyList()); |
| 44 | this.number = number; |
| 45 | } |
| 46 | |
| 47 | public Long number() { |
| 48 | return number; |
| 49 | } |
| 50 | |
| 51 | public static Long nextId() { |
| 52 | return counter.getAndIncrement(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private static class MockTimestamp implements Timestamp { |
| 57 | final int value; |
| 58 | |
| 59 | MockTimestamp(int value) { |
| 60 | this.value = value; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int compareTo(Timestamp o) { |
| 65 | if (!(o instanceof MockTimestamp)) { |
| 66 | return -1; |
| 67 | } |
| 68 | MockTimestamp that = (MockTimestamp) o; |
| 69 | return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private Timestamp timestamp1; |
| 74 | private Timestamp timestamp2; |
| 75 | private Timestamp timestamp3; |
| 76 | |
| 77 | private Intent intent1; |
| 78 | private Intent intent2; |
| 79 | private Intent intent3; |
| 80 | |
| 81 | private IntentData data1; |
| 82 | private IntentData data1Copy; |
| 83 | private IntentData data2; |
| 84 | private IntentData data2Copy; |
| 85 | private IntentData data3; |
| 86 | private IntentData data3Copy; |
| 87 | |
| 88 | IdGenerator idGenerator; |
| 89 | |
| 90 | @Before |
| 91 | public void setUpTest() { |
| 92 | idGenerator = new MockIdGenerator(); |
| 93 | Intent.bindIdGenerator(idGenerator); |
| 94 | |
| 95 | timestamp1 = new MockTimestamp(1); |
| 96 | timestamp2 = new MockTimestamp(2); |
| 97 | timestamp3 = new MockTimestamp(3); |
| 98 | |
| 99 | intent1 = new MockIntent(1L); |
| 100 | intent2 = new MockIntent(2L); |
| 101 | intent3 = new MockIntent(3L); |
| 102 | |
| 103 | data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 104 | data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1); |
| 105 | data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 106 | data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2); |
| 107 | data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 108 | data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3); |
| 109 | } |
| 110 | |
| 111 | @After |
| 112 | public void tearDownTest() { |
| 113 | Intent.unbindIdGenerator(idGenerator); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Checks that intent data objects are properly constructed. |
| 118 | */ |
| 119 | @Test |
| 120 | public void checkConstruction() { |
| 121 | assertThat(data1.state(), is(IntentState.INSTALLED)); |
| 122 | assertThat(data1.version(), is(timestamp1)); |
| 123 | assertThat(data1.intent(), is(intent1)); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Checks equals() for intent data objects. |
| 128 | */ |
| 129 | @Test |
| 130 | public void checkEquals() { |
| 131 | new EqualsTester() |
| 132 | .addEqualityGroup(data1, data1Copy) |
| 133 | .addEqualityGroup(data2, data2Copy) |
| 134 | .addEqualityGroup(data3, data3Copy) |
| 135 | .testEquals(); |
| 136 | } |
| 137 | } |