Skip to content

Commit 1174e55

Browse files
committed
TS-38628 First Agent Tests
1 parent 62921eb commit 1174e55

6 files changed

Lines changed: 248 additions & 249 deletions

File tree

agent/src/test/java/com/teamscale/jacoco/agent/AgentHttpServerTest.java

Lines changed: 0 additions & 137 deletions
This file was deleted.

agent/src/test/java/com/teamscale/jacoco/agent/convert/ConverterTest.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

agent/src/test/java/com/teamscale/jacoco/agent/logging/LoggingUtilsTest.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.teamscale.jacoco.agent
2+
3+
import com.teamscale.jacoco.agent.options.TestAgentOptionsBuilder
4+
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
5+
import okhttp3.MediaType.Companion.toMediaTypeOrNull
6+
import okhttp3.OkHttpClient
7+
import okhttp3.Request
8+
import okhttp3.RequestBody.Companion.toRequestBody
9+
import org.assertj.core.api.Assertions
10+
import org.junit.jupiter.api.AfterEach
11+
import org.junit.jupiter.api.BeforeEach
12+
import org.junit.jupiter.api.Test
13+
import java.net.URI
14+
15+
class AgentHttpServerTest {
16+
private var agent: Agent? = null
17+
private val baseUri: URI
18+
private val httpServerPort = 8081
19+
private val defaultCommitMessage = "Some Message"
20+
private val defaultPartition = "Some Partition"
21+
22+
init {
23+
baseUri = URI("http://localhost:$httpServerPort")
24+
}
25+
26+
/** Starts the http server to control the agent */
27+
@BeforeEach
28+
@Throws(Exception::class)
29+
fun setup() {
30+
val options = TestAgentOptionsBuilder()
31+
.withHttpServerPort(httpServerPort)
32+
.withTeamscaleMessage(defaultCommitMessage)
33+
.withTeamscalePartition(defaultPartition)
34+
.create()
35+
36+
agent = Agent(options, null)
37+
}
38+
39+
/** Stops the http server */
40+
@AfterEach
41+
fun teardown() {
42+
agent!!.stopServer()
43+
}
44+
45+
/** Test overwriting the commit message */
46+
@Test
47+
@Throws(Exception::class)
48+
fun testOverridingMessage() {
49+
val newMessage = "New Message"
50+
51+
putText("/message", newMessage)
52+
53+
val teamscaleServer = agent!!.options.teamscaleServer
54+
Assertions.assertThat(teamscaleServer.message).isEqualTo(newMessage)
55+
}
56+
57+
/** Test reading the commit message */
58+
@Test
59+
@Throws(Exception::class)
60+
fun testGettingMessage() {
61+
val receivedMessage = getText("/message")
62+
63+
Assertions.assertThat(receivedMessage).isEqualTo(defaultCommitMessage)
64+
}
65+
66+
/** Test overwriting the partition */
67+
@Test
68+
@Throws(Exception::class)
69+
fun testOverridingPartition() {
70+
val newPartition = "New Partition"
71+
72+
putText("/partition", newPartition)
73+
74+
val teamscaleServer = agent!!.options.teamscaleServer
75+
Assertions.assertThat(teamscaleServer.partition).isEqualTo(newPartition)
76+
}
77+
78+
/** Test reading the partition */
79+
@Test
80+
@Throws(Exception::class)
81+
fun testGettingPartition() {
82+
val receivedPartition = getText("/partition")
83+
84+
Assertions.assertThat(receivedPartition).isEqualTo(defaultPartition)
85+
}
86+
87+
/** Test reading the commit to which the agent will upload. */
88+
@Test
89+
@Throws(Exception::class)
90+
fun testGettingCommit() {
91+
var receivedCommit = getText("/commit")
92+
Assertions.assertThat(receivedCommit).isEqualTo("{\"type\":\"REVISION\",\"value\":null}")
93+
receivedCommit = getJson("/commit")
94+
Assertions.assertThat(receivedCommit).isEqualTo("{\"type\":\"REVISION\",\"value\":null}")
95+
}
96+
97+
/** Test reading the revision to which the agent will upload. */
98+
@Test
99+
@Throws(Exception::class)
100+
fun testGettingRevision() {
101+
var receivedRevision = getText("/revision")
102+
Assertions.assertThat(receivedRevision).isEqualTo("{\"type\":\"REVISION\",\"value\":null}")
103+
receivedRevision = getJson("/revision")
104+
Assertions.assertThat(receivedRevision).isEqualTo("{\"type\":\"REVISION\",\"value\":null}")
105+
}
106+
107+
@Throws(Exception::class)
108+
private fun putText(endpointPath: String, newValue: String) {
109+
val client = OkHttpClient()
110+
val textPlainMediaType = "text/plain; charset=utf-8".toMediaTypeOrNull()
111+
val endpointUrl = "$baseUri$endpointPath".toHttpUrlOrNull()
112+
val content = newValue.toByteArray()
113+
val request = Request.Builder()
114+
.url(endpointUrl!!)
115+
.method("PUT", content.toRequestBody(textPlainMediaType, 0, content.size))
116+
.build()
117+
client.newCall(request).execute()
118+
}
119+
120+
@Throws(Exception::class)
121+
private fun getText(endpointPath: String): String {
122+
val client = OkHttpClient()
123+
val endpointUrl = "$baseUri$endpointPath".toHttpUrlOrNull()
124+
val request = Request.Builder()
125+
.url(endpointUrl!!)
126+
.build()
127+
val response = client.newCall(request).execute()
128+
return response.body.string()
129+
}
130+
131+
@Throws(Exception::class)
132+
private fun getJson(endpointPath: String): String {
133+
val client = OkHttpClient()
134+
val endpointUrl = "$baseUri$endpointPath".toHttpUrlOrNull()
135+
val request = Request.Builder()
136+
.url(endpointUrl!!).header("Accept", javax.ws.rs.core.MediaType.APPLICATION_JSON)
137+
.build()
138+
val response = client.newCall(request).execute()
139+
return response.body.string()
140+
}
141+
}

0 commit comments

Comments
 (0)