vendredi 24 mai 2019

Issue testing controller with spring mvc test (code status 404)

I am working with spring rest docs now and I am facing some issues to test my controller, basically I am getting a 404 issue:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /v1/browse/players/
       Parameters = {swid=[asudyasd-asdasd-asdasd], uid=[s:20~l:23], seeAll=[true], lang=[es], region=[us], version=[23], appName=[espn], platform=[espn]}
          Headers = {}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /v1/browse/players/
       Parameters = {swid=[asudyasd-asdasd-asdasd], uid=[s:20~l:23], seeAll=[true], lang=[es], region=[us], version=[23], appName=[espn], platform=[espn]}
          Headers = {}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 404
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :200
Actual   :404

My Controller looks like:

package com.espn.csemobile.espnapp.controllers

import com.espn.api.platform.model.*
import com.espn.csemobile.espnapp.context.*
import com.espn.csemobile.espnapp.context.common.RequestBasedSWIDContext
import com.espn.csemobile.espnapp.context.common.RequestBasedSeeAllContext
import com.espn.csemobile.espnapp.context.common.RequestBasedUIDContext
import com.espn.csemobile.espnapp.models.browseplayer.BrowsePlayerResponse
import com.espn.csemobile.espnapp.services.browseplayer.BrowsePlayerServiceRepresentable
import com.espn.csemobile.espnapp.services.browseplayer.contexts.RequestBrowsePlayerContext
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import rx.Single

@Controller
@ProductApi(
        id = "v1_browse_player",
        title = "Browse Player (v1)",
        description = "")
class BrowsePlayerController {

    @Autowired
    lateinit var browsePlayerService: BrowsePlayerServiceRepresentable

    @GetRequest(
            path = "/v1/browse/players",
            timeToLive = 300,
            queries = [
                QueryParameter(name = "swid", required = true),
                QueryParameter(name = "uid"),
                QueryParameter(name = "seeAll", type = java.lang.Boolean::class),
                QueryParameter(name = "lang", required = true),
                QueryParameter(name = "region", required = true),
                QueryParameter(name = "version", required = true, type = Integer::class),
                QueryParameter(name = "appName", required = true),
                QueryParameter(name = "platform", required = true)
            ]
    )
    fun processBrowsePlayerRequest(transaction: Transaction, apiRequest: ApiRequest): Single<BrowsePlayerResponse?> {
        val applicationContext = RequestBasedApplicationContext(apiRequest)
        val standardContext = RequestBasedStandardContext(
                RequestBasedVersionContext(apiRequest),
                applicationContext,
                RequestBasedEditionContext(apiRequest, applicationContext),
                RequestBasedPlatformContext(apiRequest),
                transaction
        )
        val swidContext = RequestBasedSWIDContext(apiRequest)
        val uidContext = if (checkUIDPresent(apiRequest)) RequestBasedUIDContext(apiRequest) else null
        val seeAllContext = RequestBasedSeeAllContext(apiRequest)
        val requestBrowsePlayerContext = RequestBrowsePlayerContext(standardContext, swidContext, uidContext, seeAllContext, apiRequest)
        return browsePlayerService.getEntitiesBrowse(requestBrowsePlayerContext)
    }

    private fun checkUIDPresent(apiRequest: ApiRequest): Boolean =
            apiRequest.parameters["uid"] != null
}

And the code of my test is the next:

package com.espn.csemobile.espnapp.controllers

import com.espn.api.platform.model.ApiRequest
import com.espn.csemobile.espnapp.context.StandardContext
import com.espn.csemobile.espnapp.context.common.SWIDContext
import com.espn.csemobile.espnapp.context.common.SeeAllContext
import com.espn.csemobile.espnapp.context.common.UIDContext
import com.espn.csemobile.espnapp.models.UID
import com.espn.csemobile.espnapp.models.browseplayer.BrowsePlayerAction
import com.espn.csemobile.espnapp.models.browseplayer.BrowsePlayerResponse
import com.espn.csemobile.espnapp.models.browseplayer.EntityBrowse
import com.espn.csemobile.espnapp.models.browseplayer.SectionType
import com.espn.csemobile.espnapp.services.browseplayer.BrowsePlayerServiceRepresentable
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import rx.Single
import com.espn.csemobile.espnapp.models.browseplayer.*
import com.espn.csemobile.espnapp.services.browseplayer.contexts.RequestBrowsePlayerContext

@RunWith(SpringRunner::class)
@WebMvcTest(BrowsePlayerController::class, secure = false)
@AutoConfigureRestDocs(outputDir = "target/snippets")
class BrowsePlayerControllerTest {

