Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions agent/src/main/java/com/ajay/mocklibimpl/agent/MockAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ public static void premain(String args, Instrumentation ins) {
String mode = System.getenv(MockConstants.HT_MODE);

// Mocking Rest Template
interceptMethod(MockConstants.REST_TEMPLATE, MockConstants.GET_OBJECT, mode, MockConstants.MOCK_DATA , ins);
interceptMethod("org.springframework.web.client.RestTemplate", MockConstants.GET_OBJECT, mode, MockConstants.MOCK_DATA , ins);

// Mocking Save Method of JDBC
interceptMethod(MockConstants.POST_REPO, MockConstants.SAVE, mode, new PostMock(), ins);
// interceptMethod(MockConstants.POST_REPO, MockConstants.SAVE, mode, new PostMock(), ins);

//Mocking JDBC
interceptMethod("org.springframework.jdbc.core.JdbcTemplate", "update", mode, new PostMock(), ins);
}

private static void interceptMethod(String type, String method, String mode, Object returnValue, Instrumentation inst) {
Expand Down
42 changes: 42 additions & 0 deletions agent/src/main/java/com/ajay/mocklibimpl/agent/MockData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ajay.mocklibimpl.agent;


public class MockData {
private String id;
private String name;
private String contents;

public MockData(String id, String name, String contents) {
this.id = id;
this.name = name;
this.contents = contents;
}

public MockData() {

}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}
}
42 changes: 42 additions & 0 deletions app/src/main/java/com/ajay/mocklibimpl/config/DbConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ajay.mocklibimpl.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DbConfig {

@Value("${spring.datasource.driver-class-name}")
private String driverClassName;

@Value("${spring.datasource.url}")
private String dbUrl;

@Value("${spring.datasource.username}")
private String username;

@Value("${spring.datasource.password}")
private String password;


@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setUrl(dbUrl);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.ajay.mocklibimpl.controller;


import com.ajay.mocklibimpl.dto.GetPostDetails;
import com.ajay.mocklibimpl.dto.RequestDTO;
import com.ajay.mocklibimpl.dto.ResponseDTO;
import com.ajay.mocklibimpl.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
Expand All @@ -32,4 +30,11 @@ public ResponseEntity<?> createNewPost(@RequestBody RequestDTO request) {
}
return new ResponseEntity<>(responseDTO, HttpStatus.OK);
}

@GetMapping("/get/all/posts")
public ResponseEntity<?> getAlPosts(){
GetPostDetails getPostDetails = postService.getAllPosts();
return new ResponseEntity<>(getPostDetails, HttpStatus.OK);
}

}
39 changes: 39 additions & 0 deletions app/src/main/java/com/ajay/mocklibimpl/dto/GetPostDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ajay.mocklibimpl.dto;

import com.ajay.mocklibimpl.dao.Post;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;


public class GetPostDetails {
@JsonProperty(value = "post_list")
private List<Post> postList;
@JsonProperty(value = "http_outbound")
private String httpOutbound;

public List<Post> getPostList() {
return postList;
}

public GetPostDetails() {
}

public String getHttpOutbound() {
return httpOutbound;
}

public GetPostDetails(List<Post> postList, String httpOutbound) {
this.postList = postList;
this.httpOutbound = httpOutbound;
}

public void setPostList(List<Post> postList) {
this.postList = postList;
}

public void setHttpOutbound(String httpOutbound) {
this.httpOutbound = httpOutbound;
}
}

17 changes: 16 additions & 1 deletion app/src/main/java/com/ajay/mocklibimpl/repo/PostRepo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package com.ajay.mocklibimpl.repo;

import com.ajay.mocklibimpl.dao.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

public interface PostRepo extends JpaRepository<Post, Long> {
@Repository
public class PostRepo {

@Autowired
private JdbcTemplate jdbcTemplate;

public void save(Post post) {

System.out.println("For Data Insertion");
String sql = "INSERT INTO posts (name, contents) VALUES (?, ?)";
int i = jdbcTemplate.update(sql, post.getName(), post.getContents());
System.out.println("Updation: " + i);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.ajay.mocklibimpl.service;

import com.ajay.mocklibimpl.dto.GetPostDetails;
import com.ajay.mocklibimpl.dto.RequestDTO;
import com.ajay.mocklibimpl.dto.ResponseDTO;

public interface PostService {
ResponseDTO createPost(RequestDTO request);
GetPostDetails getAllPosts();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import com.ajay.mocklibimpl.constants.Constants;
import com.ajay.mocklibimpl.dao.Post;
import com.ajay.mocklibimpl.dto.GetPostDetails;
import com.ajay.mocklibimpl.dto.RequestDTO;
import com.ajay.mocklibimpl.dto.ResponseDTO;
import com.ajay.mocklibimpl.service.PostService;
import com.ajay.mocklibimpl.repo.PostRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

Expand All @@ -18,14 +24,22 @@ public PostServiceImpl(PostRepo postRepo) {
this.postRepo = postRepo;
}

@Autowired
private PostRepo postRepository;

@Transactional
public void savePost(Post post) {
postRepository.save(post);
}

@Override
public ResponseDTO createPost(RequestDTO request) {
ResponseDTO responseDTO = new ResponseDTO();
try {
Post post = new Post();
post.setName(request.getPostName());
post.setContents(request.getPostContent());
post = postRepo.save(post);
postRepo.save(post);

responseDTO.setDbPost(post);

Expand All @@ -39,4 +53,20 @@ public ResponseDTO createPost(RequestDTO request) {
return responseDTO;
}
}

@Override
public GetPostDetails getAllPosts() {
GetPostDetails postDetails = new GetPostDetails();
System.out.println("Before Repo");
// postRepo.findAll();
List<Post> posts = new ArrayList<>();
System.out.println("Posts: " + posts);
postDetails.setPostList(posts);

RestTemplate restTemplate = new RestTemplate();
String apiResponse = restTemplate.getForObject(Constants.WORLD_TIME_URL, String.class);

postDetails.setHttpOutbound(apiResponse);
return postDetails;
}
}
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ services:
POSTGRES_DB: mocking_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345678
ports:
- "6666:5432"

app:
build:
context: .
dockerfile: Dockerfile
ports:
- "8080:8080"

depends_on:
- db
environment:
Expand Down