Skip to content

Instantly share code, notes, and snippets.

@mike-seger
mike-seger / ws-series.md
Last active December 15, 2024 07:07
WS Series controlled LED use cases

WS Series controlled LED use cases

Model Voltage Features Use Case
WS2811 12V External IC Budget-friendly, long runs
WS2812B 5V Integrated IC, compact General-purpose, most popular
WS2813 5V Backup data line High reliability
WS2815 12V Low current, long strips Large installations
WS2801 5V SPI control Older systems or simple timing
WS2821 5V High-speed Faster animations

You:

in Spring i can do tests @WithMockUser and a list of roles is there a way to achieve the same with parameterized tests looping through an actual list of roles instead of a single test with the combination of roles?

ChatGPT:

Yes, you can achieve this with parameterized tests by dynamically setting up each test with a single role from a list of roles. However, @WithMockUser doesn’t directly support parameterized roles out of the box. Instead, you can use @ParameterizedTest and programmatically create a mock user with each role in the list.Here’s how you can do it by leveraging SecurityContextHolder to manually set up the authentication for each test:

@mike-seger
mike-seger / ControllerTests.kt
Last active November 3, 2024 14:11
Parameterized controller tests
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.MethodSource
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType

You:

i have a spring security enabled test application currently configured with a single test user in application.yaml: spring: security.user: name: user password: pw

the goal is to modify this setup to allow 2 distinct users (user1, user2) basic auth access through a SecurityFilterChain, permitting user1 access to a resource antMatchers("/dir1/") and user2 access to a resource antMatchers("/dir2/")

@mike-seger
mike-seger / SqlScriptRunner.kt
Created October 28, 2024 23:37
Sql Script Runner for JdbcTemplate
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.support.rowset.SqlRowSet
class SqlScriptRunner(private val jdbcTemplate: JdbcTemplate) {
fun executeSqlScriptAndGetLastResult(sqlScript: String): SqlRowSet? {
// Split script into individual statements by ";", ignoring line comments "--" and block comments "/* */"
val statements = sqlScript.split("(?<!;)\\s*;\\s*(?=(?:[^']*'[^']*')*[^']*$)".toRegex())
.map { it.trim() }
.filter { it.isNotEmpty() }
@mike-seger
mike-seger / Duration-Parser.js
Created September 28, 2024 18:02
Light-Weight Javascript Duration Parser
parseIso8601Duration(iso8601Duration) {
const iso8601DurationRegex = /(-)?P(?:([.,\d]+)Y)?(?:([.,\d]+)M)?(?:([.,\d]+)W)?(?:([.,\d]+)D)?T{0,1}(?:([.,\d]+)H)?(?:([.,\d]+)M)?(?:([.,\d]+)S)?/;
const matches = iso8601Duration.match(iso8601DurationRegex);
return {
sign: matches[1] === undefined ? '+' : '-',
years: matches[2] === undefined ? 0 : Number(matches[2]),
months: matches[3] === undefined ? 0 : Number(matches[3]),
weeks: matches[4] === undefined ? 0 : Number(matches[4]),
days: matches[5] === undefined ? 0 : Number(matches[5]),
@mike-seger
mike-seger / pg-data-growth.md
Created September 18, 2024 06:32
Postgres Data Growth

You:

In postgres, we have a table which contains 100MB of data with daily updates on most of the 250K records. All other data tables combined are 400MB. If the data partition size is 1 GB, is there a danger of running out of disk space over time?

ChatGPT:

Yes, there is a potential risk of running out of disk space over time, particularly due to a combination of factors such as daily updates and the partitioning strategy.

Here's a breakdown of the key considerations:

@mike-seger
mike-seger / Kubernetes MicroServices Setup.md
Last active September 8, 2024 21:30
Kubernetes MicroService Setup

You:

given an architecture consisting of 15 springboot applications, one of which is a springcloud gateway providing the entry point to the others. all other micro-services would not be accessible from the external network. how would you set these up using kubernetes?

ChatGPT:

To set up this architecture using Kubernetes (K8s), where you have 15 Spring Boot applications (microservices), one of which is a Spring Cloud Gateway that acts as the entry point to the others, here's a high-level approach. This setup ensures that the other services are not accessible from the external network, only the gateway service is.

Steps for Kubernetes Setup:

@mike-seger
mike-seger / Generic Queries.md
Last active August 23, 2024 15:24
Generic Queries

You:

can you provide a kotlin method to be used within a springboot application to check the existence of a table by name?

ChatGPT:

Certainly! Below is a Kotlin method that can be used within a Spring Boot application to check the existence of a table by name. This method uses JdbcTemplate to interact with the database.

@mike-seger
mike-seger / LiquibaseConfiguration.kt
Last active August 22, 2024 17:37
LiquibaseConfiguration
import liquibase.integration.spring.SpringLiquibase
import liquibase.database.Database
import liquibase.database.DatabaseFactory
import liquibase.database.AbstractJdbcDatabase
import liquibase.resource.ResourceAccessor
import java.sql.Connection
import javax.sql.DataSource
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.beans.factory.annotation.Value