Skip to content

Commit 8b95ca1

Browse files
rohit11544GMishx
authored andcommitted
test(integration): Added DatabaseSanitation tests in DatabaseSanitationTest
1 parent a5b70d0 commit 8b95ca1

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright Rohit Borra, 2025. Part of the SW360 GSOC Project.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*/
10+
11+
package org.eclipse.sw360.rest.resourceserver.integration;
12+
13+
import com.fasterxml.jackson.databind.JsonNode;
14+
import com.fasterxml.jackson.databind.ObjectMapper;
15+
import org.apache.thrift.TException;
16+
import org.eclipse.sw360.datahandler.thrift.users.User;
17+
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
18+
import org.eclipse.sw360.rest.resourceserver.databasesanitation.Sw360DatabaseSanitationService;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.beans.factory.annotation.Value;
23+
import org.springframework.boot.test.web.client.TestRestTemplate;
24+
import org.springframework.http.HttpEntity;
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.HttpMethod;
27+
import org.springframework.http.HttpStatus;
28+
import org.springframework.http.ResponseEntity;
29+
import org.springframework.security.access.AccessDeniedException;
30+
import org.springframework.test.context.bean.override.mockito.MockitoBean;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
33+
import java.io.IOException;
34+
import java.util.HashMap;
35+
import java.util.List;
36+
import java.util.Map;
37+
38+
import static org.junit.Assert.assertEquals;
39+
import static org.junit.Assert.assertTrue;
40+
import static org.mockito.ArgumentMatchers.any;
41+
import static org.mockito.BDDMockito.given;
42+
43+
@RunWith(SpringJUnit4ClassRunner.class)
44+
public class DatabaseSanitationTest extends TestIntegrationBase {
45+
46+
@Value("${local.server.port}")
47+
private int port;
48+
49+
@MockitoBean
50+
private Sw360DatabaseSanitationService sanitationServiceMock;
51+
52+
private User adminUser;
53+
54+
@Before
55+
public void setUp() {
56+
adminUser = new User();
57+
adminUser.setEmail("[email protected]");
58+
adminUser.setUserGroup(UserGroup.ADMIN);
59+
given(userServiceMock.getUserByEmailOrExternalId("[email protected]")).willReturn(adminUser);
60+
given(userServiceMock.getUserByEmail("[email protected]")).willReturn(adminUser);
61+
}
62+
63+
@Test
64+
public void should_search_duplicate_identifiers_success() throws IOException, TException {
65+
Map<String, Map<String, List<String>>> responseMap = new HashMap<>();
66+
responseMap.put("duplicateComponents", Map.of("compA", List.of("id1", "id2")));
67+
given(sanitationServiceMock.duplicateIdentifiers(any())).willReturn(responseMap);
68+
69+
HttpHeaders headers = getHeaders(port);
70+
HttpEntity<Void> request = new HttpEntity<>(headers);
71+
ResponseEntity<String> response = new TestRestTemplate().exchange(
72+
"http://localhost:" + port + "/api/databaseSanitation/searchDuplicate",
73+
HttpMethod.GET,
74+
request,
75+
String.class
76+
);
77+
78+
assertEquals(HttpStatus.OK, response.getStatusCode());
79+
String body = response.getBody();
80+
assertTrue(body.contains("duplicateComponents"));
81+
assertTrue(body.contains("id1"));
82+
}
83+
84+
@Test
85+
public void should_return_no_content_when_no_duplicates() throws IOException, TException {
86+
Map<String, Map<String, List<String>>> responseMap = new HashMap<>();
87+
given(sanitationServiceMock.duplicateIdentifiers(any())).willReturn(responseMap);
88+
89+
HttpHeaders headers = getHeaders(port);
90+
HttpEntity<Void> request = new HttpEntity<>(headers);
91+
ResponseEntity<String> response = new TestRestTemplate().exchange(
92+
"http://localhost:" + port + "/api/databaseSanitation/searchDuplicate",
93+
HttpMethod.GET,
94+
request,
95+
String.class
96+
);
97+
98+
assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
99+
// Controller returns empty map body with 204; accept either truly empty or '{}'
100+
String body = response.getBody();
101+
assertTrue(body == null || body.trim().isEmpty() || body.trim().equals("{}"));
102+
}
103+
104+
@Test
105+
public void should_handle_texception_from_service() throws IOException, TException {
106+
given(sanitationServiceMock.duplicateIdentifiers(any())).willThrow(new TException("thrift error"));
107+
108+
HttpHeaders headers = getHeaders(port);
109+
HttpEntity<Void> request = new HttpEntity<>(headers);
110+
ResponseEntity<String> response = new TestRestTemplate().exchange(
111+
"http://localhost:" + port + "/api/databaseSanitation/searchDuplicate",
112+
HttpMethod.GET,
113+
request,
114+
String.class
115+
);
116+
117+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
118+
JsonNode json = new ObjectMapper().readTree(response.getBody());
119+
assertTrue(json.has("status"));
120+
assertTrue(json.has("error"));
121+
}
122+
123+
@Test
124+
public void should_handle_access_denied_from_service() throws IOException, TException {
125+
given(sanitationServiceMock.duplicateIdentifiers(any())).willThrow(new AccessDeniedException("no admin"));
126+
127+
HttpHeaders headers = getHeaders(port);
128+
HttpEntity<Void> request = new HttpEntity<>(headers);
129+
ResponseEntity<String> response = new TestRestTemplate().exchange(
130+
"http://localhost:" + port + "/api/databaseSanitation/searchDuplicate",
131+
HttpMethod.GET,
132+
request,
133+
String.class
134+
);
135+
136+
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
137+
}
138+
139+
@Test
140+
public void should_expose_database_sanitation_link_in_root() throws IOException {
141+
HttpHeaders headers = getHeaders(port);
142+
HttpEntity<Void> request = new HttpEntity<>(headers);
143+
ResponseEntity<String> response = new TestRestTemplate().exchange(
144+
"http://localhost:" + port + "/api",
145+
HttpMethod.GET,
146+
request,
147+
String.class
148+
);
149+
150+
assertEquals(HttpStatus.OK, response.getStatusCode());
151+
assertTrue(response.getBody() != null && response.getBody().contains("databaseSanitation"));
152+
}
153+
}

0 commit comments

Comments
 (0)