Resilience4J Circuit Breaker configs not loaded inside integration test - spring-boot

I'm trying to create an integration test in spring boot which should give me a circuit breaker that uses some configs that I specify but for some reason it gives me one with default configs and I don't understand why :(
The test looks like this:
#SpringBootTest(
classes = [
AmericiumApi::class,
AmericiumClient::class,
RetrofitConfig::class,
HttpConnectorListener::class,
TestMetricsConfig::class,
Info4cProperties::class,
InMemoryCircuitBreakerRegistry::class,
CircuitBreakerRegistry::class
],
properties = [
"test.api.url=http://localhost:\${wiremock.server.port}",
"resilience4j.circuitbreaker.configs.default.register-health-indicator=false",
"resilience4j.circuitbreaker.configs.default.sliding-window-size=10",
"resilience4j.circuitbreaker.configs.default.sliding-window-type=COUNT_BASED",
"resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50",
"resilience4j.circuitbreaker.configs.default.minimum-number-of-calls=10",
"resilience4j.circuitbreaker.configs.default.slow-call-duration-threshold=3000",
"resilience4j.circuitbreaker.configs.default.slowCallRateThreshold=100",
"resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=3000",
"resilience4j.circuitbreaker.configs.default.automatic-transition-from-open-to-half-open-enabled=true",
"resilience4j.circuitbreaker.configs.default.permitted-number-of-calls-in-half-open-state=5",
]
)
#AutoConfigureWireMock(port = 0)
class TestWiremockIntegrationTest {
Do I need to load other classes in the context?

Related

Podam Unit Testing same instance

I've seen while I init and manufactor Pojo's in the multiple Unit tests that the Instance of pojo is the same, how can I get init different Instance for each Test .
/Init PodamFactory
PodamFactory podamFactory = new PodamFactoryImpl();
//Define Strategy to exclude some fields
DefaultClassInfoStrategy classInfoStrategy = DefaultClassInfoStrategy.getInstance();

Need wire to emit interfaces for client and server in same gradle project

I want to use interfaces for both client and server in the same android app. Usecase is to run a okhttpmockwebserver serving gRPC requests within the same app the client is running in. For this i created two library projects with their own wire configuration for client and server similar to those
wire {
kotlin {
includes = ['com..caompany.android.proto.*']
out "${buildDir}/protos"
rpcCallStyle = 'suspending'
rpcRole = 'client'
}
}
wire {
kotlin {
includes = ['com..company.android.proto.*']
out "${buildDir}/protos"
rpcCallStyle = 'suspending'
rpcRole = 'server'
}
}
Executing the wire-gradle-plugin fails with this exception:
com.company.android.proto.HelloReply$Companion$ADAPTER$1 is defined multiple times.
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: .../com/company/android/proto/HelloReply$Companion$ADAPTER$1.dex
It would help me if wire could either
Generate all classes and interfaces at once including server and client role or
Exclude the generation of class files, only generating service interfaces for client or server
Is there a workaround i can achieve a similar result without gradle plugin support?
You can have multiple kotlin blocks at the same time. Wire will throw if you generate the same class twice so you need to define the rule as unique between both.
You need one block which will generate client role interfaces. You need one block to generate server roles interfaces. Lastly, you need to generate regular types in yet another block, or in one of them (but not both).
Something like this
wire {
kotlin {
includes = ['all.services.or.package']
rpcCallStyle = 'suspending'
rpcRole = 'client'
}
kotlin {
includes = ['all.services.or.package']
rpcCallStyle = 'suspending'
rpcRole = 'server'
}
kotlin {
excludes = ['all.services.or.package']
rpcRole = 'none'
}
}

How to Unit test Service & controller (kotlin) in a Cordapp?

I have gone through many documentations for getting a sample of unit testing service and controller in a Cordapp, not the flows (that is already done using in corda docs). Can anyone please help me to get an example cordapp which implemented service unit testing?
Try taking a look at the CordaService Autopayroll sample on github.
link: https://github.com/corda/samples-java/tree/master/Features/cordaservice-autopayroll
There's an ability to access registered services that gets used here in the testing code
//Test #1 check if the requestState is being sent to the bank operator behind the scene.
#Test
fun `dummy test`() {
val future = a.startFlow(RequestFlowInitiator("500", b.info.legalIdentities.first()))
network.runNetwork()
val ptx = future.get()
println("Signed transaction hash: ${ptx.id}")
listOf(a, bank).map {
it.services.validatedTransactions.getTransaction(ptx.id)
}.forEach {
val txHash = (it as SignedTransaction).id
println("$txHash == ${ptx.id}")
assertEquals(ptx.id, txHash)
}
}
link: https://github.com/corda/samples-kotlin/blob/master/Features/cordaService-autopayroll/workflows-kotlin/src/test/kotlin/net/corda/examples/autopayroll/FlowTests.kt
Good luck!
We can use Mockito module for mocking and stabbing that is required for unit testing service functions and APIs.
This link will direct more on how to mock CordaRPCops using mockito as an example.

Injecting Spring Beans to Groovy Script

I've seen many examples about Groovy objects as Spring beans but not vice versa. I'm using Groovy in a Java EE application like this:
GroovyCodeSource groovyCodeSource = new GroovyCodeSource(urlResource);
Class groovyClass = loader.parseClass(groovyCodeSource, false);
return (GroovyObject) groovyClass.newInstance();
In this way, classes written in Groovy with #Configurable annotation are being injected with Spring beans. It's OK for now.
How can I get the same by using GroovyScriptEngine? I don't want to define a class and I want it to work like a plain script. Is Spring/Groovy capable of that?
I've seen a post about this but I'm not sure whether it answers my question or not:
HERE
Do you mean that you'd like to add properties to the script, and inject those? Would you provide getter and setter? This does not make much sense to me. What makes sense, is adding the mainContext to the bindings of the script, or adding selected beans to the bindings.
These beans - or the context - would then be accessible directly in the script, as if it was injected.
def ctx = grailsApplication.mainContext
def binding = new Binding([:])
Map variables = [
'aService',
'anotherService'
].inject([config:grailsApplication.config, mainContext:ctx]) { m, beanName ->
def bean = ctx.getBean(beanName)
m[beanName] = bean
m
}
binding.variables << variables
def compiler = new CompilerConfiguration()
compiler.setScriptBaseClass(baseScriptClassName)
def shell = new GroovyShell(new GroovyClassLoader(), binding, compiler)
script=shell.parse(scriptStr)
script.binding=binding
script.init()
script.run()

Spring DSL in Grails - resources.groovy - bean configuration in a different file?

This question already exists in a way, but the existing question is missing some important links.
I'm trying to move the configuration of beans for my tests into separate files that end in *TestsSpringBeans.groovy
I've attempted to do this after reading "Loading Bean Definitions from the File System" (search for it) in the Groovy documentation.
Here are the relevant code segments:
import grails.util.*
beans = {
...
switch(Environment.current) {
case Environment.TEST:
loadBeans("classpath:*TestsSpringBeans.groovy")
break
}
}
resources.groovy - Loading the *TestSpringBeans files from the File System.
somePlace(jobdb.Company) {
name = "SomeCompany"
addr1 = "addr1"
addr2 = "addr2"
city = "city"
email = "somedude#h0tmail.com"
fax = "555-555-5555"
phone = "444-444-4444"
state = "PA"
zip = "19608"
version: 0
created = new Date()
updated = new Date()
website = "http://www.yahoo.com"
deleted = false
}
CompanyServiceTestsSpringBeans.groovy - Defining a bean for the Integration Test
// Retrieve configured bean from
Company someplace = ApplicationHolder.getApplication().getMainContext().getBean('somePlace')
CompanyServiceTests.groovy - Obtain the bean somePlace within the Integration Test...
Upon calling getBean('somePlace') within the test an error is displayed which reads that No bean named 'somePlace' is defined
The CompanyServiceTests.groovy file is stored with my integration tests, should I be storing this file somewhere else in the project directory structure?
Since your tests run in a way that using the classpath as a reference point is less important, you might try to load the beans { ... } file by referencing via project directory specific path. (e.g. $baseDir/test/resources/MyCustomBeans.groovy) or load the beans explicitly in your tests via #BeforeClass if you are using JUnit4 annotations:
def bb = new BeanBuilder()
def resource = new FileSystemResource('src/test/resources/testContext.groovy')
bb.loadBeans(resource)
appCtx = bb.createApplicationContext()
...

Resources