SaganApiTests.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2019-2020 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.spring.gradle.convention.sagan;
  17. import okhttp3.mockwebserver.MockResponse;
  18. import okhttp3.mockwebserver.MockWebServer;
  19. import okhttp3.mockwebserver.RecordedRequest;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.gradle.sagan.Release;
  24. import org.springframework.gradle.sagan.SaganApi;
  25. import java.nio.charset.Charset;
  26. import java.util.concurrent.TimeUnit;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. public class SaganApiTests {
  29. private MockWebServer server;
  30. private SaganApi sagan;
  31. private String baseUrl;
  32. @BeforeEach
  33. public void setup() throws Exception {
  34. this.server = new MockWebServer();
  35. this.server.start();
  36. this.sagan = new SaganApi("mock-oauth-token");
  37. this.baseUrl = this.server.url("/api").toString();
  38. this.sagan.setBaseUrl(this.baseUrl);
  39. }
  40. @AfterEach
  41. public void cleanup() throws Exception {
  42. this.server.shutdown();
  43. }
  44. @Test
  45. public void createWhenValidThenNoException() throws Exception {
  46. this.server.enqueue(new MockResponse());
  47. Release release = new Release();
  48. release.setVersion("5.6.0-SNAPSHOT");
  49. release.setApiDocUrl("https://docs.spring.io/spring-security/site/docs/{version}/api/");
  50. release.setReferenceDocUrl("https://docs.spring.io/spring-security/reference/{version}/index.html");
  51. this.sagan.createReleaseForProject(release, "spring-security");
  52. RecordedRequest request = this.server.takeRequest(1, TimeUnit.SECONDS);
  53. assertThat(request.getRequestUrl().toString()).isEqualTo(this.baseUrl + "/projects/spring-security/releases");
  54. assertThat(request.getMethod()).isEqualToIgnoringCase("post");
  55. assertThat(request.getHeaders().get("Authorization")).isEqualTo("Basic bm90LXVzZWQ6bW9jay1vYXV0aC10b2tlbg==");
  56. assertThat(request.getBody().readString(Charset.defaultCharset())).isEqualToIgnoringWhitespace("{\n" +
  57. " \"version\":\"5.6.0-SNAPSHOT\",\n" +
  58. " \"current\":false,\n" +
  59. " \"referenceDocUrl\":\"https://docs.spring.io/spring-security/reference/{version}/index.html\",\n" +
  60. " \"apiDocUrl\":\"https://docs.spring.io/spring-security/site/docs/{version}/api/\"\n" +
  61. "}");
  62. }
  63. @Test
  64. public void deleteWhenValidThenNoException() throws Exception {
  65. this.server.enqueue(new MockResponse());
  66. this.sagan.deleteReleaseForProject("5.6.0-SNAPSHOT", "spring-security");
  67. RecordedRequest request = this.server.takeRequest(1, TimeUnit.SECONDS);
  68. assertThat(request.getRequestUrl().toString()).isEqualTo(this.baseUrl + "/projects/spring-security/releases/5.6.0-SNAPSHOT");
  69. assertThat(request.getMethod()).isEqualToIgnoringCase("delete");
  70. assertThat(request.getHeaders().get("Authorization")).isEqualTo("Basic bm90LXVzZWQ6bW9jay1vYXV0aC10b2tlbg==");
  71. assertThat(request.getBody().readString(Charset.defaultCharset())).isEmpty();
  72. }
  73. }