Last active
July 18, 2017 19:15
-
-
Save carlosmmelo/f22ffdd6294ab693508514544b0bca6a to your computer and use it in GitHub Desktop.
Pact Sample for Spring with Grails app
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.myapp | |
import au.com.dius.pact.provider.junit.Provider | |
import au.com.dius.pact.provider.junit.RestPactRunner | |
import au.com.dius.pact.provider.junit.loader.PactUrl | |
import au.com.dius.pact.provider.junit.target.HttpTarget | |
import au.com.dius.pact.provider.junit.target.Target | |
import au.com.dius.pact.provider.junit.target.TestTarget | |
import com.github.restdriver.clientdriver.ClientDriverRule | |
import com.healthreveal.rdbms.odsdb.myappschema.Organization | |
import com.healthreveal.rdbms.odsdb.myappschema.SystemUser | |
import grails.plugin.springsecurity.SpringSecurityService | |
import grails.test.mixin.Mock | |
import grails.test.mixin.TestFor | |
import grails.test.mixin.TestMixin | |
import grails.test.mixin.domain.DomainClassUnitTestMixin | |
import org.junit.Before | |
import org.junit.ClassRule | |
import org.junit.Test | |
import org.junit.runner.RunWith | |
import static com.github.restdriver.clientdriver.RestClientDriver.giveResponse | |
import static com.github.restdriver.clientdriver.RestClientDriver.onRequestTo | |
import static org.mockito.ArgumentMatchers.any | |
import static org.mockito.ArgumentMatchers.anyObject | |
import static org.mockito.Mockito.mock | |
import static org.mockito.Mockito.when | |
@RunWith(RestPactRunner.class) | |
@Provider("MyProvider") | |
@PactUrl(urls = ["http://MyPactBrokerServiceURL/pacts/provider/MyProvider/consumer/MyConsumer/latest"]) | |
@TestMixin(DomainClassUnitTestMixin) | |
@TestFor(MyController) | |
@Mock([SystemUser]) | |
class RevealReviewPactSpec { | |
@ClassRule | |
//Rule will be applied once: before/after whole contract test suite | |
public static final ClientDriverRule embeddedService = new ClientDriverRule(5051) | |
@TestTarget | |
public final Target target = new HttpTarget(5051) // where actual pact interactions will run | |
@Before | |
@Test | |
//Method will be run before each test of interaction | |
public void before() { | |
getUserLoginData() | |
} | |
def getUserLoginData() { | |
new SystemUser( | |
username: "tester", | |
password: "password" | |
).save(flush: true) | |
def org1 = new Organization( | |
"name": "test org", "oid": "12345", "active": true | |
) | |
controller.springSecurityService = mock(SpringSecurityService) | |
when(controller.springSecurityService.getPrincipal()).thenReturn([username: 'tester']) | |
controller.applicationUserService = mock(ApplicationUserService) | |
when(controller.applicationUserService.confirmIsUserRole(any(SystemUser.class), anyObject())).thenReturn(true) | |
controller.organizationService = mock(OrganizationService) | |
when(controller.organizationService.getAllOrgs()).thenReturn([org1]) | |
controller.getBasePatientSearch() | |
embeddedService.addExpectation( | |
onRequestTo("/api/v1/review/patient/base"), giveResponse(response.getContentAsString(), "application/json")) | |
} | |
} |
@uglyog it is not going to work if I remove line 66, but since there is no plugin for grails via pact where I could mock my service to be running I needed to do it this way to avoid having a real environment up where I could hit.
So Grails uses this UrlMappings
class where you define the url and the method from the controller to be called when you hit that url path like:
package com.MyApp
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"{
constraints {
// apply constraints here
}
}
"/api/v1/review/patient/base"(controller:"PatientAPI", action:"getBasePatientSearch")
Oh, cool. So this giveResponse(response.getContentAsString(), "application/json")
will return the actual response from the controller?
@uglyog correct the response is coming from the actual controller like if it was called from the app running
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think this is actually testing via your controller. If you commented out line 66, will it still pass?