Skip to content

Commit 4e78eed

Browse files
committed
Mockito-Update, Tag-Recommendation-API + Tests, WIP
1 parent f3c0fc4 commit 4e78eed

File tree

9 files changed

+148
-7
lines changed

9 files changed

+148
-7
lines changed

pom.xml

+9-3
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,16 @@
8181
</dependency>
8282
<dependency>
8383
<groupId>org.mockito</groupId>
84-
<artifactId>mockito-all</artifactId>
85-
<version>1.10.19</version>
84+
<artifactId>mockito-core</artifactId>
85+
<version>5.3.1</version>
8686
<scope>test</scope>
87-
</dependency>
87+
</dependency>
88+
<dependency>
89+
<groupId>com.jayway.jsonpath</groupId>
90+
<artifactId>json-path</artifactId>
91+
<version>2.8.0</version>
92+
<scope>test</scope>
93+
</dependency>
8894

8995
<!-- Messaging -->
9096
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring -->

src/main/java/de/holarse/backend/db/repositories/SearchRepository.java

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import de.holarse.backend.db.SearchIndex;
1111
import de.holarse.backend.db.datasets.SearchResultView;
12+
import de.holarse.backend.view.TagRecommendation;
1213
import java.util.List;
1314
import org.springframework.data.domain.Page;
1415
import org.springframework.data.domain.Pageable;
@@ -41,4 +42,12 @@ public interface SearchRepository extends JpaRepository<SearchIndex, Integer> {
4142
@Query(value = "refresh materialized view mv_searchindex", nativeQuery = true)
4243
void refreshIndex();
4344

45+
/**
46+
* Ermittelt Tags als Vorschlag in der Tagleiste
47+
* @param query
48+
* @return
49+
*/
50+
@Query(value = "select ms.wlabel as label, ms.use_count as useCount from mv_suggestions ms where ms.wtype = 'tag' and word @@ websearch_to_tsquery('german', :query) order by use_count", nativeQuery = true)
51+
List<TagRecommendation> autocompleteTags(@Param("query") final String query);
52+
4453
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package de.holarse.backend.view;
2+
3+
public record TagRecommendation(String label, int useCount) {}

src/main/java/de/holarse/config/MultipleHttpSecurityConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public SecurityFilterChain webFormSecurityFilterChain(final HttpSecurity http, f
132132
antMatcher("/workspace/**"),
133133
antMatcher("/wiki/*/edit"),
134134
antMatcher("/news/*/edit"),
135+
antMatcher("/webapi/**"),
135136
antMatcher("/logout")).authenticated())
136137

