Skip to content

Instantly share code, notes, and snippets.

View sirius2k's full-sized avatar

Kyoungwook Park sirius2k

  • Electronic arts
  • Seoul
View GitHub Profile
@sirius2k
sirius2k / SignupForm.java
Created June 17, 2018 04:14
SpringBoot BeanValidator custom messages from MessageSource
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SignupForm {
@NotEmpty(message = "{validation.id.NotEmpty}")
@Pattern(regexp = "^[a-zA-Z]{1}[a-zA-Z0-9_]{4,20}$", message = "{validation.id.Pattern}")
private String id;
@NotEmpty(message = "{validation.email.NotEmpty}")
@Email(message = "{validation.email.Email}")
@sirius2k
sirius2k / OptionalFilter.java
Last active June 17, 2018 01:02
Java Optional usage
// Before
public Member getMemberIfExceedsBasePrice(Order order, long basePrice) {
if (order != null && order.getTotalPrice() > basePrice) {
return order.getMember();
}
}
// After
public Optional<Member> getMemberIfExceedsBasePrice(Order order, long basePrice) {
return Optional.ofNullable(order)
@sirius2k
sirius2k / regexp.java
Last active June 16, 2018 07:41
ID, Password regexp pattern
// Id starts with alphabet and alphanumeric characters. Minimum length 4, Maximum length 20.
@Pattern(regexp = "^[a-zA-Z]{1}[a-zA-Z0-9_]{4,20}$")
// Password requires at least number, capital character, non-capital character and special character. Minimum length 8, Maximum length 20.
// ^ # start-of-string
// (?=.*[0-9]) # a digit must occur at least once
// (?=.*[a-z]) # a lower case letter must occur at least once
// (?=.*[A-Z]) # an upper case letter must occur at least once
// (?=.*[@#$%^&+=]) # a special character must occur at least once
// (?=\S+$) # no whitespace allowed in the entire string
@sirius2k
sirius2k / LoginTest.java
Created June 6, 2018 12:40
Usage of Hamcrest instanceOf
public void testLogin() throws Exception {
this.mockMvc.perform((post("/login").accept(MediaType.ALL).param("id", "admin").param("password", "admin").with(csrf())))
.andDo(print())
.andExpect(status().is3xxRedirection())
.andExpect(request().sessionAttribute("SPRING_SECURITY_LAST_EXCEPTION", instanceOf(BadCredentialsException.class)));
}
@sirius2k
sirius2k / Java snippet
Last active May 6, 2018 13:53
mustache if/else example
boolean kiss = true;
boolean hug = true;
boolean slap = false;
@sirius2k
sirius2k / SpringConfig.java
Last active April 18, 2018 17:03
conditional activation of bean with profile annotation in Spring
@Configuration
public class SpringConfig {
@Profile("development")
@Bean
public TestComponent testComponent() {
return new DevTestComponent();
}
@Profile({ "production", "staging" })
@Bean
@sirius2k
sirius2k / docker_run.sh
Last active April 18, 2018 11:46
docker run command example
# run command format
$ docker run -d -p <host port>:<container port> -v <host volume>:<container volume> --name <container name> <image>
# sample
$ docker run -d -p 8080:8080 -v /host/data:/container/data --name my_container my_container
@sirius2k
sirius2k / docker_run_bash.sh
Last active April 18, 2018 10:50
Run docker image with bash shell
# Assuming an Ubuntu Docker image and there's no ENTRYPOINT
$ docker run -it <image> /bin/bash
# Assuming an Ubuntu Docker image and override ENTRYPOINT
$ docker run -it --entrypoint "/bin/bash" <image>
@sirius2k
sirius2k / read_json.groovy
Created April 16, 2018 04:33
Read json in Jenkins pipeline groovy script
/*
example json text in dir/input.json
{ "key": "value" }
*/
def props = readJSON file: 'dir/input.json'
assert props['key'] == 'value'
assert props.key == 'value'
def props = readJSON text: '{ "key": "value" }'
assert props['key'] == 'value'
@sirius2k
sirius2k / extract_string_in_parentheses.groovy
Last active April 16, 2018 04:17
Groovy regex example
def pattern = "\\(([^)]+)\\)"
def matcher = "test string (abc)" =~ pattern
// matcher[0][1] will return abc
println("extracted string : " + matcher[0][1])