Advertisement
Guest User

Untitled

a guest
Nov 19th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. package org.arquillian.cube.toxiproxy;
  2.  
  3. import feign.gson.GsonDecoder;
  4. import feign.gson.GsonEncoder;
  5.  
  6. public interface ToxiProxy {
  7.  
  8. @feign.RequestLine("GET /proxies")
  9. Proxy[] getProxies();
  10.  
  11. @feign.RequestLine("POST /proxies")
  12. void createProxy(Proxy proxy);
  13.  
  14. @feign.RequestLine("GET /proxies/{proxy}")
  15. Proxy getProxy(@feign.Param("proxy") String name);
  16.  
  17. @feign.RequestLine("POST /proxies/{proxy}")
  18. Proxy updateProxy(@feign.Param("proxy") String name, Proxy proxy);
  19.  
  20. @feign.RequestLine("DELETE /proxies/{proxy}")
  21. Proxy removeProxy(@feign.Param("proxy") String name);
  22.  
  23. @feign.RequestLine("GET /proxies/{proxy}/upstream/toxics")
  24. Toxic getUpstreamToxic(@feign.Param("proxy") String name);
  25.  
  26. @feign.RequestLine("POST /proxies/{proxy}/upstream/toxics/{toxic}")
  27. void updateUpstreamToxic(@feign.Param("proxy") String name, @feign.Param("toxic") String toxic, ToxicType data);
  28.  
  29. @feign.RequestLine("GET /proxies/{proxy}/downstream/toxics")
  30. Toxic getDownstreamToxic(@feign.Param("proxy") String name);
  31.  
  32. @feign.RequestLine("POST /proxies/{proxy}/downstream/toxics/{toxic}")
  33. void updateDownstreamToxic(@feign.Param("proxy") String name, @feign.Param("toxic") String toxic, ToxicType data);
  34.  
  35. @feign.RequestLine("GET /reset")
  36. void reset();
  37.  
  38. public class Proxy {
  39. private String name;
  40. private String listen;
  41. private String upstream;
  42. private boolean enabled;
  43.  
  44. private Toxic upstream_toxics;
  45. private Toxic downstream_toxics;
  46.  
  47. protected Proxy() {}
  48.  
  49. public Proxy(String name, String listen, String upstream) {
  50. this.name = name;
  51. this.listen = listen;
  52. this.upstream = upstream;
  53. this.enabled = true;
  54. }
  55.  
  56. public Proxy addUpstreamToxic(ToxicType type) {
  57. if(upstream_toxics == null) {
  58. upstream_toxics = new Toxic();
  59. }
  60. if(type instanceof Latency) {
  61. upstream_toxics.latency = (Latency) type;
  62. }
  63. else if(type instanceof SlowClose) {
  64. upstream_toxics.slow_close = (SlowClose) type;
  65. }
  66. else if(type instanceof Timeout) {
  67. upstream_toxics.timeout = (Timeout) type;
  68. }
  69.  
  70. return this;
  71. }
  72.  
  73. public Proxy addDownstreamToxic(ToxicType type) {
  74. if(downstream_toxics == null) {
  75. downstream_toxics = new Toxic();
  76. }
  77. if(type instanceof Latency) {
  78. downstream_toxics.latency = (Latency) type;
  79. }
  80. else if(type instanceof SlowClose) {
  81. downstream_toxics.slow_close = (SlowClose) type;
  82. }
  83. else if(type instanceof Timeout) {
  84. downstream_toxics.timeout = (Timeout) type;
  85. }
  86.  
  87. return this;
  88. }
  89. }
  90.  
  91. public class Toxic {
  92. private Latency latency;
  93. private SlowClose slow_close;
  94. private Timeout timeout;
  95. }
  96.  
  97. public class ToxicType {
  98. private boolean enabled;
  99. }
  100.  
  101. public class Latency extends ToxicType {
  102. private int latency;
  103. private int jitter;
  104. }
  105.  
  106. public class SlowClose extends ToxicType {
  107. private int delay;
  108. }
  109.  
  110. public class Timeout extends ToxicType {
  111. private int timeout;
  112. }
  113.  
  114. public static class Builder {
  115.  
  116. public static ToxiProxy create(String url) {
  117. return feign.Feign.builder()
  118. .logger(new feign.Logger.JavaLogger())
  119. .logLevel(feign.Logger.Level.FULL)
  120. .decoder(new GsonDecoder())
  121. .encoder(new GsonEncoder())
  122. .target(ToxiProxy.class, url);
  123. }
  124. }
  125.  
  126. public static class ProxyExpander implements feign.Param.Expander {
  127.  
  128. public String expand(Object value) {
  129. if(!Proxy.class.isInstance(value)) {
  130. throw new IllegalArgumentException("Object of type " + value.getClass() + " is not of type " + Proxy.class);
  131. }
  132. return ((Proxy)value).name;
  133. }
  134.  
  135. }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement