Committed by
Gerrit Code Review
[onos-2613] - Unit testing of BgpAttrNodeFlagBitTlvTest
Change-Id: Ie34a30bcc3190f6e39648f0086598f46ddd5ff45
Showing
2 changed files
with
98 additions
and
31 deletions
... | @@ -30,7 +30,7 @@ import com.google.common.base.MoreObjects; | ... | @@ -30,7 +30,7 @@ import com.google.common.base.MoreObjects; |
30 | /** | 30 | /** |
31 | * Implements BGP attribute node flag. | 31 | * Implements BGP attribute node flag. |
32 | */ | 32 | */ |
33 | -public class BgpAttrNodeFlagBitTlv implements BGPValueType { | 33 | +public final class BgpAttrNodeFlagBitTlv implements BGPValueType { |
34 | 34 | ||
35 | protected static final Logger log = LoggerFactory | 35 | protected static final Logger log = LoggerFactory |
36 | .getLogger(BgpAttrNodeFlagBitTlv.class); | 36 | .getLogger(BgpAttrNodeFlagBitTlv.class); |
... | @@ -38,16 +38,15 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -38,16 +38,15 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
38 | public static final int ATTRNODE_FLAGBIT = 1024; | 38 | public static final int ATTRNODE_FLAGBIT = 1024; |
39 | 39 | ||
40 | /* Node flag bit TLV */ | 40 | /* Node flag bit TLV */ |
41 | - private boolean bOverloadBit; | 41 | + private final boolean bOverloadBit; |
42 | - private boolean bAttachedBit; | 42 | + private final boolean bAttachedBit; |
43 | - private boolean bExternalBit; | 43 | + private final boolean bExternalBit; |
44 | - private boolean bABRBit; | 44 | + private final boolean bAbrBit; |
45 | 45 | ||
46 | - public static final int BIT_SET = 1; | 46 | + public static final byte FIRST_BIT = (byte) 0x80; |
47 | - public static final int FIRST_BIT = 0x80; | 47 | + public static final byte SECOND_BIT = 0x40; |
48 | - public static final int SECOND_BIT = 0x40; | 48 | + public static final byte THIRD_BIT = 0x20; |
49 | - public static final int THIRD_BIT = 0x20; | 49 | + public static final byte FOURTH_BIT = 0x01; |
50 | - public static final int FOURTH_BIT = 0x01; | ||
51 | 50 | ||
52 | /** | 51 | /** |
53 | * Constructor to initialize parameters. | 52 | * Constructor to initialize parameters. |
... | @@ -55,14 +54,31 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -55,14 +54,31 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
55 | * @param bOverloadBit Overload bit | 54 | * @param bOverloadBit Overload bit |
56 | * @param bAttachedBit Attached bit | 55 | * @param bAttachedBit Attached bit |
57 | * @param bExternalBit External bit | 56 | * @param bExternalBit External bit |
58 | - * @param bABRBit ABR Bit | 57 | + * @param bAbrBit ABR Bit |
59 | */ | 58 | */ |
60 | - BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit, | 59 | + private BgpAttrNodeFlagBitTlv(boolean bOverloadBit, boolean bAttachedBit, |
61 | - boolean bExternalBit, boolean bABRBit) { | 60 | + boolean bExternalBit, boolean bAbrBit) { |
62 | this.bOverloadBit = bOverloadBit; | 61 | this.bOverloadBit = bOverloadBit; |
63 | this.bAttachedBit = bAttachedBit; | 62 | this.bAttachedBit = bAttachedBit; |
64 | this.bExternalBit = bExternalBit; | 63 | this.bExternalBit = bExternalBit; |
65 | - this.bABRBit = bABRBit; | 64 | + this.bAbrBit = bAbrBit; |
65 | + } | ||
66 | + | ||
67 | + /** | ||
68 | + * Returns object of this class with specified values. | ||
69 | + * | ||
70 | + * @param bOverloadBit Overload bit | ||
71 | + * @param bAttachedBit Attached bit | ||
72 | + * @param bExternalBit External bit | ||
73 | + * @param bAbrBit ABR Bit | ||
74 | + * @return object of BgpAttrNodeFlagBitTlv | ||
75 | + */ | ||
76 | + public static BgpAttrNodeFlagBitTlv of(final boolean bOverloadBit, | ||
77 | + final boolean bAttachedBit, | ||
78 | + final boolean bExternalBit, | ||
79 | + final boolean bAbrBit) { | ||
80 | + return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit, | ||
81 | + bExternalBit, bAbrBit); | ||
66 | } | 82 | } |
67 | 83 | ||
68 | /** | 84 | /** |
... | @@ -77,11 +93,11 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -77,11 +93,11 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
77 | boolean bOverloadBit = false; | 93 | boolean bOverloadBit = false; |
78 | boolean bAttachedBit = false; | 94 | boolean bAttachedBit = false; |
79 | boolean bExternalBit = false; | 95 | boolean bExternalBit = false; |
80 | - boolean bABRBit = false; | 96 | + boolean bAbrBit = false; |
81 | 97 | ||
82 | short lsAttrLength = cb.readShort(); | 98 | short lsAttrLength = cb.readShort(); |
83 | 99 | ||
84 | - if (lsAttrLength != 1) { | 100 | + if ((lsAttrLength != 1) || (cb.readableBytes() < lsAttrLength)) { |
85 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, | 101 | Validation.validateLen(BGPErrorType.UPDATE_MESSAGE_ERROR, |
86 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, | 102 | BGPErrorType.ATTRIBUTE_LENGTH_ERROR, |
87 | lsAttrLength); | 103 | lsAttrLength); |
... | @@ -89,13 +105,13 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -89,13 +105,13 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
89 | 105 | ||
90 | byte nodeFlagBits = cb.readByte(); | 106 | byte nodeFlagBits = cb.readByte(); |
91 | 107 | ||
92 | - bOverloadBit = ((nodeFlagBits & (byte) FIRST_BIT) == FIRST_BIT); | 108 | + bOverloadBit = ((nodeFlagBits & FIRST_BIT) == FIRST_BIT); |
93 | - bAttachedBit = ((nodeFlagBits & (byte) SECOND_BIT) == SECOND_BIT); | 109 | + bAttachedBit = ((nodeFlagBits & SECOND_BIT) == SECOND_BIT); |
94 | - bExternalBit = ((nodeFlagBits & (byte) THIRD_BIT) == THIRD_BIT); | 110 | + bExternalBit = ((nodeFlagBits & THIRD_BIT) == THIRD_BIT); |
95 | - bABRBit = ((nodeFlagBits & (byte) FOURTH_BIT) == FOURTH_BIT); | 111 | + bAbrBit = ((nodeFlagBits & FOURTH_BIT) == FOURTH_BIT); |
96 | 112 | ||
97 | - return new BgpAttrNodeFlagBitTlv(bOverloadBit, bAttachedBit, | 113 | + return BgpAttrNodeFlagBitTlv.of(bOverloadBit, bAttachedBit, |
98 | - bExternalBit, bABRBit); | 114 | + bExternalBit, bAbrBit); |
99 | } | 115 | } |
100 | 116 | ||
101 | /** | 117 | /** |
... | @@ -103,7 +119,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -103,7 +119,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
103 | * | 119 | * |
104 | * @return Overload Bit | 120 | * @return Overload Bit |
105 | */ | 121 | */ |
106 | - boolean getOverLoadBit() { | 122 | + public boolean overLoadBit() { |
107 | return bOverloadBit; | 123 | return bOverloadBit; |
108 | } | 124 | } |
109 | 125 | ||
... | @@ -112,7 +128,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -112,7 +128,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
112 | * | 128 | * |
113 | * @return Attached Bit | 129 | * @return Attached Bit |
114 | */ | 130 | */ |
115 | - boolean getAttachedBit() { | 131 | + public boolean attachedBit() { |
116 | return bAttachedBit; | 132 | return bAttachedBit; |
117 | } | 133 | } |
118 | 134 | ||
... | @@ -121,7 +137,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -121,7 +137,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
121 | * | 137 | * |
122 | * @return External Bit | 138 | * @return External Bit |
123 | */ | 139 | */ |
124 | - boolean getExternalBit() { | 140 | + public boolean externalBit() { |
125 | return bExternalBit; | 141 | return bExternalBit; |
126 | } | 142 | } |
127 | 143 | ||
... | @@ -130,8 +146,8 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -130,8 +146,8 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
130 | * | 146 | * |
131 | * @return ABR Bit | 147 | * @return ABR Bit |
132 | */ | 148 | */ |
133 | - boolean getABRBit() { | 149 | + public boolean abrBit() { |
134 | - return bABRBit; | 150 | + return bAbrBit; |
135 | } | 151 | } |
136 | 152 | ||
137 | @Override | 153 | @Override |
... | @@ -141,13 +157,13 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -141,13 +157,13 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
141 | 157 | ||
142 | @Override | 158 | @Override |
143 | public int write(ChannelBuffer cb) { | 159 | public int write(ChannelBuffer cb) { |
144 | - // TODO will be implementing it later | 160 | + // TODO This will be implemented in the next version |
145 | return 0; | 161 | return 0; |
146 | } | 162 | } |
147 | 163 | ||
148 | @Override | 164 | @Override |
149 | public int hashCode() { | 165 | public int hashCode() { |
150 | - return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bABRBit); | 166 | + return Objects.hash(bOverloadBit, bAttachedBit, bExternalBit, bAbrBit); |
151 | } | 167 | } |
152 | 168 | ||
153 | @Override | 169 | @Override |
... | @@ -161,7 +177,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -161,7 +177,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
161 | return Objects.equals(bOverloadBit, other.bOverloadBit) | 177 | return Objects.equals(bOverloadBit, other.bOverloadBit) |
162 | && Objects.equals(bAttachedBit, other.bAttachedBit) | 178 | && Objects.equals(bAttachedBit, other.bAttachedBit) |
163 | && Objects.equals(bExternalBit, other.bExternalBit) | 179 | && Objects.equals(bExternalBit, other.bExternalBit) |
164 | - && Objects.equals(bABRBit, other.bABRBit); | 180 | + && Objects.equals(bAbrBit, other.bAbrBit); |
165 | } | 181 | } |
166 | return false; | 182 | return false; |
167 | } | 183 | } |
... | @@ -171,7 +187,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { | ... | @@ -171,7 +187,7 @@ public class BgpAttrNodeFlagBitTlv implements BGPValueType { |
171 | return MoreObjects.toStringHelper(getClass()) | 187 | return MoreObjects.toStringHelper(getClass()) |
172 | .add("bOverloadBit", bOverloadBit) | 188 | .add("bOverloadBit", bOverloadBit) |
173 | .add("bAttachedBit", bAttachedBit) | 189 | .add("bAttachedBit", bAttachedBit) |
174 | - .add("bExternalBit", bExternalBit).add("bABRBit", bABRBit) | 190 | + .add("bExternalBit", bExternalBit).add("bAbrBit", bAbrBit) |
175 | .toString(); | 191 | .toString(); |
176 | } | 192 | } |
177 | } | 193 | } | ... | ... |
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.bgp; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | +import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv; | ||
20 | + | ||
21 | +import com.google.common.testing.EqualsTester; | ||
22 | + | ||
23 | +/** | ||
24 | + * Test for BGP attribute node flag. | ||
25 | + */ | ||
26 | +public class BgpAttrNodeFlagBitTlvTest { | ||
27 | + | ||
28 | + private final boolean bOverloadBit = true; | ||
29 | + private final boolean bAttachedBit = true; | ||
30 | + private final boolean bExternalBit = true; | ||
31 | + private final boolean bABRBit = true; | ||
32 | + | ||
33 | + private final boolean bOverloadBit1 = false; | ||
34 | + private final boolean bAttachedBit1 = false; | ||
35 | + private final boolean bExternalBit1 = false; | ||
36 | + private final boolean bABRBit1 = false; | ||
37 | + | ||
38 | + private final BgpAttrNodeFlagBitTlv data = BgpAttrNodeFlagBitTlv | ||
39 | + .of(bOverloadBit, bAttachedBit, bExternalBit, bABRBit); | ||
40 | + private final BgpAttrNodeFlagBitTlv sameAsData = BgpAttrNodeFlagBitTlv | ||
41 | + .of(bOverloadBit, bAttachedBit, bExternalBit, bABRBit); | ||
42 | + private final BgpAttrNodeFlagBitTlv diffData = BgpAttrNodeFlagBitTlv | ||
43 | + .of(bOverloadBit1, bAttachedBit1, bExternalBit1, bABRBit1); | ||
44 | + | ||
45 | + @Test | ||
46 | + public void basics() { | ||
47 | + | ||
48 | + new EqualsTester().addEqualityGroup(data, sameAsData) | ||
49 | + .addEqualityGroup(diffData).testEquals(); | ||
50 | + } | ||
51 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment