Skip to content

Instantly share code, notes, and snippets.

View jcohen66's full-sized avatar

Jonathan T. Cohen jcohen66

View GitHub Profile
@jcohen66
jcohen66 / AuthorizationConfig.java
Last active December 22, 2018 22:41
Spring AuthorizationConfig #spring #security #oauth2
package com.example.springwebsecurity.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
@jcohen66
jcohen66 / SecurityConfig.java
Last active December 22, 2018 22:40
Spring SecurityConfig #spring #security #oauth2
package com.example.springwebsecurity.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
@jcohen66
jcohen66 / SpringApplicationTest.java
Created December 23, 2018 01:36
Spring Application Test #spring #test
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@jcohen66
jcohen66 / HttpRequestTest.java
Created December 23, 2018 01:39
Spring HttpRequest Test #spring #test #http
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@jcohen66
jcohen66 / SmokeTest.java
Created December 23, 2018 01:41
Spring Smoke Test #spring #test
import com.katonahcomputing.springbasictests.Home.HomeController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
@jcohen66
jcohen66 / WebMockTest.java
Created December 23, 2018 01:42
Spring Mock Test #spring #test #mock
import static org.hamcrest.Matchers.containsString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.katonahcomputing.springbasictests.Greeting.GreetingController;
import com.katonahcomputing.springbasictests.Greeting.GreetingService;
import org.junit.Test;
@jcohen66
jcohen66 / companion-object.scala
Created December 23, 2018 03:13
Companion Object #object #singleton #class
object NYTimesAccount {
private val userName = "vitthal"
private val password = "boo"
// Implements Factory design pattern.
def apply() = new NYTimesAccount
}
@jcohen66
jcohen66 / date-comparison.java
Created December 23, 2018 03:15
Date Comparison #date
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse("2009-12-31");
Date date1 = sdf.parse("2010-01-01");
System.out.println("date1 : " + sdf.format(date1));
@jcohen66
jcohen66 / completable-future.java
Created December 23, 2018 03:17
Completable Future #future #concurrency
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Application {
public static void main(String[] args) throws ExecutionException, InterruptedException {
Future<String> completableFuture = calculateAsync();
String result = completableFuture.get();
System.out.println(result);
@jcohen66
jcohen66 / lambda-function-composition.java
Created December 23, 2018 03:19
Lambda Function Composition #function #lambda #composition #apply
/**
* This translates to g(f(x))
*/
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
Function<Integer, Integer> h = f.andThen(g);
int res2 = h.apply(1);
System.out.println(res2);