Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
musab.bozkurt committed Feb 13, 2024
1 parent e380638 commit ffec9fa
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mb.livedataservice.client.jsonplaceholder.request.PostRequest;
import com.mb.livedataservice.client.jsonplaceholder.response.PostResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.DeleteExchange;
Expand All @@ -19,7 +20,7 @@ public interface DeclarativeJSONPlaceholderRestClient {
PostResponse getPost(@PathVariable Integer id);

@DeleteExchange("/posts/{id}")
void deletePost(@PathVariable Integer id);
ResponseEntity<Void> deletePost(@PathVariable Integer id);

@PostExchange("/posts")
PostResponse createPost(@RequestBody PostRequest post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public class JSONPlaceholderRestClient {

private final RestClient restClient;

public JSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties JSONPlaceholderClientProperties) {
public JSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties placeholderClientProperties) {
JdkClientHttpRequestFactory jdkClientHttpRequestFactory = new JdkClientHttpRequestFactory();
jdkClientHttpRequestFactory.setReadTimeout(Duration.ofSeconds(15));

this.restClient = builder
.baseUrl(JSONPlaceholderClientProperties.getUrl())
.baseUrl(placeholderClientProperties.getUrl())
.requestFactory(jdkClientHttpRequestFactory)
.requestInterceptor(new BasicAuthenticationInterceptor(JSONPlaceholderClientProperties.getClientId(), JSONPlaceholderClientProperties.getClientSecret()))
.requestInterceptor(new BasicAuthenticationInterceptor(placeholderClientProperties.getClientId(), placeholderClientProperties.getClientSecret()))
.defaultStatusHandler(HttpStatusCode::isError, (request, response) -> {
throw new BaseException(new ErrorCode() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
public class DeclarativeHttpInterfaces {

@Bean
public DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties JSONPlaceholderClientProperties) {
public DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties placeholderClientProperties) {
JdkClientHttpRequestFactory jdkClientHttpRequestFactory = new JdkClientHttpRequestFactory();
jdkClientHttpRequestFactory.setReadTimeout(Duration.ofSeconds(15));

RestClient restClient = builder
.baseUrl(JSONPlaceholderClientProperties.getUrl())
.baseUrl(placeholderClientProperties.getUrl())
.requestFactory(jdkClientHttpRequestFactory)
.requestInterceptor(new BasicAuthenticationInterceptor(JSONPlaceholderClientProperties.getClientId(), JSONPlaceholderClientProperties.getClientSecret()))
.requestInterceptor(new BasicAuthenticationInterceptor(placeholderClientProperties.getClientId(), placeholderClientProperties.getClientSecret()))
.defaultStatusHandler(HttpStatusCode::isError, (request, response) -> {
throw new BaseException(new ErrorCode() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@EnableConfigurationProperties({SwaggerConfig.class, SwaggerConfig.SwaggerServices.class})
public class SwaggerConfig {

public List<SwaggerServices> services;
private List<SwaggerServices> services;

@Bean
public OpenAPI springShopOpenAPI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ public class ScoreBoard extends BaseEntity {
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
Class<?> oEffectiveClass = o instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
ScoreBoard that = (ScoreBoard) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
return this instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/mb/livedataservice/data/model/Tutorial.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public class Tutorial {
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
Class<?> oEffectiveClass = o instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
Tutorial tutorial = (Tutorial) o;
return Objects.equals(getId(), tutorial.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
return this instanceof HibernateProxy hibernateProxy ? hibernateProxy.getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public Map<String, Object> verifyToken(ApiPlayIntegrityTokenResult tokenResult)
map.put(SUCCEEDED, true);
} catch (Exception e) {
log.error("Exception occurred while verifying play integrity token. Exception: {}", ExceptionUtils.getStackTrace(e));
if (e instanceof GoogleJsonResponseException) {
map.put(RESULT, ((GoogleJsonResponseException) e).getDetails());
if (e instanceof GoogleJsonResponseException googleJsonResponseException) {
map.put(RESULT, googleJsonResponseException.getDetails());
} else {
map.put(RESULT, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class TutorialControllerTest extends BaseUnitTest {
private TestRestTemplate restTemplate;

@Autowired
private JSONPlaceholderRestClient JSONPlaceholderRestClient;
private JSONPlaceholderRestClient placeholderRestClient;

@Autowired
private DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient;
Expand Down Expand Up @@ -122,7 +122,7 @@ void shouldGetTutorialWhenValidTutorialId() {
void shouldGetAllTutorials() {
Tutorial[] tutorials = restTemplate.getForObject("/api/tutorials", Tutorial[].class);

assertThat(tutorials.length).isGreaterThan(100);
assertThat(tutorials).hasSizeGreaterThan(100);
}

@Test
Expand Down Expand Up @@ -168,7 +168,7 @@ void shouldDeleteAllTutorials() {
void shouldGetAllTutorialsByPublishedTrue() {
Tutorial[] tutorials = restTemplate.getForObject("/api/tutorials/published", Tutorial[].class);

assertThat(tutorials.length).isGreaterThan(100);
assertThat(tutorials).hasSizeGreaterThan(100);
}

@Test
Expand All @@ -188,18 +188,18 @@ void shouldGetAllTutorialsByFilter() {

assertThat(tutorials.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(tutorialsBody).isNotNull();
assertThat(tutorialsBody.getNumberOfElements()).isGreaterThanOrEqualTo(1);
assertThat(tutorialsBody.getNumberOfElements()).isPositive();
}

@Test
void shouldFindAllTodos() {
String todos = JSONPlaceholderRestClient.findAllTodos();
String todos = placeholderRestClient.findAllTodos();
assertNotNull(todos);
}

@Test
void shouldFindAllPosts() {
List<PostResponse> posts = JSONPlaceholderRestClient.findAllPosts();
List<PostResponse> posts = placeholderRestClient.findAllPosts();
assertNotNull(posts);
assertThat(posts).hasSizeGreaterThanOrEqualTo(1);
}
Expand All @@ -208,39 +208,40 @@ void shouldFindAllPosts() {
void shouldCreatePost() {
PostRequest newPost = new PostRequest(5, null, "The Lord of the Rings", null);

PostResponse post = JSONPlaceholderRestClient.createPost(newPost);
PostResponse post = placeholderRestClient.createPost(newPost);
assertNotNull(post);
assertThat(post.userId()).isEqualTo(newPost.userId());
assertThat(post.title()).isEqualTo(newPost.title());
}

@Test
void shouldGetPostById() {
PostResponse post = JSONPlaceholderRestClient.getPostById(5);
PostResponse post = placeholderRestClient.getPostById(5);
assertNotNull(post);
}

@Test
public void shouldGetAllPosts() {
void shouldGetAllPosts() {
List<PostResponse> posts = declarativeJSONPlaceholderRestClient.getAllPosts();
assertThat(posts.size()).isEqualTo(100);
assertThat(posts).hasSize(100);
}

@Test
public void shouldGetSinglePost() {
void shouldGetSinglePost() {
PostResponse post = declarativeJSONPlaceholderRestClient.getPost(5);
assertThat(post.id()).isEqualTo(5);
}

@Test
public void shouldDeleteSinglePost() {
declarativeJSONPlaceholderRestClient.deletePost(5);
void shouldDeleteSinglePost() {
ResponseEntity<Void> response = declarativeJSONPlaceholderRestClient.deletePost(5);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}

@Test
public void shouldCreateNewPost() {
void shouldCreateNewPost() {
PostRequest newPost = new PostRequest(1, null, "new title", "new body");
PostResponse post = declarativeJSONPlaceholderRestClient.createPost(newPost);
assertThat("new title").isEqualTo(post.title());
assertThat(post.title()).isEqualTo("new title");
}
}

0 comments on commit ffec9fa

Please sign in to comment.