Merge branch 'eugenp:master' into master
This commit is contained in:
commit
a8d8a0f948
|
@ -12,7 +12,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class AccountUnitTest {
|
||||
public class AccountManualTest {
|
||||
|
||||
private Account account;
|
||||
|
|
@ -14,6 +14,26 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<version>${mockito-inline.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-function</finalName>
|
||||
<resources>
|
||||
|
@ -24,4 +44,10 @@
|
|||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<mockito-inline.version>3.8.0</mockito-inline.version>
|
||||
<assertj.version>3.22.0</assertj.version>
|
||||
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
public final class CustomStringUtils {
|
||||
|
||||
private CustomStringUtils() {}
|
||||
|
||||
public static boolean isEmpty(CharSequence cs) { return cs == null || cs.length() == 0; }
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
public class StaticCounter {
|
||||
|
||||
private static int counter = 0;
|
||||
|
||||
public static int incrementCounter() {
|
||||
return ++counter;
|
||||
}
|
||||
|
||||
public static int getCounterValue() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CollectionUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenListOfNumbers_whenReverseStaticMethodIsCalled_thenNumbersInReversedOrderAreReturned() {
|
||||
List<String> list = Arrays.asList("1", "2", "3");
|
||||
Collections.reverse(list);
|
||||
assertThat(list).containsExactly("3", "2", "1");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class CustomStringUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenNonEmptyString_whenIsEmptyMethodIsCalled_thenFalseIsReturned() {
|
||||
boolean empty = CustomStringUtils.isEmpty("baeldung");
|
||||
assertThat(empty).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenEmptyString_whenIsEmptyMethodIsCalled_thenTrueIsReturned() {
|
||||
boolean empty = CustomStringUtils.isEmpty("");
|
||||
assertThat(empty).isTrue();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class StaticCounterUnitTest {
|
||||
|
||||
@Test
|
||||
void givenStaticCounter_whenIncrementCounterIsCalled_thenValueIsIncresedByOne() {
|
||||
int oldValue = StaticCounter.getCounterValue();
|
||||
int newValue = StaticCounter.incrementCounter();
|
||||
assertThat(newValue).isEqualTo(oldValue + 1);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.staticmethods;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class StringUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
void givenSimpleString_whenCapitalizeStaticMethodIsCalled_thenCapitalizedStringIsReturned() {
|
||||
String str = StringUtils.capitalize("baeldung");
|
||||
assertThat(str).isEqualTo("Baeldung");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.streams.parallel;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Level;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.TearDown;
|
||||
|
||||
@State(Scope.Benchmark)
|
||||
public class FileSearchCost {
|
||||
|
||||
private final static String FILE_NAME = "src/main/resources/Test";
|
||||
|
||||
@Setup(Level.Trial)
|
||||
public void setup() throws IOException {
|
||||
for (int i = 0; i < 1500; i++) {
|
||||
File targetFile = new File(FILE_NAME + i);
|
||||
FileUtils.writeStringToFile(targetFile, "Test", "UTF8");
|
||||
}
|
||||
}
|
||||
|
||||
@TearDown(Level.Trial)
|
||||
public void tearDown() {
|
||||
for (int i = 0; i < 1500; i++) {
|
||||
File fileToDelete = new File(FILE_NAME + i);
|
||||
fileToDelete.delete();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void textFileSearchSequential() throws IOException {
|
||||
Files.walk(Paths.get("src/main/resources/")).map(Path::normalize).filter(Files::isRegularFile)
|
||||
.filter(path -> path.getFileName().toString().endsWith(".txt")).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
public static void textFileSearchParallel() throws IOException {
|
||||
Files.walk(Paths.get("src/main/resources/")).parallel().map(Path::normalize).filter(Files::isRegularFile)
|
||||
.filter(path -> path.getFileName().toString().endsWith(".txt")).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
FROM alpine:latest
|
||||
|
||||
ARG name
|
||||
ENV env_name $name
|
||||
|
||||
COPY greetings.sh .
|
||||
|
||||
RUN chmod +x /greetings.sh
|
||||
|
||||
CMD ["/greetings.sh"]
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
|
||||
echo Hello $env_name
|
|
@ -1,4 +1,4 @@
|
|||
## Relevant Articles:
|
||||
|
||||
- [Creating a Kubertes Admission Controller in Java](https://www.baeldung.com/java-kubernetes-admission-controller)
|
||||
- [Creating a Kubernetes Admission Controller in Java](https://www.baeldung.com/java-kubernetes-admission-controller)
|
||||
- [Access Control Models](https://www.baeldung.com/java-access-control-models)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.uuid.event;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener;
|
||||
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertEvent;
|
||||
|
||||
import com.baeldung.uuid.model.UuidIdentifiedEntity;
|
||||
|
||||
|
||||
public class UuidIdentifiedEntityEventListener extends AbstractMongoEventListener<UuidIdentifiedEntity> {
|
||||
|
||||
@Override
|
||||
public void onBeforeConvert(BeforeConvertEvent<UuidIdentifiedEntity> event) {
|
||||
|
||||
super.onBeforeConvert(event);
|
||||
UuidIdentifiedEntity entity = event.getSource();
|
||||
|
||||
if(entity.getId() == null) {
|
||||
entity.setId(UUID.randomUUID());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.uuid.model;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
@Document
|
||||
public class Book extends UuidIdentifiedEntity {
|
||||
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.uuid.model;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
|
||||
public abstract class UuidIdentifiedEntity {
|
||||
|
||||
@Id
|
||||
protected UUID id;
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
|
||||
if(this.id != null) {
|
||||
|
||||
throw new UnsupportedOperationException("ID is already defined");
|
||||
}
|
||||
|
||||
this.id = id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.uuid.repository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
import com.baeldung.uuid.model.Book;
|
||||
|
||||
public interface BookRepository extends MongoRepository<Book, UUID> {
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.uuid.repository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
import com.baeldung.uuid.model.UuidIdentifiedEntity;
|
||||
|
||||
|
||||
@NoRepositoryBean
|
||||
public interface CustomMongoRepository<T extends UuidIdentifiedEntity> extends MongoRepository<T, UUID> {
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.uuid.repository.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
|
||||
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
|
||||
|
||||
import com.baeldung.uuid.model.UuidIdentifiedEntity;
|
||||
import com.baeldung.uuid.repository.CustomMongoRepository;
|
||||
|
||||
|
||||
public class CustomMongoRepositoryImpl<T extends UuidIdentifiedEntity> extends SimpleMongoRepository<T, UUID> implements CustomMongoRepository<T> {
|
||||
|
||||
public CustomMongoRepositoryImpl(MongoEntityInformation<T, UUID> metadata, MongoOperations mongoOperations) {
|
||||
|
||||
super(metadata, mongoOperations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S save(S entity) {
|
||||
generateId(entity);
|
||||
return super.save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> List<S> saveAll(Iterable<S> entities) {
|
||||
entities.forEach(entity -> generateId(entity));
|
||||
return super.saveAll(entities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> S insert(S entity) {
|
||||
generateId(entity);
|
||||
return super.insert(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends T> List<S> insert(Iterable<S> entities) {
|
||||
entities.forEach(entity -> generateId(entity));
|
||||
return super.insert(entities);
|
||||
}
|
||||
|
||||
protected <S extends T> void generateId(S entity) {
|
||||
|
||||
if(entity != null && entity.getId() == null) {
|
||||
entity.setId(UUID.randomUUID());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.uuid;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.uuid.config.CustomRepositoryMongoConfig;
|
||||
import com.baeldung.uuid.model.Book;
|
||||
import com.baeldung.uuid.repository.BookRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* This test requires:
|
||||
* * mongodb instance running on the environment
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CustomRepositoryMongoConfig.class)
|
||||
public class CustomRepositoryLiveTest {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private MongoOperations mongoOps;
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
if (!mongoOps.collectionExists(Book.class)) {
|
||||
mongoOps.createCollection(Book.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mongoOps.dropCollection(Book.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInsertingBook_thenBookIsInserted() {
|
||||
final Book book = new Book();
|
||||
book.setTitle("The Lord of the Rings");
|
||||
book.setAuthor("JRR Tolkien");
|
||||
Book savedBook = bookRepository.save(book);
|
||||
|
||||
Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedBook.getId())), Book.class);
|
||||
|
||||
assertEquals(result.getTitle(), "The Lord of the Rings");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.uuid;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.uuid.config.EntityCallbackMongoConfig;
|
||||
import com.baeldung.uuid.model.Book;
|
||||
import com.baeldung.uuid.repository.BookRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* This test requires:
|
||||
* * mongodb instance running on the environment
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = EntityCallbackMongoConfig.class)
|
||||
public class EntityCallbackLiveTest {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private MongoOperations mongoOps;
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
if (!mongoOps.collectionExists(Book.class)) {
|
||||
mongoOps.createCollection(Book.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mongoOps.dropCollection(Book.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingArticle_thenArticleIsInserted() {
|
||||
final Book book = new Book();
|
||||
book.setTitle("The Lord of the Rings");
|
||||
book.setAuthor("JRR Tolkien");
|
||||
|
||||
Book savedArticle = bookRepository.save(book);
|
||||
|
||||
Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(result.getTitle(), "The Lord of the Rings");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.baeldung.uuid;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoOperations;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.baeldung.uuid.config.EventMongoConfig;
|
||||
import com.baeldung.uuid.model.Book;
|
||||
import com.baeldung.uuid.repository.BookRepository;
|
||||
|
||||
/**
|
||||
*
|
||||
* This test requires:
|
||||
* * mongodb instance running on the environment
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = EventMongoConfig.class)
|
||||
public class EventLiveTest {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@Autowired
|
||||
private MongoOperations mongoOps;
|
||||
|
||||
@Before
|
||||
public void testSetup() {
|
||||
if (!mongoOps.collectionExists(Book.class)) {
|
||||
mongoOps.createCollection(Book.class);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mongoOps.dropCollection(Book.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSavingArticle_thenArticleIsInserted() {
|
||||
final Book book = new Book();
|
||||
book.setTitle("The Lord of the Rings");
|
||||
book.setAuthor("JRR Tolkien");
|
||||
|
||||
Book savedArticle = bookRepository.save(book);
|
||||
|
||||
Book result = mongoOps.findOne(Query.query(Criteria.where("_id").is(savedArticle.getId())), Book.class);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(result.getTitle(), "The Lord of the Rings");
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.uuid.config;
|
||||
|
||||
import org.bson.UuidRepresentation;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.uuid.repository.impl.CustomMongoRepositoryImpl;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository", repositoryBaseClass = CustomMongoRepositoryImpl.class)
|
||||
public class CustomRepositoryMongoConfig {
|
||||
|
||||
@Bean
|
||||
public MongoClient mongo() throws Exception {
|
||||
final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test");
|
||||
final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
|
||||
.uuidRepresentation(UuidRepresentation.STANDARD)
|
||||
.applyConnectionString(connectionString).build();
|
||||
return MongoClients.create(mongoClientSettings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "test");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.uuid.config;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bson.UuidRepresentation;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.uuid.model.UuidIdentifiedEntity;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository")
|
||||
public class EntityCallbackMongoConfig {
|
||||
|
||||
@Bean
|
||||
public MongoClient mongo() throws Exception {
|
||||
final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test");
|
||||
final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
|
||||
.uuidRepresentation(UuidRepresentation.STANDARD)
|
||||
.applyConnectionString(connectionString).build();
|
||||
return MongoClients.create(mongoClientSettings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "test");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public BeforeConvertCallback<UuidIdentifiedEntity> beforeSaveCallback() {
|
||||
|
||||
return (entity, collection) -> {
|
||||
|
||||
if(entity.getId() == null) {
|
||||
entity.setId(UUID.randomUUID());
|
||||
}
|
||||
return entity;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.uuid.config;
|
||||
|
||||
import org.bson.UuidRepresentation;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
import com.baeldung.uuid.event.UuidIdentifiedEntityEventListener;
|
||||
import com.mongodb.ConnectionString;
|
||||
import com.mongodb.MongoClientSettings;
|
||||
import com.mongodb.client.MongoClient;
|
||||
import com.mongodb.client.MongoClients;
|
||||
|
||||
@Configuration
|
||||
@EnableMongoRepositories(basePackages = "com.baeldung.uuid.repository")
|
||||
public class EventMongoConfig {
|
||||
|
||||
@Bean
|
||||
public MongoClient mongo() throws Exception {
|
||||
final ConnectionString connectionString = new ConnectionString("mongodb://localhost:27017/test");
|
||||
final MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
|
||||
.uuidRepresentation(UuidRepresentation.STANDARD)
|
||||
.applyConnectionString(connectionString).build();
|
||||
return MongoClients.create(mongoClientSettings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate() throws Exception {
|
||||
return new MongoTemplate(mongo(), "test");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UuidIdentifiedEntityEventListener uuidIdentifiedEntityEventListener() {
|
||||
|
||||
return new UuidIdentifiedEntityEventListener();
|
||||
}
|
||||
}
|
|
@ -45,6 +45,7 @@
|
|||
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
|
||||
<module>spring-boot-jasypt</module>
|
||||
<module>spring-boot-keycloak</module>
|
||||
<module>spring-boot-keycloak-2</module>
|
||||
<module>spring-boot-libraries</module>
|
||||
<module>spring-boot-libraries-2</module>
|
||||
<module>spring-boot-libraries-comparison</module>
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
## Spring Boot Keycloak
|
||||
|
||||
This module contains articles about Keycloak in Spring Boot projects.
|
||||
|
||||
## Relevant articles:
|
||||
- [Disabling Keycloak Security in Spring Boot](https://www.baeldung.com/spring-keycloak-security-disable)
|
|
@ -0,0 +1,65 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.keycloak</groupId>
|
||||
<artifactId>spring-boot-keycloak-2</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<name>spring-boot-keycloak-2</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>This is a simple application demonstrating integration between Keycloak and Spring Boot.</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<keycloak-adapter-bom.version>15.0.2</keycloak-adapter-bom.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.keycloak</groupId>
|
||||
<artifactId>keycloak-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.keycloak.bom</groupId>
|
||||
<artifactId>keycloak-adapter-bom</artifactId>
|
||||
<version>${keycloak-adapter-bom.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,28 +1,25 @@
|
|||
package com.baeldung.disablingkeycloak;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.http.ResponseEntity;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ActiveProfiles("disablingkeycloak")
|
||||
public class DisablingKeycloakIntegrationTest {
|
||||
class DisablingKeycloakIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
public void givenUnauthenticated_whenGettingUser_shouldReturnUser() {
|
||||
void givenUnauthenticated_whenGettingUser_shouldReturnUser() {
|
||||
ResponseEntity<User> responseEntity = restTemplate.getForEntity("/users/1", User.class);
|
||||
|
||||
assertEquals(HttpStatus.SC_OK, responseEntity.getStatusCodeValue());
|
|
@ -10,4 +10,3 @@ This module contains articles about Keycloak in Spring Boot projects.
|
|||
- [Customizing Themes for Keycloak](https://www.baeldung.com/spring-keycloak-custom-themes)
|
||||
- [Securing SOAP Web Services With Keycloak](https://www.baeldung.com/soap-keycloak)
|
||||
- [Get Keycloak User ID in Spring](https://www.baeldung.com/spring-keycloak-get-user-id)
|
||||
- [Disabling Keycloak Security in Spring Boot](https://www.baeldung.com/spring-keycloak-security-disable)
|
||||
|
|
Loading…
Reference in New Issue