137138
// Normale Webseite, auch als Gast nutzbar
@@ -147,7 +148,7 @@ public SecurityFilterChain webFormSecurityFilterChain(final HttpSecurity http, f
147148
antMatcher("/privacy"),
148149
antMatcher("/impressum"),
149150
antMatcher("/imprint")).permitAll())
150-
151+
151152
// Form-Login
152153
.formLogin(form -> form.loginPage("/login").permitAll()
153154
.successHandler(successHandler())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.holarse.web.api;
2+
3+
import de.holarse.backend.db.repositories.SearchRepository;
4+
import de.holarse.backend.view.TagView;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.http.MediaType;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
17+
@RestController
18+
@RequestMapping(value = {"/webapi/tags"})
19+
public class TagApiController {
20+
21+
private final static transient Logger logger = LoggerFactory.getLogger(TagApiController.class);
22+
23+
@Autowired
24+
SearchRepository searchRepository;
25+
26+
@GetMapping(value = "autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
27+
public List<TagView> autoComplete(@RequestParam final String query) {
28+
logger.info("Autocomplete request for input {}", query);
29+
return new ArrayList<>();
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package de.holarse.web.api;
2+
3+
import de.holarse.backend.db.ApiUser;
4+
import de.holarse.backend.db.repositories.SearchRepository;
5+
import de.holarse.backend.view.TagRecommendation;
6+
import static org.junit.jupiter.api.Assertions.*;
7+
8+
import de.holarse.test.TestHelper;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
import org.mockito.MockitoAnnotations;
14+
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
15+
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
import org.springframework.http.MediaType;
19+
import org.springframework.security.test.context.support.WithMockUser;
20+
import org.springframework.test.web.servlet.MockMvc;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
22+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
23+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
24+
25+
import org.springframework.test.web.servlet.ResultActions;
26+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
27+
import static org.hamcrest.MatcherAssert.*;
28+
import static org.hamcrest.Matchers.*;
29+
import org.mockito.Mock;
30+
import static org.mockito.ArgumentMatchers.anyString;
31+
import static org.mockito.Mockito.when;
32+
33+
public class TagApiControllerTest {
34+
35+
private final static transient Logger log = LoggerFactory.getLogger(TagApiControllerTest.class);
36+
37+
@Mock
38+
SearchRepository searchRepositoryMock;
39+
40+
TagApiController controller;
41+
42+
@BeforeEach
43+
public void setup() throws Exception {
44+
MockitoAnnotations.openMocks(this);
45+
controller = new TagApiController();
46+
}
47+
@Test
48+
public void testRequestWithoutLogin() throws Exception {
49+
final String searchTerm = "döner";
50+
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
51+
mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf())).andExpect(status().is(200));
52+
// TODO: Should return redirect to login
53+
}
54+
55+
@Test
56+
@WithMockUser("admin")
57+
public void testRequestWithLogin() throws Exception {
58+
final String searchTerm = "döner";
59+
60+
controller.searchRepository = searchRepositoryMock;
61+
62+
when(searchRepositoryMock.autocompleteTags(anyString())).thenReturn(new ArrayList<>());
63+
64+
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
65+
ResultActions result = mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf()))
66+
.andExpect(status().isOk())
67+
.andExpect(resp -> assertThat("should be json", resp.getResponse().getContentType().equals(MediaType.APPLICATION_JSON_VALUE)));
68+
69+
result.andExpect(jsonPath("$", hasSize(0)));
70+
}
71+
72+
@Test
73+
@WithMockUser("admin")
74+
public void testSingleResult() throws Exception {
75+
controller.searchRepository = searchRepositoryMock;
76+
final String searchTerm = "döner";
77+
78+
final List<TagRecommendation> mockResult = List.of(new TagRecommendation("döner", 1));
79+
80+
when(searchRepositoryMock.autocompleteTags(searchTerm)).thenReturn(mockResult);
81+
82+
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
83+
ResultActions result = mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf()))
84+
.andExpect(status().isOk())
85+
.andExpect(resp -> assertThat("should be json", resp.getResponse().getContentType().equals(MediaType.APPLICATION_JSON_VALUE)));
86+
87+
result.andExpect(jsonPath("$", hasSize(1)));
88+
}
89+
90+
}

src/test/java/de/holarse/web/controller/SearchControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SearchControllerTest {
3333

3434
@BeforeEach
3535
public void setup() throws Exception {
36-
MockitoAnnotations.initMocks(this);
36+
MockitoAnnotations.openMocks(this);
3737
controller = new SearchController();
3838
}
3939

src/test/java/de/holarse/web/controller/StaticControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class StaticControllerTest {
1616

1717
@BeforeEach
1818
public void setup() throws Exception {
19-
MockitoAnnotations.initMocks(this);
19+
MockitoAnnotations.openMocks(this);
2020
controller = new StaticController();
2121
}
2222

src/test/java/de/holarse/web/controller/WelcomeControllerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class WelcomeControllerTest {
2525

2626
@BeforeEach
2727
public void setup() throws Exception {
28-
MockitoAnnotations.initMocks(this);
28+
MockitoAnnotations.openMocks(this);
2929
controller = new WelcomeController();
3030
controller.apiUserRepository = apiUserRepositoryMock;
3131
}

0 commit comments

Comments
 (0)