Skip to content

Commit 05e7003

Browse files
committed
feat: implement hashCode and equals methods in DataValue class, add unit tests for DataValue and GsAPI classes
1 parent 558a796 commit 05e7003

File tree

4 files changed

+119
-6
lines changed

4 files changed

+119
-6
lines changed

src/main/java/io/github/codenilson/gsapi_core/models/DataValue.java

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,46 @@ public void setStartRange(String range) {
7474
public String toString() {
7575
return "DataValue [rowData=" + values + ", startRange=" + startRange + "]";
7676
}
77-
}
77+
78+
/**
79+
* Computes a hash code for this DataValue object.
80+
*
81+
* @return A hash code value for this object.
82+
*/
83+
@Override
84+
public int hashCode() {
85+
final int prime = 31;
86+
int result = 1;
87+
result = prime * result + ((values == null) ? 0 : values.hashCode());
88+
result = prime * result + ((startRange == null) ? 0 : startRange.hashCode());
89+
return result;
90+
}
91+
92+
/**
93+
* Compares this DataValue object to another object for equality.
94+
*
95+
* @param obj The object to compare to.
96+
* @return {@code true} if the objects are equal; {@code false} otherwise.
97+
*/
98+
@Override
99+
public boolean equals(Object obj) {
100+
if (this == obj)
101+
return true;
102+
if (obj == null)
103+
return false;
104+
if (getClass() != obj.getClass())
105+
return false;
106+
DataValue other = (DataValue) obj;
107+
if (values == null) {
108+
if (other.values != null)
109+
return false;
110+
} else if (!values.equals(other.values))
111+
return false;
112+
if (startRange == null) {
113+
if (other.startRange != null)
114+
return false;
115+
} else if (!startRange.equals(other.startRange))
116+
return false;
117+
return true;
118+
}
119+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.github.codenilson.gsapi_core;
2+
3+
import static org.mockito.ArgumentMatchers.any;
4+
import static org.mockito.Mockito.when;
5+
6+
import java.io.IOException;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.mockito.Mock;
15+
import org.mockito.MockitoAnnotations;
16+
17+
import io.github.codenilson.gsapi_core.client.GoogleSheetsClient;
18+
import io.github.codenilson.gsapi_core.errors.GSAPIError;
19+
import io.github.codenilson.gsapi_core.models.DataValue;
20+
21+
public class GsAPITest {
22+
23+
@Mock
24+
private GoogleSheetsClient client;
25+
26+
private GsAPI api;
27+
28+
@BeforeEach
29+
void setUp() throws Exception {
30+
31+
MockitoAnnotations.openMocks(this);
32+
33+
List<List<Object>> values = Arrays.asList(
34+
Arrays.asList("header", "header2", "header3"),
35+
Arrays.asList("Test", "Test2", "Test3"),
36+
Arrays.asList("Test4", "Test5", "Test6"));
37+
38+
when(client.fetchValues(any(SheetRequest.class))).thenReturn(values);
39+
40+
api = new GsAPI(client);
41+
}
42+
43+
@Test
44+
void testRetrieveData() {
45+
DataValue dataValue1 = new DataValue(Arrays.asList("Test", "Test2", "Test3"), 1);
46+
DataValue dataValue2 = new DataValue(Arrays.asList("Test4", "Test5", "Test6"), 2);
47+
48+
List<DataValue> expected = Arrays.asList(dataValue1, dataValue2);
49+
50+
List<DataValue> result = api.retrieveData(new SheetRequest("test-id", "A1:C3")).get();
51+
52+
Assertions.assertEquals(expected, result, "retrieveData() should return the expected data values.");
53+
54+
}
55+
56+
@Test
57+
void testRetrieveDataRaisesGsAPIErrorIfDataIsNull() throws IOException {
58+
when(client.fetchValues(any(SheetRequest.class))).thenReturn(null);
59+
60+
Optional<List<DataValue>> result = api.retrieveData(new SheetRequest("test-id", "A1:C3"));
61+
62+
Assertions.assertTrue(result.isEmpty(), "Expected Optional.empty() when values are null");
63+
64+
}
65+
66+
@Test
67+
void testRetrieveDataThrowsGSAPIErrorOnIOException() throws IOException {
68+
when(client.fetchValues(any(SheetRequest.class))).thenThrow(new IOException("Network error"));
69+
70+
Assertions.assertThrows(GSAPIError.class, () -> {
71+
api.retrieveData(new SheetRequest("test-id", "A1:C3"));
72+
}, "Expected GSAPIError to be thrown when an IOException occurs");
73+
}
74+
}

src/test/java/io/github/codenilson/gsapi_core/DataValueTest.java renamed to src/test/java/io/github/codenilson/gsapi_core/models/DataValueTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.models;
22

33
import java.util.ArrayList;
44
import java.util.List;
@@ -7,8 +7,6 @@
77
import org.junit.jupiter.api.BeforeEach;
88
import org.junit.jupiter.api.Test;
99

10-
import io.github.codenilson.gsapi_core.models.DataValue;
11-
1210
public class DataValueTest {
1311

1412
private DataValue dataValue;

src/test/java/io/github/codenilson/gsapi_core/AuthenticatorTest.java renamed to src/test/java/io/github/codenilson/gsapi_core/utils/AuthenticatorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.codenilson.gsapi_core;
1+
package io.github.codenilson.gsapi_core.utils;
22

33
import static org.mockito.ArgumentMatchers.any;
44
import static org.mockito.Mockito.mock;
@@ -18,7 +18,6 @@
1818
import com.google.auth.oauth2.GoogleCredentials;
1919

2020
import io.github.codenilson.gsapi_core.errors.GSAPIError;
21-
import io.github.codenilson.gsapi_core.utils.Authenticator;
2221

2322
class AuthenticatorTest {
2423

0 commit comments

Comments
 (0)