Created
March 2, 2012 18:41
-
-
Save anonymous/1960304 to your computer and use it in GitHub Desktop.
Test "featured should return all artists' info"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.fdvs.bandwdth | |
| import grails.converters.JSON | |
| import static java.net.HttpURLConnection.* | |
| class ArtistController { | |
| private static final String ERROR_MANDATORY_ID = 'Must receive artist id' | |
| def facebookService | |
| def lastFMService | |
| def youTubeService | |
| def twitterService | |
| def bingService | |
| def echoNestService | |
| def featuredArtistsService | |
| def facebookFeed(String facebookId) { | |
| if (!facebookId) | |
| return render(status: 400, text: 'Must receive Facebook ID') | |
| def feed = facebookService.getUserFeedJson(facebookId) | |
| withFormat { | |
| json { | |
| renderJson feed as JSON | |
| } | |
| } | |
| } | |
| def search(String id) { | |
| def result = lastFMService.artistSearch(id) | |
| renderJson result as JSON | |
| } | |
| // TODO Change name to 'search' when all apps (mobile and TV) are prepared for EchoNest | |
| def searchEchoNest(String name) { | |
| def result = echoNestService.artistSearch(name) | |
| renderJson result as JSON | |
| } | |
| // TODO Change name to 'show' when all apps (mobile and TV) are prepared for EchoNest | |
| def showEchoNest(String id) { | |
| try { | |
| def result = echoNestService.artistInfo(id) | |
| renderJson result as JSON | |
| } | |
| catch (IllegalArgumentException e) { | |
| render status: 400, text: e.message | |
| } | |
| } | |
| def show(String id) { | |
| def result = lastFMService.artistInfo(id) | |
| renderJson result as JSON | |
| } | |
| def searchTwitter(String id) { | |
| def result = twitterService.searchTweets(id) | |
| renderJson result as JSON | |
| } | |
| def searchBingImage(String id) { | |
| def result = bingService.searchImages(id) | |
| renderJson result as JSON | |
| } | |
| def searchYoutube(String id) { | |
| def topTracks = lastFMService.topTracks(id) | |
| def videoResults = youTubeService.searchVideos(id, params.mobile) | |
| def videos = [] | |
| topTracks?.each {trackName -> | |
| def trackVideo = videoResults?.find { video -> | |
| video?.title?.toLowerCase().contains(trackName.toLowerCase()) | |
| } | |
| if (trackVideo) videos << trackVideo | |
| } | |
| renderJson videos as JSON | |
| } | |
| def saveFeatured(List savedArtists) { | |
| if(savedArtists.size() < 4) { | |
| return render(status: HTTP_BAD_REQUEST) | |
| } | |
| try{ | |
| featuredArtistsService.saveFeaturedArtists(savedArtists) | |
| return render(status: HTTP_OK) | |
| } | |
| catch(ArtistUnknownException e) { | |
| return render(status: HTTP_BAD_REQUEST) | |
| } | |
| } | |
| def featured(){ | |
| def result = featuredArtistsService.getFeaturedArtists() | |
| render() { result } | |
| } | |
| private renderJson(obj) { | |
| if (params.callback) { | |
| // Render as JSON-P. | |
| render text: "$params.callback($obj)", contentType: 'application/javascript' | |
| } else { | |
| render obj | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.fdvs.bandwdth | |
| import grails.plugin.spock.ControllerSpec | |
| import grails.test.mixin.TestFor | |
| import static java.net.HttpURLConnection.* | |
| @TestFor(ArtistController) | |
| class ArtistControllerSpec extends ControllerSpec { | |
| def "searchYoutube should return at most one video per track"() { | |
| when: | |
| def youTubeService = mockFor(YouTubeService, true) | |
| youTubeService.demand.searchVideos() { String id, def mobile -> | |
| [ | |
| "U2 - With Or Without You", | |
| "u2- live - with or without you", | |
| "pride - u2", | |
| "U2 - Beautiful Day", | |
| "U2 - One - Anton Corbjin Version", | |
| "U2 - One", | |
| "U2 Where The Streets Have No Name Live At Slane Castle", | |
| "U2 - Where The Streets Have No Name" | |
| ].collect { [title: it] } | |
| } | |
| def lastFMService = mockFor(LastFMService, true) | |
| lastFMService.demand.topTracks() { String id -> | |
| [ | |
| "With Or Without You", | |
| "Beautiful Day", | |
| "One", | |
| "I still haven't found what I'm looking for", | |
| "Where The Streets Have No Name" | |
| ] | |
| } | |
| controller.lastFMService = lastFMService.createMock() | |
| controller.youTubeService = youTubeService.createMock() | |
| controller.searchYoutube("u2") | |
| then: | |
| mockResponse.status == 200 | |
| def responseData = mockResponse.json | |
| responseData.size() == 4 | |
| [ | |
| "U2 - With Or Without You", | |
| "U2 - Beautiful Day", | |
| "U2 - One - Anton Corbjin Version", | |
| "U2 Where The Streets Have No Name Live At Slane Castle" | |
| ].each { | |
| assert it in responseData*.title | |
| } | |
| } | |
| def "searchYoutube for an unkown artist should return an empty list"() { | |
| when: | |
| def unknownId = "theUnknownArtist123" | |
| def youTubeService = mockFor(YouTubeService) | |
| youTubeService.demand.searchVideos() { String id, def mobile -> | |
| [ | |
| "Some video", | |
| "Some other video", | |
| "Video by $unknownId" | |
| ].collect { [title: it] } | |
| } | |
| def lastFMService = mockFor(LastFMService) | |
| lastFMService.demand.topTracks() { String id -> null } | |
| controller.youTubeService = youTubeService.createMock() | |
| controller.lastFMService = lastFMService.createMock() | |
| controller.searchYoutube(unknownId) | |
| then: | |
| mockResponse.status == 200 | |
| mockResponse.json.empty | |
| } | |
| def "featured should return all artists' info"() { | |
| setup: | |
| def featuredArtistsService = mockFor(FeaturedArtistsService) | |
| featuredArtistsService.demand.getFeaturedArtists() { stubFeaturedArtists } | |
| controller.featuredArtistsService = featuredArtistsService.createMock() | |
| when: | |
| controller.featured() | |
| then: | |
| mockResponse.status == HTTP_OK | |
| def responseData = mockResponse.json //aca es donde rompe | |
| responseData.size() == stubFeaturedArtists.size() | |
| ['name', 'thumbnail', 'image', 'bio', 'summary'].every { propertyName -> | |
| responseData.every { artist -> | |
| artist.properties.containsKey(propertyName) | |
| } | |
| } | |
| } | |
| def "saveFeatured should fail if less than 4 artists try to be saved"() { | |
| when: | |
| controller.saveFeatured(savedArtists) | |
| then: | |
| mockResponse.status == HTTP_BAD_REQUEST | |
| // TODO Specify error code or message | |
| where: | |
| savedArtists << [[], ['The Beatles'], ['The Beatles', 'The Rolling Stones', 'The Stooges']] | |
| } | |
| def "saveFeatured should fail if some artists don't exist"() { | |
| setup: | |
| def featuredArtistsService = mockFor(FeaturedArtistsService) | |
| featuredArtistsService.demand.saveFeaturedArtists() { throw new ArtistUnknownException() } | |
| controller.featuredArtistsService = featuredArtistsService.createMock() | |
| when: | |
| controller.saveFeatured([ | |
| 'The Beatles', | |
| 'NonexistentArtist01', | |
| 'The Rolling Stones', | |
| 'Foo Fighters' | |
| ]) | |
| then: | |
| mockResponse.status == HTTP_BAD_REQUEST | |
| // TODO Specify error code or message | |
| } | |
| def "saveFeatured should succeed when saving 4 or more well known artists"() { | |
| setup: | |
| def savedNames | |
| def featuredArtistsService = mockFor(FeaturedArtistsService) | |
| featuredArtistsService.demand.saveFeaturedArtists() { artistNames -> | |
| savedNames = artistNames | |
| } | |
| controller.featuredArtistsService = featuredArtistsService.createMock() | |
| when: | |
| controller.saveFeatured(stubFeaturedArtists*.name) | |
| then: | |
| mockResponse.status == HTTP_OK | |
| savedNames.containsAll(stubFeaturedArtists*.name) | |
| } | |
| def stubFeaturedArtists = [ | |
| [ | |
| name: "The Beatles", | |
| thumbnail: "http://userserve-ak.last.fm/serve/34/2245544.jpg", | |
| image: "http://userserve-ak.last.fm/serve/500/2245544/The+Beatles.jpg", | |
| bio: "The Beatles</strong> were an iconic rock group from Liverpool, ...", | |
| summary: "The Beatles were an iconic rock group from Liverpool, ..." | |
| ], [ | |
| name: "The Rolling Stones", | |
| thumbnail: "http://userserve-ak.last.fm/serve/34/207811.jpg", | |
| image: "http://userserve-ak.last.fm/serve/500/207811/The+Rolling+Stones.jpg", | |
| bio: "The Rolling Stones are a band which formed in London, ...", | |
| summary: "The Rolling Stones..." | |
| ], [ | |
| name: "U2", | |
| thumbnail: "http://userserve-ak.last.fm/serve/34/380317.jpg", | |
| image: "http://userserve-ak.last.fm/serve/_/380317/U2.jpg", | |
| bio: "U2 is an Irish rock band which formed in 1976 in Dublin, Ireland ...", | |
| summary: "U2 is an Irish..." | |
| ], [ | |
| name: "The Beach Boys", | |
| thumbnail: "http://userserve-ak.last.fm/serve/34/5685767.jpg", | |
| image: "http://userserve-ak.last.fm/serve/_/5685767/The+Beach+Boys.jpg", | |
| bio: "The Beach Boys are an iconic pop and rock music group...", | |
| summary: "The Beach Boys are..." | |
| ], [ | |
| name: "Oasis", | |
| thumbnail: "http://userserve-ak.last.fm/serve/34/129563.jpg", | |
| image: "http://userserve-ak.last.fm/serve/500/129563/Oasis.jpg", | |
| bio: "There are multiple artists who have used the name Oasis...", | |
| summary: "There are multiple artists..." | |
| ] | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment