Skip to content

Commit

Permalink
Implement getCampuses()
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhandwell committed Jun 12, 2019
1 parent 349a5f7 commit f58cfa2
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}
dependencies {
implementation 'com.github.joelhandwell:populi-api-client:1.0.3'
implementation 'com.github.joelhandwell:populi-api-client:1.0.4'
}
```

Expand All @@ -32,6 +32,7 @@ fun clientWithAccessKey(){
.withAccessKey("7283dac8472d4e5d...").build()
println(populi.getDegrees())
println(populi.getUsers())
println(populi.getCampuses())
}
```

Expand Down Expand Up @@ -64,7 +65,7 @@ fun clientWithAccessKey(){
| AvailableRoles | get | [getAvailableRoles](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#getAvailableRoles) |<ul><li> - [ ] </li></ul> |
| Backup | download | [downloadBackup](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#downloadBackup) |<ul><li> - [ ] </li></ul> |
| Backup | request | [requestBackup](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#requestBackup) |<ul><li> - [ ] </li></ul> |
| Campuses | get | [getCampuses](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#getCampuses) |<ul><li> - [ ] </li></ul> |
| Campuses | get | [getCampuses](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#getCampuses) |<ul><li> - [x] </li></ul> |
| CampusFromStudent | delete | [deleteCampusFromStudent](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#deleteCampusFromStudent) |<ul><li> - [ ] </li></ul> |
| CampusToStudent | add | [addCampusToStudent](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#addCampusToStudent) |<ul><li> - [ ] </li></ul> |
| CommunicationPlanFromPerson | delete | [deleteCommunicationPlanFromPerson](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#deleteCommunicationPlanFromPerson) |<ul><li> - [ ] </li></ul> |
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ plugins {
}

group 'com.github.joelhandwell'
version '1.0.3'
version '1.0.4'

repositories {
jcenter()
Expand Down
9 changes: 8 additions & 1 deletion src/main/kotlin/com/github/joelhandwell/populi/client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,22 @@ class Populi(
*/
fun getUsers(): MutableList<User> = sendRequest(this.api.getUsers(accessKey)).person

/**
* Returns all campuses. [ref](https://support.populiweb.com/hc/en-us/articles/223798747-API-Reference#getCampuses)
*/
fun getCampuses(): MutableList<Campus> = sendRequest(this.api.getCampuses(accessKey)).campus

fun getRaw(task: String): String = this.api.getRaw(accessKey, task).execute().body().toString()
}

interface PopuliApi {
@FormUrlEncoded @POST(API_URI) fun requestAccessKey(@Field("username") username: String, @Field("password") password: String): Call<AccessKeyResponse>
@FormUrlEncoded @POST(API_URI) fun getDegrees(@Field(FIELD_ACCESS_KEY) accessKey: String, @Field(FIELD_TASK) task: String = "getDegrees"): Call<DegreeResponse>
@FormUrlEncoded @POST(API_URI) fun getUsers(@Field(FIELD_ACCESS_KEY) accessKey: String, @Field(FIELD_TASK) task: String = "getUsers"): Call<UserResponse>
@FormUrlEncoded @POST(API_URI) fun getCampuses(@Field(FIELD_ACCESS_KEY) accessKey: String, @Field(FIELD_TASK) task: String = "getCampuses"): Call<CampusResponse>

//for debug
//@FormUrlEncoded @POST(API_URI) fun getUsersRaw(@Field(FIELD_ACCESS_KEY) accessKey: String, @Field(FIELD_TASK) task: String = "getUsers"): Call<String>
@FormUrlEncoded @POST(API_URI) fun getRaw(@Field(FIELD_ACCESS_KEY) accessKey: String, @Field(FIELD_TASK) task: String): Call<String>
}

private const val API_URI = "api/"
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/com/github/joelhandwell/populi/model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ data class User(
@XmlRootElement(name = "response")
data class UserResponse(
var person: MutableList<User> = mutableListOf()
)

@XmlRootElement(name = "campus")
@XmlAccessorType(XmlAccessType.FIELD)
data class Campus(
var id: Int,
var name: String,
var status: String,
var city: String,
var state: String,
var zip: String,
var country: String,

@XmlElement(name = "is_primary")
var is_primary: Int
)

@XmlRootElement(name = "response")
data class CampusResponse(
var campus: MutableList<Campus> = mutableListOf()
)
66 changes: 66 additions & 0 deletions src/test/kotlin/com/github/joelhandwell/populi/CampusSpec.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.joelhandwell.populi

import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.io.StringWriter
import javax.xml.bind.JAXB
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

object CampusSpec : Spek({
describe("Campus") {
it("marshal to xml") {
val campusResponse = CampusResponse()
campusResponse.campus.addAll(mutableListOf(
Campus(1, "SF Campus", "ACTIVE", "San Francisco", "CA", "12345", "USA", 1),
Campus(2, "NY Campus", "ACTIVE", "New York", "NY", "67890", "USA", 0)
))
val sw = StringWriter()
JAXB.marshal(campusResponse, sw)
assertEquals(XML_HEADER + getCampusesXml.trim(), sw.toString().trim())
}

it("unmarshal from xml"){
val r = JAXB.unmarshal(getCampusesXml.reader(), CampusResponse::class.java)
assertCampuses(r.campus)
}
}
})

const val getCampusesXml = """
<response>
<campus>
<id>1</id>
<name>SF Campus</name>
<status>ACTIVE</status>
<city>San Francisco</city>
<state>CA</state>
<zip>12345</zip>
<country>USA</country>
<is_primary>1</is_primary>
</campus>
<campus>
<id>2</id>
<name>NY Campus</name>
<status>ACTIVE</status>
<city>New York</city>
<state>NY</state>
<zip>67890</zip>
<country>USA</country>
<is_primary>0</is_primary>
</campus>
</response>
"""

fun assertCampuses(campuses: MutableList<Campus>){
assertEquals(2, campuses.size)
val sf = campuses.firstOrNull { it.id == 1 }
assertNotNull(sf)
assertEquals("SF Campus", sf.name)
assertEquals("ACTIVE", sf.status)
assertEquals("San Francisco", sf.city)
assertEquals("CA", sf.state)
assertEquals("12345", sf.zip)
assertEquals("USA", sf.country)
assertEquals(1, sf.is_primary)
}
28 changes: 27 additions & 1 deletion src/test/kotlin/com/github/joelhandwell/populi/ClientSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import com.github.tomakehurst.wiremock.client.WireMock
import org.slf4j.LoggerFactory
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.nio.file.Paths
import java.util.*
import kotlin.test.assertNotNull

object ClientSpec : Spek({

describe("Client") {
(LoggerFactory.getLogger("org.eclipse.jetty") as ch.qos.logback.classic.Logger).level = Level.INFO
val wireMockServer = WireMockServer()
val populi = Populi.Builder().withBaseUrl("http://localhost:$WIREMOCK_PORT/").withAccessKey(TEST_API_ACCESS_KEY).build()
val populi =
Populi.Builder().withBaseUrl("http://localhost:$WIREMOCK_PORT/").withAccessKey(TEST_API_ACCESS_KEY).build()
beforeGroup { wireMockServer.start() }

it("creates api client with username and password") {
Expand Down Expand Up @@ -54,6 +57,29 @@ object ClientSpec : Spek({
assertUsers(populi.getUsers())
}

it("send request, receive response and parse it into Campuses") {
WireMock.stubFor(
WireMock.post("/api/").withRequestBody(WireMock.containing("access_key=$TEST_API_ACCESS_KEY&task=getCampuses"))
.willReturn(WireMock.aResponse().withBody(getCampusesXml))
)
assertCampuses(populi.getCampuses())
}

xit("real") {
val input = Paths.get("${System.getProperty("user.dir")}\\local.properties").toFile().inputStream()
val p = Properties()
p.load(input)

val real = Populi.Builder()
.withBaseUrl(p.getProperty("real.baseurl"))
.withUsername(p.getProperty("real.username"))
.withPassword(p.getProperty("real.password"))
.withDebugFlag(true)
.build()

println(real.getRaw("getCampuses"))
}

afterGroup { wireMockServer.stop() }
}
})

0 comments on commit f58cfa2

Please sign in to comment.