Skip to content

Commit b93f603

Browse files
author
Ramgopal Nagaboina
authored
api: fix inverted value comparison in ImageStoreDetailResponse.equals (#14023)
equals() returned false when the two responses had the SAME value and true when the values DIFFERED (the value branch was inverted): else if (this.getValue().equals(other.getValue())) return false; So two identical details were treated as unequal and two details differing only by value were treated as equal, corrupting any Set/Map/dedup keyed on ImageStoreDetailResponse. It also NPEd when value was null. Compare with !Objects.equals(getValue(), other.getValue()), which restores the correct result and is null-safe. Adds tests for equal name+value, differing value, and differing name.
1 parent 6e4413d commit b93f603

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎api/src/main/java/org/apache/cloudstack/api/response/ImageStoreDetailResponse.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.response;
1818

19+
import java.util.Objects;
20+
1921
import com.google.gson.annotations.SerializedName;
2022

2123
import org.apache.cloudstack.api.BaseResponse;
@@ -81,7 +83,7 @@ public boolean equals(Object obj) {
8183
return false;
8284
} else if (!oid.equals(other.getName()))
8385
return false;
84-
else if (this.getValue().equals(other.getValue()))
86+
else if (!Objects.equals(this.getValue(), other.getValue()))
8587
return false;
8688
return true;
8789
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.response;
18+
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
public class ImageStoreDetailResponseTest {
23+
24+
@Test
25+
public void equalsIsTrueForSameNameAndValue() {
26+
ImageStoreDetailResponse a = new ImageStoreDetailResponse("key", "value");
27+
ImageStoreDetailResponse b = new ImageStoreDetailResponse("key", "value");
28+
Assert.assertEquals(a, b);
29+
Assert.assertEquals(a.hashCode(), b.hashCode());
30+
}
31+
32+
@Test
33+
public void equalsIsFalseWhenValueDiffers() {
34+
ImageStoreDetailResponse a = new ImageStoreDetailResponse("key", "value");
35+
ImageStoreDetailResponse c = new ImageStoreDetailResponse("key", "other");
36+
Assert.assertNotEquals(a, c);
37+
}
38+
39+
@Test
40+
public void equalsIsFalseWhenNameDiffers() {
41+
ImageStoreDetailResponse a = new ImageStoreDetailResponse("key", "value");
42+
ImageStoreDetailResponse d = new ImageStoreDetailResponse("other", "value");
43+
Assert.assertNotEquals(a, d);
44+
}
45+
}

0 commit comments

Comments
 (0)