Skip to content

Commit

Permalink
fix: Add mock server utility to get the last request. Use this utilit…
Browse files Browse the repository at this point in the history
…y to decouple tests from internals.
  • Loading branch information
iocanel committed Mar 15, 2019
1 parent ab8ddcf commit d08f4bd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.fabric8.mockwebserver.crud.CrudDispatcher;
import io.fabric8.mockwebserver.dsl.MockServerExpectation;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
Expand Down Expand Up @@ -91,4 +92,13 @@ public void expectAndReturnAsString(String method, String path, int code, String
public MockWebServer getMockServer() {
return mock.getServer();
}

public RecordedRequest getLastRequest() throws InterruptedException {
int count = mock.getServer().getRequestCount();
RecordedRequest request = null;
while (count-- > 0) {
request = mock.getServer().takeRequest();
}
return request;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@Ignore
public class CreateOrReplaceResourceTest {

@Rule
public KubernetesServer server = new KubernetesServer();

Expand All @@ -49,13 +49,9 @@ public void testResourceReplace() throws Exception {
.withNewMetadata().withResourceVersion("12345").and().build()).once();

KubernetesClient client = server.getClient();
assertNotNull(client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace());

server.getMockServer().takeRequest();
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("12345", requestPod.getMetadata().getResourceVersion());
Pod pod = client.resource(new PodBuilder().withNewMetadata().withName("pod123").and().withNewSpec().and().build()).createOrReplace();
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());
}

@Test
Expand Down Expand Up @@ -133,11 +129,8 @@ public void testResourceReplaceFromLoad() throws Exception {
Pod pod = (Pod) result.get(0);
assertEquals("12345", pod.getMetadata().getResourceVersion());

RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());
server.getMockServer().takeRequest();

request = server.getMockServer().takeRequest();
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
Expand All @@ -154,10 +147,7 @@ public void testCreateFromLoad() throws Exception {
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());

RecordedRequest request = server.getMockServer().takeRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());

request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
Expand All @@ -174,10 +164,9 @@ public void testReplaceFromLoad() throws Exception {
assertNotNull(pod);
assertEquals("12345", pod.getMetadata().getResourceVersion());

RecordedRequest request = server.getMockServer().takeRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());

request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals("/api/v1/namespaces/test/pods/nginx", request.getPath());
Pod requestPod = new ObjectMapper().readerFor(Pod.class).readValue(request.getBody().inputStream());
assertEquals("nginx", requestPod.getMetadata().getName());
}
Expand All @@ -196,8 +185,7 @@ public void testReplaceWithoutLock() throws Exception {
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());

server.getMockServer().takeRequest(); // ignore the first request
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(request.getBody().inputStream());
assertEquals("1000", replacedMap.getMetadata().getResourceVersion());
}
Expand All @@ -214,7 +202,7 @@ public void testReplaceWithLock() throws Exception {
assertNotNull(map);
assertEquals("1001", map.getMetadata().getResourceVersion());

RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
ConfigMap replacedMap = new ObjectMapper().readerFor(ConfigMap.class).readValue(request.getBody().inputStream());
assertEquals("900", replacedMap.getMetadata().getResourceVersion());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void testCreateWithOnlySubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

RecordedRequest request = server.getMockServer().takeRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().readByteArray()));
}

@Test
Expand All @@ -75,7 +75,7 @@ public void testCreateWithUserNamesAndGroupsAndNoSubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand All @@ -95,7 +95,7 @@ public void testCreateWithUserNamesAndGroupsAndOverwriteSubjects() throws Except
);
assertEquals(expectedOpenshiftRoleBinding, response);

RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand All @@ -117,9 +117,7 @@ public void testReplaceWithOnlySubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand All @@ -139,9 +137,8 @@ public void testReplaceWithUserNamesAndGroupsAndNoSubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();

RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand All @@ -162,9 +159,7 @@ public void testReplaceWithUserNamesAndGroupsAndOverwriteSubjects() throws Excep
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand All @@ -186,9 +181,7 @@ public void testPatchWithOnlySubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(
"[{\"op\":\"replace\",\"path\":\"/userNames/0\",\"value\":\"testuser1\"},{\"op\":\"add\",\"path\":\"/userNames/1\",\"value\":\"testuser2\"},{\"op\":\"add\",\"path\":\"/userNames/2\",\"value\":\"system:serviceaccount:test:svcacct\"},{\"op\":\"add\",\"path\":\"/metadata\",\"value\":{}},{\"op\":\"add\",\"path\":\"/groupNames\",\"value\":[\"testgroup\"]},{\"op\":\"add\",\"path\":\"/subjects\",\"value\":[{\"kind\":\"User\",\"name\":\"testuser1\"},{\"kind\":\"User\",\"name\":\"testuser2\"},{\"kind\":\"ServiceAccount\",\"name\":\"svcacct\",\"namespace\":\"test\"},{\"kind\":\"Group\",\"name\":\"testgroup\"}]}]",
request.getBody().readUtf8()
Expand All @@ -211,9 +204,7 @@ public void testPatchWithUserNamesAndGroupsAndNoSubjects() throws Exception {
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(
"[{\"op\":\"replace\",\"path\":\"/userNames/0\",\"value\":\"testuser1\"},{\"op\":\"add\",\"path\":\"/userNames/1\",\"value\":\"testuser2\"},{\"op\":\"add\",\"path\":\"/userNames/2\",\"value\":\"system:serviceaccount:test:svcacct\"},{\"op\":\"add\",\"path\":\"/metadata\",\"value\":{}},{\"op\":\"add\",\"path\":\"/groupNames\",\"value\":[\"testgroup\"]},{\"op\":\"add\",\"path\":\"/subjects\",\"value\":[{\"kind\":\"User\",\"name\":\"testuser1\"},{\"kind\":\"User\",\"name\":\"testuser2\"},{\"kind\":\"ServiceAccount\",\"name\":\"svcacct\",\"namespace\":\"test\"},{\"kind\":\"Group\",\"name\":\"testgroup\"}]}]",
request.getBody().readUtf8()
Expand All @@ -237,9 +228,7 @@ public void testPatchWithUserNamesAndGroupsAndOverwriteSubjects() throws Excepti
);
assertEquals(expectedOpenshiftRoleBinding, response);

// Initial GET
server.getMockServer().takeRequest();
RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(
"[{\"op\":\"replace\",\"path\":\"/userNames/0\",\"value\":\"testuser1\"},{\"op\":\"add\",\"path\":\"/userNames/1\",\"value\":\"testuser2\"},{\"op\":\"add\",\"path\":\"/userNames/2\",\"value\":\"system:serviceaccount:test:svcacct\"},{\"op\":\"add\",\"path\":\"/metadata\",\"value\":{}},{\"op\":\"add\",\"path\":\"/groupNames\",\"value\":[\"testgroup\"]},{\"op\":\"add\",\"path\":\"/subjects\",\"value\":[{\"kind\":\"User\",\"name\":\"testuser1\"},{\"kind\":\"User\",\"name\":\"testuser2\"},{\"kind\":\"ServiceAccount\",\"name\":\"svcacct\",\"namespace\":\"test\"},{\"kind\":\"Group\",\"name\":\"testgroup\"}]}]",
request.getBody().readUtf8()
Expand All @@ -261,7 +250,7 @@ public void testCreateInline() throws Exception {
.done();
assertEquals(expectedOpenshiftRoleBinding, response);

RecordedRequest request = server.getMockServer().takeRequest();
RecordedRequest request = server.getLastRequest();
assertEquals(expectedOpenshiftRoleBinding, new ObjectMapper().readerFor(OpenshiftRoleBinding.class).readValue(request.getBody().inputStream()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.fabric8.openshift.client.NamespacedOpenShiftClient;
import io.fabric8.openshift.client.OpenshiftAdapterSupport;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.rules.ExternalResource;

import java.util.HashMap;
Expand Down Expand Up @@ -90,4 +91,13 @@ public void expectAndReturnAsString(String path, int code, String body) {
public MockWebServer getMockServer() {
return mock.getServer();
}

public RecordedRequest getLastRequest() throws InterruptedException {
int count = mock.getServer().getRequestCount();
RecordedRequest request = null;
while (count-- > 0) {
request = mock.getServer().takeRequest();
}
return request;
}
}

0 comments on commit d08f4bd

Please sign in to comment.