From f386368f1bfb4a1c753d2f4d27da19decd4cb9c6 Mon Sep 17 00:00:00 2001 From: K Siva Prasad Reddy Date: Thu, 30 Nov 2023 22:35:34 +0530 Subject: [PATCH] Code formatting using prettier --- build.gradle | 8 ++- pom.xml | 15 ++++-- .../products/config/SecurityConfig.java | 29 ++++++---- .../products/domain/ProductRepository.java | 7 ++- .../products/ContainersConfig.java | 9 ++-- .../products/TestApplication.java | 5 +- .../products/api/ProductControllerTests.java | 53 +++++++++++-------- 7 files changed, 83 insertions(+), 43 deletions(-) diff --git a/build.gradle b/build.gradle index f8b26a3..8550849 100644 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,13 @@ spotless { java { importOrder() removeUnusedImports() - palantirJavaFormat("2.38.0") + prettier(['prettier': '3.0.3', 'prettier-plugin-java': '2.3.0']) + .config([ + 'parser': 'java', + 'tabWidth': 4, + 'printWidth': 80, + 'plugins': ['prettier-plugin-java'] + ]) formatAnnotations() } } diff --git a/pom.xml b/pom.xml index c6a0aae..8f257db 100644 --- a/pom.xml +++ b/pom.xml @@ -85,9 +85,18 @@ - - 2.38.0 - + + + 3.0.3 + 2.3.0 + + + java + 4 + 80 + prettier-plugin-java + + diff --git a/src/main/java/com/testcontainers/products/config/SecurityConfig.java b/src/main/java/com/testcontainers/products/config/SecurityConfig.java index fc8d231..e4aab8b 100644 --- a/src/main/java/com/testcontainers/products/config/SecurityConfig.java +++ b/src/main/java/com/testcontainers/products/config/SecurityConfig.java @@ -17,17 +17,24 @@ class SecurityConfig { @Bean - SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http.authorizeHttpRequests(c -> c.requestMatchers(HttpMethod.GET, "/api/products") - .permitAll() - .requestMatchers(HttpMethod.POST, "/api/products") - .authenticated() - .anyRequest() - .authenticated()) - .sessionManagement(c -> c.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) - .cors(CorsConfigurer::disable) - .csrf(CsrfConfigurer::disable) - .oauth2ResourceServer(oauth2 -> oauth2.jwt(withDefaults())); + SecurityFilterChain securityFilterChain(HttpSecurity http) + throws Exception { + http + .authorizeHttpRequests(c -> + c + .requestMatchers(HttpMethod.GET, "/api/products") + .permitAll() + .requestMatchers(HttpMethod.POST, "/api/products") + .authenticated() + .anyRequest() + .authenticated() + ) + .sessionManagement(c -> + c.sessionCreationPolicy(SessionCreationPolicy.STATELESS) + ) + .cors(CorsConfigurer::disable) + .csrf(CsrfConfigurer::disable) + .oauth2ResourceServer(oauth2 -> oauth2.jwt(withDefaults())); return http.build(); } } diff --git a/src/main/java/com/testcontainers/products/domain/ProductRepository.java b/src/main/java/com/testcontainers/products/domain/ProductRepository.java index 600b24a..41e5506 100644 --- a/src/main/java/com/testcontainers/products/domain/ProductRepository.java +++ b/src/main/java/com/testcontainers/products/domain/ProductRepository.java @@ -7,6 +7,7 @@ @Repository public class ProductRepository { + private static final AtomicLong ID = new AtomicLong(0L); private static final List PRODUCTS = new ArrayList<>(); @@ -15,7 +16,11 @@ public List getAll() { } public Product create(Product product) { - Product p = new Product(ID.incrementAndGet(), product.title(), product.description()); + Product p = new Product( + ID.incrementAndGet(), + product.title(), + product.description() + ); PRODUCTS.add(p); return p; } diff --git a/src/test/java/com/testcontainers/products/ContainersConfig.java b/src/test/java/com/testcontainers/products/ContainersConfig.java index 7cd7d33..3f9d3a1 100644 --- a/src/test/java/com/testcontainers/products/ContainersConfig.java +++ b/src/test/java/com/testcontainers/products/ContainersConfig.java @@ -7,6 +7,7 @@ @TestConfiguration(proxyBeanMethods = false) public class ContainersConfig { + static String KEYCLOAK_IMAGE = "quay.io/keycloak/keycloak:23.0.1"; static String realmImportFile = "/keycloaktcdemo-realm.json"; static String realmName = "keycloaktcdemo"; @@ -14,11 +15,11 @@ public class ContainersConfig { @Bean KeycloakContainer keycloak(DynamicPropertyRegistry registry) { var keycloak = new KeycloakContainer(KEYCLOAK_IMAGE) - .withRealmImportFile(realmImportFile) - .withReuse(true); + .withRealmImportFile(realmImportFile); registry.add( - "spring.security.oauth2.resourceserver.jwt.issuer-uri", - () -> keycloak.getAuthServerUrl() + "/realms/" + realmName); + "spring.security.oauth2.resourceserver.jwt.issuer-uri", + () -> keycloak.getAuthServerUrl() + "/realms/" + realmName + ); return keycloak; } } diff --git a/src/test/java/com/testcontainers/products/TestApplication.java b/src/test/java/com/testcontainers/products/TestApplication.java index beccd05..6882083 100644 --- a/src/test/java/com/testcontainers/products/TestApplication.java +++ b/src/test/java/com/testcontainers/products/TestApplication.java @@ -5,6 +5,9 @@ public class TestApplication { public static void main(String[] args) { - SpringApplication.from(Application::main).with(ContainersConfig.class).run(args); + SpringApplication + .from(Application::main) + .with(ContainersConfig.class) + .run(args); } } diff --git a/src/test/java/com/testcontainers/products/api/ProductControllerTests.java b/src/test/java/com/testcontainers/products/api/ProductControllerTests.java index 77ed3af..0b09698 100644 --- a/src/test/java/com/testcontainers/products/api/ProductControllerTests.java +++ b/src/test/java/com/testcontainers/products/api/ProductControllerTests.java @@ -26,9 +26,9 @@ @Import(ContainersConfig.class) class ProductControllerTests { - public static final String GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials"; - public static final String CLIENT_ID = "product-service"; - public static final String CLIENT_SECRET = "jTJJqdzeCSt3DmypfHZa42vX8U9rQKZ9"; + static final String GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials"; + static final String CLIENT_ID = "product-service"; + static final String CLIENT_SECRET = "jTJJqdzeCSt3DmypfHZa42vX8U9rQKZ9"; @LocalServerPort private int port; @@ -48,37 +48,41 @@ void shouldGetProductsWithoutAuthToken() { @Test void shouldGetUnauthorizedWhenCreateProductWithoutAuthToken() { - given().contentType("application/json") - .body( - """ + given() + .contentType("application/json") + .body( + """ { "title": "New Product", "description": "Brand New Product" } - """) - .when() - .post("/api/products") - .then() - .statusCode(401); + """ + ) + .when() + .post("/api/products") + .then() + .statusCode(401); } @Test void shouldCreateProductWithAuthToken() { String token = getToken(); - given().header("Authorization", "Bearer " + token) - .contentType("application/json") - .body( - """ + given() + .header("Authorization", "Bearer " + token) + .contentType("application/json") + .body( + """ { "title": "New Product", "description": "Brand New Product" } - """) - .when() - .post("/api/products") - .then() - .statusCode(201); + """ + ) + .when() + .post("/api/products") + .then() + .statusCode(201); } private String getToken() { @@ -92,10 +96,15 @@ private String getToken() { map.put("client_secret", singletonList(CLIENT_SECRET)); String authServerUrl = - oAuth2ResourceServerProperties.getJwt().getIssuerUri() + "/protocol/openid-connect/token"; + oAuth2ResourceServerProperties.getJwt().getIssuerUri() + + "/protocol/openid-connect/token"; var request = new HttpEntity<>(map, httpHeaders); - KeyCloakToken token = restTemplate.postForObject(authServerUrl, request, KeyCloakToken.class); + KeyCloakToken token = restTemplate.postForObject( + authServerUrl, + request, + KeyCloakToken.class + ); assert token != null; return token.accessToken();