Commit 67ceb92e authored by Ronak Sakhuja's avatar Ronak Sakhuja Committed by Krishna Nikhil Vedurumudi
Browse files

Add circuitbreaker to all inter service communication

parent 37d143ad
......@@ -12,7 +12,7 @@ The following software have components provided under the terms of this license:
- Adapter: RxJava (from https://repo1.maven.org/maven2/com/squareup/retrofit2/adapter-rxjava)
- Apache Commons Codec (from https://commons.apache.org/proper/commons-codec/)
- Apache Commons Collections (from https://commons.apache.org/proper/commons-collections/)
- Apache Commons Lang (from http://commons.apache.org/proper/commons-lang/)
- Apache Commons Lang (from https://commons.apache.org/proper/commons-lang/)
- Apache Commons Logging (from http://commons.apache.org/proper/commons-logging/)
- Apache HttpAsyncClient (from http://hc.apache.org/httpcomponents-asyncclient)
- Apache HttpClient (from http://hc.apache.org/httpcomponents-client)
......@@ -26,10 +26,12 @@ The following software have components provided under the terms of this license:
- Asynchronous Http Client (from https://repo1.maven.org/maven2/org/asynchttpclient/async-http-client)
- Asynchronous Http Client Netty Utils (from https://repo1.maven.org/maven2/org/asynchttpclient/async-http-client-netty-utils)
- Bean Validation API (from http://beanvalidation.org)
- Brave Instrumentation: Http Adapters (from )
- Bean Validation API (from http://beanvalidation.org)
- Brave (from https://repo1.maven.org/maven2/io/zipkin/brave/brave)
- Brave Instrumentation: Http Adapters (from https://repo1.maven.org/maven2/io/zipkin/brave/brave-instrumentation-http)
- Brave instrumentation for Reactor Netty HTTP (from https://github.com/reactor/reactor-netty)
- Byte Buddy (without dependencies) (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy)
- Byte Buddy Java agent (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy-agent)
- Byte Buddy agent (from https://repo1.maven.org/maven2/net/bytebuddy/byte-buddy-agent)
- ClassMate (from http://github.com/cowtowncoder/java-classmate)
- Converter: Jackson (from https://repo1.maven.org/maven2/com/squareup/retrofit2/converter-jackson)
- Core functionality for the Reactor Netty library (from https://github.com/reactor/reactor-netty)
......@@ -42,27 +44,27 @@ The following software have components provided under the terms of this license:
- Guava: Google Core Libraries for Java (from https://repo1.maven.org/maven2/com/google/guava/guava)
- HPPC Collections (from https://repo1.maven.org/maven2/com/carrotsearch/hppc)
- HTTP functionality for the Reactor Netty library (from https://github.com/reactor/reactor-netty)
- Hibernate Validator Engine (from )
- Hibernate Validator Engine (from https://repo1.maven.org/maven2/org/hibernate/validator/hibernate-validator)
- IntelliJ IDEA Annotations (from http://www.jetbrains.org)
- J2ObjC Annotations (from https://github.com/google/j2objc/)
- JBoss Logging 3 (from http://www.jboss.org)
- JCIP Annotations under Apache License (from http://stephenc.github.com/jcip-annotations)
- JSON Small and Fast Parser (from https://repo1.maven.org/maven2/net/minidev/json-smart)
- JSON Web Token support for the JVM (from https://github.com/jwtk/jjwt.git)
- JSON Web Token support for the JVM (from https://github.com/jwtk/jjwt)
- JSR107 API and SPI (from https://github.com/jsr107/jsr107spec)
- Jackson dataformat: CBOR (from http://github.com/FasterXML/jackson-dataformats-binary)
- Jackson dataformat: Smile (from http://github.com/FasterXML/jackson-dataformats-binary)
- Jackson datatype: JSR310 (from https://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-jsr310)
- Jackson datatype: Joda (from https://github.com/FasterXML/jackson-datatype-joda)
- Jackson datatype: jdk8 (from https://repo1.maven.org/maven2/com/fasterxml/jackson/datatype/jackson-datatype-jdk8)
- Jackson module: Afterburner (from https://github.com/FasterXML/jackson-modules-base)
- Jackson module: JAXB-annotations (from http://github.com/FasterXML/jackson-module-jaxb-annotations)
- Jackson-annotations (from http://github.com/FasterXML/jackson)
- Jackson-core (from https://github.com/FasterXML/jackson-core)
- Jackson-dataformat-XML (from http://wiki.fasterxml.com/JacksonExtensionXmlDataBinding)
- Jackson-core (from http://wiki.fasterxml.com/JacksonHome)
- Jackson-dataformat-XML (from https://github.com/FasterXML/jackson-dataformat-xml)
- Jackson-dataformat-YAML (from https://github.com/FasterXML/jackson-dataformats-text)
- Jackson-datatype-Joda (from http://wiki.fasterxml.com/JacksonModuleJoda)
- Jackson-module-parameter-names (from https://repo1.maven.org/maven2/com/fasterxml/jackson/module/jackson-module-parameter-names)
- Jakarta Bean Validation API (from https://beanvalidation.org)
- Java Native Access (from https://github.com/java-native-access/jna)
- Java Native Access (from https://repo1.maven.org/maven2/net/java/dev/jna/jna)
- Java Native Access Platform (from https://github.com/java-native-access/jna)
- Java UUID Generator (from http://wiki.fasterxml.com/JugHome)
- Javassist (from http://www.javassist.org/)
......@@ -117,34 +119,43 @@ The following software have components provided under the terms of this license:
- Objenesis (from http://objenesis.org)
- OkHttp (from https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp)
- OkHttp Logging Interceptor (from https://repo1.maven.org/maven2/com/squareup/okhttp3/logging-interceptor)
- OkHttp URLConnection (from https://repo1.maven.org/maven2/com/squareup/okhttp3/okhttp-urlconnection)
- Okio (from https://github.com/square/okio/)
- OpenCensus (from https://github.com/census-instrumentation/opencensus-java)
- OpenCensus (from https://github.com/census-instrumentation/opencensus-java)
- PWDB :: Database (from https://repo1.maven.org/maven2/org/linguafranca/pwdb/database)
- Proton-J (from https://repo1.maven.org/maven2/org/apache/qpid/proton-j)
- Reactive Streams Netty driver (from https://github.com/reactor/reactor-netty)
- Retrofit (from https://repo1.maven.org/maven2/com/squareup/retrofit2/retrofit)
- Retrofit (from https://github.com/square/retrofit)
- SnakeYAML (from http://code.google.com/p/snakeyaml/)
- Spring AOP (from https://github.com/spring-projects/spring-framework)
- Spring Beans (from https://github.com/spring-projects/spring-framework)
- Spring Boot AOP Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-aop)
- Spring Boot Actuator (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-actuator)
- Spring Boot Actuator AutoConfigure (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-actuator-autoconfigure)
- Spring Boot Actuator Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-actuator)
- Spring Boot Json Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-json)
- Spring Boot Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter)
- Spring Boot Tomcat Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-tomcat)
- Spring Boot Validation Starter (from https://projects.spring.io/spring-boot/#/spring-boot-parent/spring-boot-starters/spring-boot-starter-validation)
- Spring Boot Web Starter (from http://projects.spring.io/spring-boot/)
- Spring Commons Logging Bridge (from https://github.com/spring-projects/spring-framework)
- Spring Context (from https://github.com/spring-projects/spring-framework)
- Spring Core (from https://github.com/spring-projects/spring-framework)
- Spring Data Core (from https://repo1.maven.org/maven2/org/springframework/data/spring-data-commons)
- Spring Expression Language (SpEL) (from https://github.com/spring-projects/spring-framework)
- Spring Framework: AOP (from http://www.springframework.org)
- Spring Framework: Context (from http://www.springframework.org)
- Spring Framework: WebMVC (from http://www.springframework.org)
- Spring TestContext Framework (from https://github.com/spring-projects/spring-framework)
- Spring Transaction (from https://github.com/spring-projects/spring-framework)
- Spring beans (from https://repo1.maven.org/maven2/org/springframework/spring-beans)
- Spring core (from https://repo1.maven.org/maven2/org/springframework/spring-core)
- Spring web (from https://repo1.maven.org/maven2/org/springframework/spring-web)
- Spring Web (from https://github.com/spring-projects/spring-framework)
- Spring Web MVC (from https://github.com/spring-projects/spring-framework)
- T-Digest (from https://github.com/tdunning/t-digest)
- Vavr (from http://vavr.io)
- Vavr Match (from http://vavr.io)
- Woodstox (from https://github.com/FasterXML/woodstox)
- Zipkin Core Library (from https://repo1.maven.org/maven2/io/zipkin/zipkin2/zipkin)
- Zipkin Reporter Brave (from https://repo1.maven.org/maven2/io/zipkin/reporter2/zipkin-reporter-brave)
- Zipkin Reporter: Core (from )
- Zipkin v2 (from )
- Zipkin Reporter: Core (from https://repo1.maven.org/maven2/io/zipkin/reporter2/zipkin-reporter)
- aalto-xml (from )
- aggs-matrix-stats (from https://github.com/elastic/elasticsearch)
- brave (from )
- compiler (from http://github.com/spullara/mustache.java)
- elasticsearch-cli (from https://github.com/elastic/elasticsearch)
- elasticsearch-core (from https://github.com/elastic/elasticsearch)
......@@ -153,7 +164,7 @@ The following software have components provided under the terms of this license:
- elasticsearch-x-content (from https://github.com/elastic/elasticsearch)
- error-prone annotations (from https://repo1.maven.org/maven2/com/google/errorprone/error_prone_annotations)
- io.grpc:grpc-context (from https://github.com/grpc/grpc-java)
- jackson-databind (from http://github.com/FasterXML/jackson)
- jackson-databind (from http://wiki.fasterxml.com/JacksonHome)
- javatuples (from http://www.javatuples.org)
- javax.inject (from http://code.google.com/p/atinject/)
- lang-mustache (from https://github.com/elastic/elasticsearch)
......@@ -162,25 +173,26 @@ The following software have components provided under the terms of this license:
- micrometer-core (from https://github.com/micrometer-metrics/micrometer)
- micrometer-registry-azure-monitor (from https://github.com/micrometer-metrics/micrometer)
- mockito-core (from https://github.com/mockito/mockito)
- okhttp-urlconnection (from https://github.com/square/okhttp)
- org.apiguardian:apiguardian-api (from https://github.com/apiguardian-team/apiguardian)
- org.opentest4j:opentest4j (from https://github.com/ota4j-team/opentest4j)
- parent-join (from https://github.com/elastic/elasticsearch)
- rank-eval (from https://github.com/elastic/elasticsearch)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://resilience4j.readme.io)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- resilience4j (from https://github.com/resilience4j/resilience4j)
- rest (from https://github.com/elastic/elasticsearch)
- rest-high-level (from https://github.com/elastic/elasticsearch)
- rxjava (from https://github.com/ReactiveX/RxJava)
- server (from https://github.com/elastic/elasticsearch)
- spring-boot (from https://spring.io/projects/spring-boot)
- spring-boot-actuator (from https://spring.io/projects/spring-boot)
- spring-boot-actuator-autoconfigure (from https://spring.io/projects/spring-boot)
- spring-boot-autoconfigure (from https://spring.io/projects/spring-boot)
- spring-boot-starter (from https://spring.io/projects/spring-boot)
- spring-boot-starter-actuator (from https://spring.io/projects/spring-boot)
- spring-boot-starter-json (from https://spring.io/projects/spring-boot)
- spring-boot-starter-log4j2 (from https://spring.io/projects/spring-boot)
- spring-boot-starter-tomcat (from https://spring.io/projects/spring-boot)
- spring-boot-starter-validation (from https://spring.io/projects/spring-boot)
- spring-boot-starter-web (from https://spring.io/projects/spring-boot)
- swagger-annotations (from https://repo1.maven.org/maven2/io/swagger/swagger-annotations)
- swagger-jaxrs (from )
- tomcat-embed-core (from http://tomcat.apache.org/)
......@@ -214,7 +226,7 @@ The following software have components provided under the terms of this license:
- Netty/Codec/HTTP (from https://repo1.maven.org/maven2/io/netty/netty-codec-http)
- Reflections (from http://github.com/ronmamo/reflections)
- SnakeYAML (from http://code.google.com/p/snakeyaml/)
- Spring core (from https://repo1.maven.org/maven2/org/springframework/spring-core)
- Spring Core (from https://github.com/spring-projects/spring-framework)
- jakarta.xml.bind-api (from )
========================================================================
......@@ -263,17 +275,18 @@ EPL-1.0
========================================================================
The following software have components provided under the terms of this license:
- AspectJ weaver (from http://www.aspectj.org)
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- JUnit Jupiter (Aggregator) (from https://junit.org/junit5/)
- JUnit Jupiter API (from https://junit.org/junit5/)
- JUnit Jupiter Engine (from https://junit.org/junit5/)
- JUnit Jupiter Params (from https://junit.org/junit5/)
- Microsoft Application Insights Java SDK Core (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Java SDK Spring Boot starter (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Java SDK Web Module (from https://github.com/Microsoft/ApplicationInsights-Java)
- Microsoft Application Insights Log4j 2 Appender (from https://github.com/Microsoft/ApplicationInsights-Java)
- SnakeYAML (from http://code.google.com/p/snakeyaml/)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- org.junit.jupiter:junit-jupiter-api (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-engine (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-params (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-commons (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-engine (from http://junit.org/junit5/)
......@@ -284,10 +297,10 @@ The following software have components provided under the terms of this license:
- Expression Language 3.0 (from https://projects.eclipse.org/projects/ee4j.el)
- JUnit Jupiter (Aggregator) (from https://junit.org/junit5/)
- JUnit Jupiter API (from https://junit.org/junit5/)
- JUnit Jupiter Engine (from https://junit.org/junit5/)
- JUnit Jupiter Params (from https://junit.org/junit5/)
- jakarta.annotation-api (from https://projects.eclipse.org/projects/ee4j.ca)
- org.junit.jupiter:junit-jupiter-api (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-engine (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-params (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-commons (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-engine (from http://junit.org/junit5/)
......@@ -348,7 +361,7 @@ LGPL-2.1-only
The following software have components provided under the terms of this license:
- Elastic JNA Distribution (from https://github.com/java-native-access/jna)
- Java Native Access (from https://github.com/java-native-access/jna)
- Java Native Access (from https://repo1.maven.org/maven2/net/java/dev/jna/jna)
- Java Native Access Platform (from https://github.com/java-native-access/jna)
- Javassist (from http://www.javassist.org/)
- Microsoft Application Insights Java SDK Core (from https://github.com/Microsoft/ApplicationInsights-Java)
......@@ -412,7 +425,7 @@ The following software have components provided under the terms of this license:
- adal4j (from https://github.com/AzureAD/azure-activedirectory-library-for-java)
- azure-documentdb (from https://azure.microsoft.com/en-us/services/cosmos-db/)
- documentdb-bulkexecutor (from http://azure.microsoft.com/en-us/services/documentdb/)
- java jwt (from http://www.jwt.io)
- java jwt (from https://github.com/auth0/java-jwt)
- micrometer-core (from https://github.com/micrometer-metrics/micrometer)
- mockito-core (from https://github.com/mockito/mockito)
- mockito-junit-jupiter (from https://github.com/mockito/mockito)
......@@ -492,7 +505,7 @@ The following software have components provided under the terms of this license:
- Microsoft Azure SDK for SQL API of Azure Cosmos DB Service (from https://github.com/Azure/azure-sdk-for-java)
- Microsoft Azure client library for Blob Storage (from https://github.com/Azure/azure-sdk-for-java)
- Project Lombok (from https://projectlombok.org)
- Spring web (from https://repo1.maven.org/maven2/org/springframework/spring-web)
- Spring Web (from https://github.com/spring-projects/spring-framework)
- azure-documentdb (from https://azure.microsoft.com/en-us/services/cosmos-db/)
- msal4j (from https://github.com/AzureAD/microsoft-authentication-library-for-java)
- reactive-streams (from http://www.reactive-streams.org/)
......@@ -506,12 +519,12 @@ The following software have components provided under the terms of this license:
- Checker Qual (from https://checkerframework.org)
- JSON in Java (from https://github.com/douglascrockford/JSON-java)
- JUnit Jupiter (Aggregator) (from https://junit.org/junit5/)
- JUnit Jupiter API (from https://junit.org/junit5/)
- JUnit Jupiter Engine (from https://junit.org/junit5/)
- JUnit Jupiter Params (from https://junit.org/junit5/)
- JavaBeans Activation Framework API jar (from )
- Spongy Castle (from http://rtyley.github.io/spongycastle/)
- jakarta.xml.bind-api (from )
- org.junit.jupiter:junit-jupiter-api (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-engine (from http://junit.org/junit5/)
- org.junit.jupiter:junit-jupiter-params (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-commons (from http://junit.org/junit5/)
- org.junit.platform:junit-platform-engine (from http://junit.org/junit5/)
......
......@@ -46,6 +46,7 @@
<azure.servicebus.version>3.6.3</azure.servicebus.version>
<io.micrometer.version>1.6.6</io.micrometer.version>
<azure-spring-data-cosmos.version>3.7.0</azure-spring-data-cosmos.version>
<resilience4jVersion>1.7.0</resilience4jVersion>
</properties>
......@@ -253,6 +254,22 @@
</exclusion>
</exclusions>
</dependency>
<!-- dependencies for resilience4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-all</artifactId>
<version>${resilience4jVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
......
......@@ -14,7 +14,12 @@
package org.opengroup.osdu.azure.httpconfig;
import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.decorators.Decorators;
import org.apache.http.HttpStatus;
import org.opengroup.osdu.azure.logging.CoreLoggerFactory;
import org.opengroup.osdu.azure.resiliency.AzureCircuitBreakerConfiguration;
import org.opengroup.osdu.core.common.http.FetchServiceHttpRequest;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.IHttpClient;
......@@ -25,7 +30,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.function.Supplier;
import static java.util.Arrays.asList;
/**
* Extends URlFetchService and implements IHttpClient to send requests.
......@@ -33,17 +42,20 @@ import java.net.URISyntaxException;
@Primary
@Component
public class HttpClientAzure implements IHttpClient {
private static final String LOGGER_NAME = HttpClientAzure.class.getName();
@Autowired
private AzureCircuitBreakerConfiguration azureCircuitBreakerConfiguration;
@Autowired
private UrlFetchServiceImpl urlFetchService;
/**
* calls urlfetchservice's send request.
* Decorated method to send request with circuitbreaker.
*
* @param httpRequest made by user class
* @return HttpResponse
*/
@Override
public HttpResponse send(final HttpRequest httpRequest) {
public HttpResponse decoratedSend(final HttpRequest httpRequest) {
org.opengroup.osdu.core.common.model.http.HttpResponse response = null;
try {
response = this.urlFetchService.sendRequest(FetchServiceHttpRequest.builder()
......@@ -63,4 +75,56 @@ public class HttpClientAzure implements IHttpClient {
httpResponse.setRequest(httpRequest);
return httpResponse;
}
/**
* calls urlfetchservice's send request after applying a circuitbreaker.
*
* @param httpRequest made by user class
* @return HttpResponse
*/
@Override
public HttpResponse send(final HttpRequest httpRequest) {
if (!azureCircuitBreakerConfiguration.isEnable()) {
// Call method without CircuitBreaker
return this.decoratedSend(httpRequest);
}
String circuitBreakerName = getHostName(httpRequest.getUrl());
if (circuitBreakerName == null) {
// Call method without CircuitBreaker
return this.decoratedSend(httpRequest);
}
CircuitBreaker circuitBreaker = azureCircuitBreakerConfiguration.getCircuitBreakerRegistry().circuitBreaker(circuitBreakerName);
circuitBreaker.getEventPublisher()
.onStateTransition(event -> CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info("CircuitBreakerEvent : {}", event));
Supplier<HttpResponse> httpResponseSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> this.decoratedSend(httpRequest));
// Ensuring CallNotPermittedException that is being thrown by CircuitBreaker is caught and Error Code 503 is thrown.
// We are throwing Error 503 based on information from https://docs.microsoft.com/en-us/azure/architecture/patterns/circuit-breaker
return Decorators.ofSupplier(httpResponseSupplier).withFallback(asList(CallNotPermittedException.class), throwable -> {
CoreLoggerFactory.getInstance().getLogger(LOGGER_NAME).info(throwable.getMessage());
throw new AppException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Service Unavailable", "Service Unavailable");
}).get();
}
/**
* Fetches hostname from URL.
* http://entitlements/api/entitlements/v2 --> entitlements
* @param url eg : http://entitlements/api/entitlements/v2
* @return will return "entitlements" or null if there's an error with URL
*/
public String getHostName(final String url) {
URI uri = null;
try {
uri = new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
}
String hostname = uri.getHost();
// to provide faultproof result, check if not null then return only hostname, without www.
if (hostname != null) {
return hostname.startsWith("www.") ? hostname.substring(4) : hostname;
}
return null;
}
}
/** Copyright © Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
**/
package org.opengroup.osdu.azure.resiliency;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
* Configuration for CircuitBreaker.
*/
@Configuration
@ConfigurationProperties("azure.circuitbreaker")
@Getter
@Setter
public class AzureCircuitBreakerConfiguration {
/**
* Constructor which initializes CircuitBreakerRegistry.
*/
public AzureCircuitBreakerConfiguration() {
this.setCBR();
}
private boolean enable = false;
@Getter
private CircuitBreakerRegistry circuitBreakerRegistry;
/**
* Create CircuitBreakerRegistry.
*/
private void setCBR() {
// Create a CircuitBreakerRegistry with a custom global configuration
this.circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults();
}
}
/** Copyright © Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
**/
package org.opengroup.osdu.azure.httpconfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.resiliency.AzureCircuitBreakerConfiguration;
import org.opengroup.osdu.core.common.http.FetchServiceHttpRequest;
import org.opengroup.osdu.core.common.http.HttpRequest;
import org.opengroup.osdu.core.common.http.UrlFetchServiceImpl;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.net.URISyntaxException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class HttpClientAzureTest {
@InjectMocks
HttpClientAzure sut;
@Mock
UrlFetchServiceImpl urlFetchService;
@Spy
AzureCircuitBreakerConfiguration azureCircuitBreakerConfiguration;
@Test
public void when_circuitbreaker_name_is_localhost() throws URISyntaxException {
azureCircuitBreakerConfiguration.setEnable(true);
azureCircuitBreakerConfiguration.setCircuitBreakerRegistry(CircuitBreakerRegistry.ofDefaults());
when(azureCircuitBreakerConfiguration.isEnable()).thenReturn(true);
org.opengroup.osdu.core.common.model.http.HttpResponse httpResponse = new org.opengroup.osdu.core.common.model.http.HttpResponse();
httpResponse.setBody("rr");
when(urlFetchService.sendRequest(any(FetchServiceHttpRequest.class))).thenReturn(httpResponse);
sut.send(HttpRequest.builder().url("http://localhost/").build());
assertEquals(azureCircuitBreakerConfiguration.getCircuitBreakerRegistry().getAllCircuitBreakers().get(0).getName(),"localhost");
}
@Test
public void when_circuitbreaker_name_is_localhost_with_www() throws URISyntaxException {
azureCircuitBreakerConfiguration.setEnable(true);
azureCircuitBreakerConfiguration.setCircuitBreakerRegistry(CircuitBreakerRegistry.ofDefaults());
when(azureCircuitBreakerConfiguration.isEnable()).thenReturn(true);
org.opengroup.osdu.core.common.model.http.HttpResponse httpResponse = new org.opengroup.osdu.core.common.model.http.HttpResponse();
httpResponse.setBody("rr");
when(urlFetchService.sendRequest(any(FetchServiceHttpRequest.class))).thenReturn(httpResponse);
sut.send(HttpRequest.builder().url("http://www.localhost/").build());
assertEquals(azureCircuitBreakerConfiguration.getCircuitBreakerRegistry().getAllCircuitBreakers().get(0).getName(),"localhost");
}
@Test
public void when_circuitbreaker_name_is_entitlements() throws URISyntaxException {
azureCircuitBreakerConfiguration.setEnable(true);
azureCircuitBreakerConfiguration.setCircuitBreakerRegistry(CircuitBreakerRegistry.ofDefaults());
when(azureCircuitBreakerConfiguration.isEnable()).thenReturn(true);
org.opengroup.osdu.core.common.model.http.HttpResponse httpResponse = new org.opengroup.osdu.core.common.model.http.HttpResponse();
httpResponse.setBody("rr");
when(urlFetchService.sendRequest(any(FetchServiceHttpRequest.class))).thenReturn(httpResponse);
sut.send(HttpRequest.builder().url("http://entitlements/api/v2").build());
assertEquals(azureCircuitBreakerConfiguration.getCircuitBreakerRegistry().getAllCircuitBreakers().get(0).getName(),"entitlements");
}
}
/** Copyright © Microsoft Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
**/
package org.opengroup.osdu.azure.resiliency;
import io.jsonwebtoken.lang.Assert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
@ExtendWith(MockitoExtension.class)
public class AzureCircuitBreakerConfigurationTest {
AzureCircuitBreakerConfiguration circuitBreakerConfiguration = new AzureCircuitBreakerConfiguration();
@Test
public void should_create_CircuitBreakerRegistry() {
Assert.isTrue(circuitBreakerConfiguration.getCircuitBreakerRegistry()!=null);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment