Skip to content

Commit

Permalink
Merge pull request #10 from SRGSSR/develop
Browse files Browse the repository at this point in the history
2
  • Loading branch information
SebastienChauvin authored Jul 9, 2018
2 parents 42f1cd6 + e41ef99 commit 9ebb1ca
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.example</groupId>
<artifactId>pfff</artifactId>
<version>0.0.1-SNAPSHOT</version>
<version>2</version>
<packaging>jar</packaging>

<name>pfff</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public AuthenticationConfig(
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/v1/update/check", "/api/v1/whatisnew/text", "/api/v1/whatisnew/html", "/api/v1/version", "/webjars/**/*").permitAll()
.antMatchers("/api/v1/update/check", "/api/v1/whatisnew/text", "/api/v1/whatisnew/html", "/api/v1/version", "/api/v1/playlist/recommendation/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.pfff.controller;

import com.example.pfff.service.RecommendationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* Copyright (c) SRG SSR. All rights reserved.
* <p>
* License information is available from the LICENSE file.
*/
@Controller
public class RecommendationController {
@Autowired
RecommendationService service;

@RequestMapping("/api/v1/playlist/recommendation/{purpose}/{urn}")
@ResponseBody
Object recommendation(
HttpServletRequest request,
@PathVariable("purpose") String purpose,
@PathVariable("urn") String urn,
@RequestParam(value = "standalone", required = false, defaultValue = "false") boolean standalone,
@RequestParam(value = "format", required = false, defaultValue = "media") String format) {
List<String> urns = service.getRecommendedUrns(purpose, urn, standalone);
urns.add(0, urn);
if ("urn".equals(format)) {
return urns;
} else { //if ("media".equals(format)) {
UriComponentsBuilder builder = UriComponentsBuilder.newInstance();
builder.scheme(request.getScheme());
builder.host("il.srgssr.ch");
builder.path("integrationlayer/2.0/mediaList/byUrns.json");
builder.queryParam("urns", String.join(",", urns));
return new ModelAndView(new RedirectView(builder.toUriString()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.pfff.model.peach;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.ArrayList;
import java.util.List;

public class RecommendationResult {
@JsonProperty("status")
private String status;

@JsonProperty("result")
private Result result;

private static class Result {
@JsonProperty("items")
private Item[] items;
}

private static class Item {
@JsonProperty("id")
private String id;
@JsonProperty("urn")
private String urn;
}

public List<String> getUrns() {
ArrayList<String> urns = new ArrayList<>();
if (result != null) {
if (result.items != null) {
for (int i = 0; i < result.items.length; i++) {
Item item = result.items[i];
if (item.urn != null) {
urns.add(item.urn);
} else {
urns.add("urn:rts:video:" + item.id);
}
}
return urns;
}
}
return urns;
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/example/pfff/service/RecommendationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.pfff.service;

import com.example.pfff.model.peach.RecommendationResult;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.List;

@Service
public class RecommendationService {

private RestTemplate restTemplate;

public RecommendationService() {
restTemplate = new RestTemplate();
}

public List<String> getRecommendedUrns(String purpose, String urn, boolean standalone) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance().scheme("http").host("peach.ebu.io").path("api/v1/chrts/continuous_playback_mobile");
uriComponentsBuilder.queryParam("urn", urn);
uriComponentsBuilder.queryParam("purpose", purpose);
uriComponentsBuilder.queryParam("pageSize", 50);
uriComponentsBuilder.queryParam("standalone", standalone);
UriComponents url = uriComponentsBuilder.build();

System.out.println(url.toUriString());

return restTemplate.exchange(url.toUriString(), HttpMethod.GET, null, RecommendationResult.class).getBody().getUrns();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.example.pfff.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.WebApplicationContext;

import java.util.List;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RecommendationIntegrationTest {
private MockMvc mvc;

@Autowired
private WebApplicationContext context;

@Before
public void setup() {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build();
}

@Test
public void getRecommendation() throws Exception {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:9691670";

mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN)).andExpect(status().isFound()).andExpect(content().string(""));

checkRecommendationWithRedirect(purpose, mediaURN, "false");

checkRecommendationWithRedirect(purpose, mediaURN, "true");
}

private void checkRecommendationWithRedirect(String purpose, String mediaURN, String standalone) throws Exception {
MvcResult mvcResult = mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", standalone)).andExpect(status().is3xxRedirection()).andExpect(content().string("")).andReturn();
String location = mvcResult.getResponse().getHeader("Location");
System.out.println(location);
RestTemplate restTemplate = new RestTemplate();
MediaListResult result = restTemplate.exchange(location, HttpMethod.GET, null, MediaListResult.class).getBody();
Assert.assertNotNull(result.mediaList);
}

static class MediaListResult {
@JsonProperty("mediaList")
List<Object> mediaList;
}

@Test
public void getRecommendationURNFormat() throws Exception {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:9691670";
String format = "urn";

mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray());

mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", "false").param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray());

mvc.perform(get("/api/v1/playlist/recommendation/" + purpose + "/" + mediaURN).param("standalone", "true").param("format", format)).andExpect(status().isOk()).andExpect(jsonPath("$").isArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.example.pfff.controller;

import com.example.pfff.service.RecommendationService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class RecommendationServiceTests {

@Autowired
private RecommendationService recommendationService;

@Test
public void getRecommendedUrnsContinuousplaybackRTSTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:9691670";
boolean standalone = false;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}

@Test
public void getRecommendedUrnsContinuousplaybackStandaloneRTSTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:9691670";
boolean standalone = true;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}

@Test
public void getRecommendedUrnsContinuousplaybackSRFTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:859dc7e6-a155-41da-9d34-8f4eb800f73c";
boolean standalone = false;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}

@Test
public void getRecommendedUrnsContinuousplaybackStandaloneSRFTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:rts:video:859dc7e6-a155-41da-9d34-8f4eb800f73c";
boolean standalone = true;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}

@Test
public void getRecommendedUrnsContinuousplaybackSwissTxtTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:swisstxt:video:rts:288208";
boolean standalone = false;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}

@Test
public void getRecommendedUrnsContinuousplaybackStandaloneSwisstxtTest() {
String purpose = "continuousplayback";
String mediaURN = "urn:swisstxt:video:rts:288208";
boolean standalone = true;
List<String> urns = recommendationService.getRecommendedUrns(purpose, mediaURN, standalone);

Assert.assertNotNull(urns);
}
}

0 comments on commit 9ebb1ca

Please sign in to comment.