    @Autowired
    private val mockMvc: MockMvc? = null
    @MockBean
    private lateinit var browsePlayerServiceRepresentable: BrowsePlayerServiceRepresentable
    @MockBean
    private lateinit var standardContext: StandardContext
    @MockBean
    private lateinit var sWIDContext: SWIDContext
    @MockBean
    private lateinit var uIDContext: UIDContext
    @MockBean
    private lateinit var seeAllContext: SeeAllContext
    @MockBean
    private lateinit var apiRequest: ApiRequest

    @Test
    @Throws(Exception::class)
    fun shouldReturnDefaultMessage() {
        val contextRequest = RequestBrowsePlayerContext(this.standardContext, this.sWIDContext, this.uIDContext, this.seeAllContext, apiRequest)
        `when`(browsePlayerServiceRepresentable.getEntitiesBrowse(contextRequest)).thenReturn(Single.just(presentBrowsePlayers()))
        this.mockMvc!!.perform(get("/v1/browse/players/").param("swid", "asudyasd-asdasd-asdasd")
                .param("uid", "s:20~l:23").param("seeAll", true.toString()).param("lang", "es")
                .param("region", "us").param("version", "23").param("appName", "espn")
                .param("platform", "espn"))
                .andDo(print()).andExpect(status().isOk)
    }


    private fun  presentBrowsePlayers(): BrowsePlayerResponse {
        var sections = arrayListOf<Node>()
        val playersResponse = buildMockSuggestedPlayers()
        val nodePlayers =  Node(null, SectionType.PLAYERS, playersResponse)
        sections.add(nodePlayers)
        val entitiesResponse = buildMockEntitiesBrowse()
        val nodeEntities =  Node(null, SectionType.ENTITIES, entitiesResponse)
        sections.add(nodeEntities)
        return  BrowsePlayerResponse(presentTopHeader(), buildAnalytics(), sections)
    }

    private fun buildMockSuggestedPlayers(): List<SuggestedPlayer> {
        val items = arrayListOf<SuggestedPlayer>()
        items.add(SuggestedPlayer(
                "http://i.espncdn.com/i/players/130x180/45843.jpg",
                "Lionel Messi",
                "SOCCER",
                "dc5f8d51332b0ab2b4b0c97efdc624e0",
                buildAnalyticsPlayer("s:600~t:83", "s:600~l:740", "s:600", SuggestedType.TEAM)
        ))
        items.add(SuggestedPlayer(
                "http://i.espncdn.com/i/players/130x180/22774.jpg",
                "Cristiano Ronaldo",
                "SOCCER",
                "19e162c06bf5d817e9f981bf96001ee2",
                buildAnalyticsPlayer("s:600~t:111", "s:600~l:730", "s:600", SuggestedType.LEAGUE)
        ))
        return items
    }

    private fun  buildAnalyticsPlayer( teamUid:String?,  leagueId :String,  sportUid :String,  suggestedType:SuggestedType):PlayerAnalytics {
        return  PlayerAnalytics(teamUid, leagueId, sportUid, suggestedType)
    }

     private fun presentTopHeader():TopBrowsePlayerHeader {
        val  searchURL = "http://someurl"
        return  TopBrowsePlayerHeader("Player Browse", searchURL)
    }

     private fun buildAnalytics():Analytics {
        return  Analytics(null, null, null, null, null, PageType.ALL_PLAYERS, false)
    }

    private fun buildMockEntitiesBrowse(): List<EntityBrowse> {
        var items = arrayListOf<EntityBrowse>()
        items.add(buildSport("NFL", "s:20~l:28", "http://a.espncdn.com/i/teamlogos/leagues/500/nfl.png"))
        items.add(buildSport("NCAA Football", "s:20~l:23", "http://a.espncdn.com/redesign/assets/img/icons/ESPN-icon-football-college.png"))
        items.add(buildSport("NBA", "s:40~l:46", "http://a.espncdn.com/i/teamlogos/leagues/500/nba.png"))
        items.add(buildSport("NCAA Basketball", "s:40~l:41", "http://a.espncdn.com/redesign/assets/img/icons/ESPN-icon-basketball.png"))
        return items
    }

    private fun buildSport(label: String, uid: String, image: String): EntityBrowse {
        val type = "internalLink"
        val url = "sportscenter://x-callback-url/showPlayerBrowse?uid=$uid"
        val action = BrowsePlayerAction(type, url)
        return  EntityBrowse(label, UID.create(uid), action, image)
    }
}

I had tested also using the path: /v1/browse/players, but it continues failing.

I think it's something related with the way in which I am referencing the path of my controller in the test: /v1/browse/players/". Or maybe with the params?

I am not pretty sure about whay is happenig so far.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire