Skip to content

Commit

Permalink
Code formatting using prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Nov 30, 2023
1 parent 4e7381d commit f386368
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 43 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
15 changes: 12 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,18 @@
<java>
<importOrder />
<removeUnusedImports />
<palantirJavaFormat>
<version>2.38.0</version>
</palantirJavaFormat>
<prettier>
<devDependencies>
<prettier>3.0.3</prettier>
<prettier-plugin-java>2.3.0</prettier-plugin-java>
</devDependencies>
<config>
<parser>java</parser>
<tabWidth>4</tabWidth>
<printWidth>80</printWidth>
<plugins>prettier-plugin-java</plugins>
</config>
</prettier>
<formatAnnotations />
</java>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

@Repository
public class ProductRepository {

private static final AtomicLong ID = new AtomicLong(0L);
private static final List<Product> PRODUCTS = new ArrayList<>();

Expand All @@ -15,7 +16,11 @@ public List<Product> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@

@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";

@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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();
Expand Down

0 comments on commit f386368

Please sign in to comment.