aboutsummaryrefslogtreecommitdiff
path: root/juick-api
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-01 13:56:53 +0300
committerGravatar Vitaly Takmazov2016-12-01 13:56:53 +0300
commit3d7f60c1c0b48dad2f58bffe3352d1c5c1b93f00 (patch)
treeab311d5d6102b2c76c03b266fc1b794108cba0fc /juick-api
parent92d6700919e87c81db4a42a4b286257f62566cbf (diff)
juick-api: Tags controller from perl, refactoring
Diffstat (limited to 'juick-api')
-rw-r--r--juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java3
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Tags.java45
-rw-r--r--juick-api/src/test/java/com/juick/api/tests/MessagesTests.java81
3 files changed, 103 insertions, 26 deletions
diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java
index 99731d2a..da7f5d3b 100644
--- a/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java
+++ b/juick-api/src/main/java/com/juick/api/configuration/ApiSecurityConfig.java
@@ -3,7 +3,6 @@ package com.juick.api.configuration;
import com.juick.server.security.JuickAuthenticationEntryPoint;
import com.juick.service.UserService;
import com.juick.service.security.JuickUserDetailsService;
-import com.juick.service.security.deprecated.CookieSimpleHashRememberMeServices;
import com.juick.service.security.deprecated.RequestParamHashRememberMeServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -45,7 +44,7 @@ public class ApiSecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.OPTIONS).permitAll()
- .antMatchers("/messages", "/users", "/thread").permitAll()
+ .antMatchers("/messages", "/users", "/thread", "/tags").permitAll()
.anyRequest().hasRole("USER")
.and().httpBasic().authenticationEntryPoint(getJuickAuthenticationEntryPoint())
.and().anonymous()
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Tags.java b/juick-api/src/main/java/com/juick/api/controllers/Tags.java
new file mode 100644
index 00000000..5548da17
--- /dev/null
+++ b/juick-api/src/main/java/com/juick/api/controllers/Tags.java
@@ -0,0 +1,45 @@
+package com.juick.api.controllers;
+
+import com.juick.User;
+import com.juick.server.helpers.TagStats;
+import com.juick.service.TagService;
+import com.juick.service.UserService;
+import com.juick.util.UserUtils;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import javax.inject.Inject;
+import java.security.Principal;
+import java.util.List;
+
+/**
+ * Created by vitalyster on 29.11.2016.
+ */
+@Controller
+@ResponseBody
+public class Tags {
+ @Inject
+ UserService userService;
+ @Inject
+ TagService tagService;
+
+ @RequestMapping(value = "/tags", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
+ public List<TagStats> tags(
+ Principal principal,
+ @RequestParam(required = false, defaultValue = "0") int user_id
+ ) {
+ String name = UserUtils.getUsername(principal, null);
+ User visitor = userService.getUserByName(name);
+ if (user_id == 0) {
+ user_id = visitor.getUid();
+ }
+ if (user_id > 0) {
+ return tagService.getUserTagStats(user_id);
+ }
+ return tagService.getTagStats();
+ }
+}
diff --git a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
index 05744a61..e0f9b840 100644
--- a/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
+++ b/juick-api/src/test/java/com/juick/api/tests/MessagesTests.java
@@ -7,7 +7,9 @@ import com.juick.api.configuration.ApiAppConfiguration;
import com.juick.api.configuration.ApiMvcConfiguration;
import com.juick.api.configuration.ApiSecurityConfig;
import com.juick.configuration.DataConfiguration;
+import com.juick.server.helpers.TagStats;
import com.juick.service.MessagesService;
+import com.juick.service.TagService;
import com.juick.service.UserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
@@ -59,6 +61,12 @@ public class MessagesTests {
UserService userService() {
return Mockito.mock(UserService.class);
}
+
+ @Bean
+ @Primary
+ TagService tagService() {
+ return Mockito.mock(TagService.class);
+ }
}
private MockMvc mockMvc;
@@ -69,6 +77,11 @@ public class MessagesTests {
private MessagesService messagesService;
@Inject
private UserService userService;
+ @Inject
+ private TagService tagService;
+
+ private User ugnich, freefd;
+ String ugnichName, ugnichPassword, freefdName, freefdPassword;
private static Message getMessage(final User user, final String messageText) {
Message msg = new Message();
@@ -98,12 +111,30 @@ public class MessagesTests {
.apply(SecurityMockMvcConfigurers.springSecurity())
.dispatchOptions(true)
.build();
+ ugnichName = "ugnich";
+ ugnichPassword = "MyPassw0rd!";
+ freefdName = "freefd";
+ freefdPassword = "MyPassw0rd!";
+
+ ugnich = getUser(1, ugnichName, ugnichPassword);
+ freefd = getUser(2, freefdName, freefdPassword);
+
+ List<String> users = new ArrayList<>(2);
+ users.add(ugnichName);
+ users.add(freefdName);
+
+ when(userService.getUsersByName(users))
+ .thenReturn(Arrays.asList(ugnich, freefd));
+ when(userService.getUserByName(ugnichName))
+ .thenReturn(ugnich);
+ when(userService.getFullyUserByName(ugnichName))
+ .thenReturn(ugnich);
+ when(userService.getUserByName(null))
+ .thenReturn(new User());
}
@Test
public void testAllUnAuthorized() throws Exception {
- when(userService.getUserByName(null))
- .thenReturn(new User());
mockMvc.perform(get("/"))
.andExpect(status().is4xxClientError());
@@ -120,17 +151,11 @@ public class MessagesTests {
@Test
public void homeTestWithMessages() throws Exception {
- String ugnichName = "ugnich";
- String uginchPassword = "MyPassw0rd!";
String msgText = "Привет, я - Угнич";
- User user = getUser(1, ugnichName, uginchPassword);
- Message msg = getMessage(user, msgText);
+ Message msg = getMessage(ugnich, msgText);
+
- when(userService.getUserByName(ugnichName))
- .thenReturn(user);
- when(userService.getFullyUserByName(ugnichName))
- .thenReturn(user);
when(messagesService.getMyFeed(1, 0))
.thenReturn(Collections.singletonList(1));
when(messagesService.getMessages(Collections.singletonList(1)))
@@ -138,7 +163,7 @@ public class MessagesTests {
mockMvc.perform(
get("/home")
- .with(httpBasic(ugnichName, uginchPassword)))
+ .with(httpBasic(ugnichName, ugnichPassword)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(1)))
@@ -233,20 +258,7 @@ public class MessagesTests {
@Test
public void anonymousApis() throws Exception {
- String ugnichName = "ugnich";
- String uginchPassword = "MyPassw0rd!";
- String freefdName = "freefd";
- String freefdPassword = "MyPassw0rd!";
-
- User ugnich = getUser(1, ugnichName, uginchPassword);
- User freefd = getUser(2, freefdName, freefdPassword);
-
- List<String> users = new ArrayList<>(2);
- users.add(ugnichName);
- users.add(freefdName);
- when(userService.getUsersByName(users))
- .thenReturn(Arrays.asList(ugnich, freefd));
mockMvc.perform(get("/messages"))
.andExpect(status().isOk());
@@ -258,4 +270,25 @@ public class MessagesTests {
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", hasSize(2)));
}
+ @Test
+ public void tags() throws Exception {
+ Tag weather = new Tag("weather");
+ TagStats sw = new TagStats();
+ sw.setTag(weather);
+ sw.setUsageCount(15);
+ Tag yo = new Tag("yo");
+ TagStats sy = new TagStats();
+ sy.setTag(yo);
+ sy.setUsageCount(5);
+ when(tagService.getUserTagStats(1)).thenReturn(Collections.singletonList(sy));
+ when(tagService.getTagStats()).thenReturn(Arrays.asList(sy, sw));
+ mockMvc.perform(get("/tags"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(2)));
+ mockMvc.perform(get("/tags")
+ .param("user_id", "1"))
+ .andExpect(status().isOk())
+ .andExpect(jsonPath("$", hasSize(1)))
+ .andExpect(jsonPath("$[0].messages", is(5)));
+ }
}