Commit 1c2cdf1d authored by Maxym Borodenko's avatar Maxym Borodenko
Browse files

Languages API endpoint

parent e8a8aa73
Pipeline #4017 passed with stage
in 1 minute and 37 seconds
......@@ -21,6 +21,8 @@ import java.util.concurrent.TimeUnit;
import org.genesys.catalog.server.components.AddStuffInterceptor;
import org.genesys.catalog.server.components.StartupInitializer;
import org.genesys.catalog.service.worker.GeonamesISOLanguageSource;
import org.genesys.catalog.service.worker.ISO639VocabularyUpdater;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
......@@ -115,6 +117,16 @@ public class WebConfiguration extends WebMvcConfigurerAdapter {
return cookieLocaleResolver;
}
@Bean
public ISO639VocabularyUpdater iSO639VocabularyUpdater() {
return new ISO639VocabularyUpdater();
}
@Bean
public GeonamesISOLanguageSource geonamesISOLanguageSource() {
return new GeonamesISOLanguageSource();
}
@Bean
public AddStuffInterceptor addStuffInterceptor() {
return new AddStuffInterceptor();
......
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.controller.api.v0;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.vocab.VocabularyTerm;
import org.genesys.catalog.service.worker.GeonamesISOLanguageSource.LanguageInfo;
import org.genesys.catalog.service.worker.ISO639VocabularyUpdater;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* The Class LanguagesController.
*
* @author Maxym Borodenko
*/
@RestController
@RequestMapping(LanguagesController.API_BASE)
@PreAuthorize("isAuthenticated()")
public class LanguagesController {
protected static final String API_BASE = "/api/v0/lang";
private static final Logger LOG = LoggerFactory.getLogger(LanguagesController.class);
@Autowired
private ISO639VocabularyUpdater iso639VocabularyUpdater;
@GetMapping(value = "/{code}", produces = MediaType.APPLICATION_JSON_VALUE)
public LanguageInfo get(@PathVariable("code") final String code) throws IOException {
final VocabularyTerm vocabularyTerm = iso639VocabularyUpdater.getISO639Vocabulary().getTerms().stream()
.filter(term -> term.getCode().toLowerCase().equals(code.toLowerCase())).findFirst()
.orElseThrow(() -> new NotFoundElement("Language with such code not found"));
return new LanguageInfo(vocabularyTerm.getCode(), vocabularyTerm.getTitle());
}
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<LanguageInfo> autocomplete(@RequestParam("l") final String languageLike) throws IOException {
if (languageLike.length() < 3)
return Collections.emptyList();
final List<LanguageInfo> languages = new ArrayList<>(20);
iso639VocabularyUpdater.getISO639Vocabulary().getTerms().stream()
.filter(term -> term.getTitle().toLowerCase().startsWith(languageLike.toLowerCase()))
.limit(20)
.forEach(term -> languages.add(new LanguageInfo(term.getCode(), term.getTitle())));
return languages;
}
}
/*
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.catalog.server.controller.api.v0;
import org.genesys.catalog.server.controller.rest.AbstractRestTest;
import org.genesys.catalog.server.controller.rest.WithMockOAuth2Authentication;
import org.genesys.catalog.service.worker.GeonamesISOLanguageSource.LanguageInfo;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* The Class LanguagesControllerTest.
*
* @author Maxym Borodenko
*/
public class LanguagesControllerTest extends AbstractRestTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
@Transactional
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(documentationConfiguration(restDocumentation).uris().withScheme("https").withHost(
"api.catalog.genesys-pgr.org").withPort(443)).build();
}
@Test
@WithMockOAuth2Authentication()
public void getLanguageInfoByCodeTest() throws Exception {
final LanguageInfo languageInfo = new LanguageInfo("ukr", "Ukrainian");
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(LanguagesController.API_BASE.concat("/{code}"), languageInfo.getCode()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.code", is(languageInfo.getCode())))
.andExpect(jsonPath("$.language", is(languageInfo.getLanguage())));
/*@formatter:on*/
}
@Test
@WithMockOAuth2Authentication()
public void listLanguageInfoTest() throws Exception {
final LanguageInfo languageInfo = new LanguageInfo("ukr", "Ukrainian");
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(LanguagesController.API_BASE.concat("/autocomplete"))
.param("l", languageInfo.getLanguage().substring(0, 4))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.[0]", not(nullValue())))
.andExpect(jsonPath("$.[0].code", not(nullValue())))
.andExpect(jsonPath("$.[0].language", not(nullValue())));
/*@formatter:on*/
}
@Test
@WithMockOAuth2Authentication()
public void searchTermToShortTest() throws Exception {
final LanguageInfo languageInfo = new LanguageInfo("ukr", "Ukrainian");
/*@formatter:off*/
mockMvc
.perform(RestDocumentationRequestBuilders.get(LanguagesController.API_BASE.concat("/autocomplete"))
.param("l", languageInfo.getLanguage().substring(0, 2))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(content().string("[]"));
/*@formatter:on*/
}
}
......@@ -33,6 +33,8 @@ import org.genesys.catalog.server.controller.api.v0.LocationController;
import org.genesys.catalog.server.controller.api.v0.PartnerController;
import org.genesys.catalog.server.controller.api.v0.PermissionController;
import org.genesys.catalog.server.tests.ServiceTest;
import org.genesys.catalog.service.worker.GeonamesISOLanguageSource;
import org.genesys.catalog.service.worker.ISO639VocabularyUpdater;
import org.junit.Rule;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Bean;
......@@ -89,6 +91,16 @@ public abstract class AbstractRestTest extends ServiceTest {
return new OAuthServiceImpl();
}
@Bean
public ISO639VocabularyUpdater iSO639VocabularyUpdater() {
return new ISO639VocabularyUpdater();
}
@Bean
public GeonamesISOLanguageSource geonamesISOLanguageSource() {
return new GeonamesISOLanguageSource();
}
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
final MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment