Skip to content

Commit cd67256

Browse files
committed
first commit
1 parent 0e02589 commit cd67256

8 files changed

+281
-4
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ repositories {
1414

1515
dependencies {
1616
implementation 'org.springframework.boot:spring-boot-starter-web'
17+
testCompile('org.springframework.boot:spring-boot-starter-test')
1718
testImplementation('org.springframework.boot:spring-boot-starter-test') {
1819
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
1920
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.beta.exception;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* @author Abhishek_Kumar
7+
* Class Description: POJO for respone entity in REST
8+
* */
9+
10+
public class CustomErrorResponse implements Serializable {
11+
12+
13+
/**
14+
*
15+
*/
16+
private static final long serialVersionUID = 5838540944024728920L;
17+
18+
private int status;
19+
private String error;
20+
21+
public int getStatus() {
22+
return status;
23+
}
24+
25+
public void setStatus(int status) {
26+
this.status = status;
27+
}
28+
29+
public String getError() {
30+
return error;
31+
}
32+
33+
public void setError(String error) {
34+
this.error = error;
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.beta.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.ControllerAdvice;
6+
import org.springframework.web.bind.annotation.ExceptionHandler;
7+
import org.springframework.web.context.request.WebRequest;
8+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
9+
/**
10+
* @author Abhishek_Kumar
11+
* Class Description: Handler for error/exception.
12+
* */
13+
14+
@ControllerAdvice
15+
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {
16+
17+
@ExceptionHandler(RecordNotFoundException.class)
18+
public ResponseEntity<CustomErrorResponse> customHandleNotFound(Exception ex, WebRequest request) {
19+
20+
CustomErrorResponse errors = new CustomErrorResponse();
21+
errors.setError(ex.getMessage());
22+
errors.setStatus(HttpStatus.NOT_FOUND.value());
23+
24+
return new ResponseEntity<>(errors, HttpStatus.NOT_FOUND);
25+
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.beta.exception;
2+
3+
/**
4+
* @author Abhishek_Kumar
5+
* Class Description: Record not found exception.
6+
* */
7+
8+
public class RecordNotFoundException extends RuntimeException {
9+
10+
public RecordNotFoundException(String msg) {
11+
super(msg);
12+
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
package com.beta.replyservice;
22

3+
import javax.naming.ServiceUnavailableException;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
36
import org.springframework.web.bind.annotation.GetMapping;
47
import org.springframework.web.bind.annotation.RestController;
58
import org.springframework.web.bind.annotation.PathVariable;
69

710
@RestController
811
public class ReplyController {
12+
13+
@Autowired
14+
ReplyHelperService replyHelperService;
915

1016
@GetMapping("/reply")
1117
public ReplyMessage replying() {
1218
return new ReplyMessage("Message is empty");
1319
}
1420

1521
@GetMapping("/reply/{message}")
16-
public ReplyMessage replying(@PathVariable String message) {
22+
public ReplyMessage replying(@PathVariable String message) throws ServiceUnavailableException {
23+
message = replyHelperService.businessLogic(message);
1724
return new ReplyMessage(message);
1825
}
26+
1927
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.beta.replyservice;
2+
3+
import java.math.BigInteger;
4+
import java.security.MessageDigest;
5+
import java.security.NoSuchAlgorithmException;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
import com.beta.exception.RecordNotFoundException;
11+
12+
/**
13+
* @author Abhishek_Kumar
14+
* Class Description: Helper Service for Reply Service Application.
15+
*
16+
* */
17+
18+
19+
@Service
20+
public class ReplyHelperService {
21+
22+
@Autowired
23+
ReplyController replyController;
24+
25+
public String businessLogic(String str) throws RecordNotFoundException {
26+
if (str == null) {
27+
throw new RecordNotFoundException("Invalid input");
28+
}
29+
30+
String sArray[] = str.split("-");
31+
if (sArray[0] == null || sArray[1] == null) {
32+
throw new RecordNotFoundException("Invalid input");
33+
}
34+
if (sArray[0].isEmpty() || sArray[1].isEmpty()) {
35+
throw new RecordNotFoundException("Invalid input");
36+
}
37+
String s = sArray[1];
38+
String reverseNum = reverseString(sArray[0]);
39+
40+
int num;
41+
try {
42+
num = Integer.parseInt(reverseNum);
43+
} catch (NumberFormatException e) {
44+
throw new RecordNotFoundException("Invalid input");
45+
}
46+
while (num > 0) {
47+
int i = num % 10;
48+
49+
if (i == 1) {
50+
s = reverseString(s);
51+
} else if (i == 2) {
52+
s = performMDEncryption(s);
53+
} else {
54+
throw new RecordNotFoundException("Invalid input");
55+
}
56+
57+
num = num / 10;
58+
59+
}
60+
61+
return s;
62+
}
63+
64+
public String performMDEncryption(String message) {
65+
try {
66+
67+
// Static getInstance method is called with hashing MD5
68+
MessageDigest md = MessageDigest.getInstance("MD5");
69+
70+
// digest() method is called to calculate message digest
71+
// of an input digest() return array of byte
72+
byte[] messageDigest = md.digest(message.getBytes());
73+
74+
// Convert byte array into signum representation
75+
BigInteger no = new BigInteger(1, messageDigest);
76+
77+
// Convert message digest into hex value
78+
String hashtext = no.toString(16);
79+
while (hashtext.length() < 32) {
80+
hashtext = "0" + hashtext;
81+
}
82+
return hashtext;
83+
}
84+
85+
// For specifying wrong message digest algorithms
86+
catch (NoSuchAlgorithmException e) {
87+
throw new RuntimeException(e);
88+
}
89+
90+
}
91+
92+
public String reverseString(String message) {
93+
// reverse string
94+
StringBuffer sb = new StringBuffer(message);
95+
return sb.reverse().toString();
96+
}
97+
98+
}

src/main/java/com/beta/replyservice/ReplyMessage.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.beta.replyservice;
22

3-
public class ReplyMessage {
3+
import java.io.Serializable;
44

5+
public class ReplyMessage implements Serializable{
6+
7+
/**
8+
*
9+
*/
10+
private static final long serialVersionUID = 1L;
11+
512
private final String message;
613

714
public ReplyMessage(String message) {

src/test/java/com/beta/replyservice/RestServiceApplicationTest.java

+85-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,95 @@
11
package com.beta.replyservice;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.Assert.assertEquals;
44

5+
import java.io.IOException;
6+
import java.io.UnsupportedEncodingException;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
import org.springframework.beans.factory.annotation.Autowired;
512
import org.springframework.boot.test.context.SpringBootTest;
13+
import org.springframework.http.MediaType;
14+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
15+
import org.springframework.test.context.web.WebAppConfiguration;
16+
import org.springframework.test.web.servlet.MockMvc;
17+
import org.springframework.test.web.servlet.MvcResult;
18+
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
19+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
20+
import org.springframework.web.context.WebApplicationContext;
21+
22+
import com.fasterxml.jackson.core.JsonParseException;
23+
import com.fasterxml.jackson.core.JsonProcessingException;
24+
import com.fasterxml.jackson.databind.JsonMappingException;
25+
import com.fasterxml.jackson.databind.ObjectMapper;
26+
27+
import junit.framework.Assert;
628

7-
@SpringBootTest
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33+
34+
@RunWith(SpringJUnit4ClassRunner.class)
35+
@SpringBootTest(classes = RestServiceApplication.class)
36+
@WebAppConfiguration
837
public class RestServiceApplicationTest {
938

39+
@Autowired
40+
ReplyController repl;
41+
42+
@Autowired
43+
ReplyHelperService helper;
44+
45+
protected MockMvc mvc;
46+
47+
@Autowired
48+
WebApplicationContext webApplicationContext;
49+
50+
@Before
51+
public void setUp() {
52+
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
53+
}
54+
55+
public String mapToJson(Object obj) throws JsonProcessingException {
56+
ObjectMapper objectMapper = new ObjectMapper();
57+
return objectMapper.writeValueAsString(obj);
58+
}
59+
60+
public <T> T mapFromJson(String json, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
61+
62+
ObjectMapper objectMapper = new ObjectMapper();
63+
return objectMapper.readValue(json, clazz);
64+
}
65+
66+
@Test
67+
public void replyingTest() {
68+
String uri = "/reply/{message}";
69+
String str = "1-abhi";
70+
71+
String inputJson = null;
72+
try {
73+
inputJson = mapToJson(str);
74+
} catch (JsonProcessingException e1) {
75+
// TODO Auto-generated catch block
76+
e1.printStackTrace();
77+
}
78+
79+
MvcResult mvcResult = null;
80+
try {
81+
MvcResult grantAuthzResult = mvc.perform(MockMvcRequestBuilders.get(uri, str)
82+
.contentType(MediaType.APPLICATION_JSON_VALUE).content(inputJson.toString()))
83+
.andExpect(status().isOk()).andReturn();
84+
85+
System.out.println(grantAuthzResult.getResponse().getContentAsString());
86+
87+
} catch (Exception e) {
88+
System.out.println("Error happened while testing reply api");
89+
}
90+
91+
}
92+
1093
@Test
1194
public void contextLoads() {
1295
}

0 commit comments

Comments
 (0)