Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c707e9

Browse files
committed
Test Hackerrank Inventory API
Get inventory from barcode
1 parent 8a50c48 commit 1c707e9

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package hackerrank.api;
2+
3+
public class BarcodeNotFoundException extends NullPointerException {
4+
public BarcodeNotFoundException(String errorMessage) {
5+
super(errorMessage);
6+
}
7+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package hackerrank.api;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.DeserializationFeature;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.sun.net.httpserver.HttpHandler;
7+
8+
import java.io.IOException;
9+
import java.net.MalformedURLException;
10+
import java.util.logging.Level;
11+
import java.util.logging.Logger;
12+
13+
public class BarcodeReader {
14+
15+
private static final String TAG = HttpHandler.class.getSimpleName();
16+
private final static Logger logger =
17+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
18+
private final String barcodeURI = "https://jsonmock.hackerrank.com/api/inventory?barcode=";
19+
private Inventory inventory;
20+
21+
public Inventory read(int barcode)
22+
throws BarcodeNotFoundException {
23+
Inventory[] inventories = null;
24+
25+
try {
26+
String inventoryJSONString = InputStreamConverter.toString(
27+
HttpRequests.get(barcodeURI+barcode))
28+
.toString();
29+
30+
inventories = getInventories(inventoryJSONString);
31+
32+
} catch (MalformedURLException e) {
33+
logger.log(Level.WARNING, TAG + " MalformedURLException: " + e.getMessage());
34+
} catch (IOException e) {
35+
logger.log(Level.WARNING, TAG + " IOException: " + e.getMessage());
36+
} catch (Exception e) {
37+
logger.log(Level.WARNING, TAG + " Exception: " + e.getMessage());
38+
}
39+
40+
if ( inventories.length == 0 )
41+
throw new BarcodeNotFoundException("Barcode Not Found!");
42+
43+
return inventories[0];
44+
}
45+
46+
private Inventory[] getInventories(String inventories)
47+
throws JsonProcessingException {
48+
49+
ObjectMapper objectMapper = new ObjectMapper();
50+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
51+
objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, false);
52+
objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
53+
54+
return objectMapper
55+
.readValue(inventories,
56+
Inventory[].class);
57+
}
58+
59+
}
60+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package hackerrank.api;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
class HttpRequests {
9+
10+
public static InputStream get(String url)
11+
throws IOException {
12+
InputStream inputStream = null;
13+
URL urlObject = new URL(url);
14+
HttpURLConnection conn = (HttpURLConnection) urlObject.openConnection();
15+
conn.setRequestMethod("GET");
16+
inputStream = conn.getInputStream();
17+
18+
return inputStream;
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package hackerrank.api;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import java.io.BufferedReader;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.InputStreamReader;
10+
11+
class InputStreamConverter {
12+
13+
public static String toString(InputStream is) {
14+
15+
StringBuilder sb = new StringBuilder();
16+
String jsonString = null;
17+
String line;
18+
19+
try (BufferedReader reader = new BufferedReader(
20+
new InputStreamReader(is))) {
21+
22+
while ((line = reader.readLine()) != null) {
23+
sb.append(line).append('\n');
24+
}
25+
26+
JSONObject json = new JSONObject(sb.toString());
27+
JSONArray dataArray = (JSONArray) json.get("data");
28+
jsonString = dataArray.toString();
29+
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
}
33+
34+
return jsonString;
35+
}
36+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package hackerrank.api;
2+
3+
import com.sun.net.httpserver.HttpHandler;
4+
5+
import java.util.logging.Logger;
6+
7+
public class Inventory {
8+
private final static Logger logger =
9+
Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
10+
private static final String TAG = HttpHandler.class.getSimpleName();
11+
12+
private final String URL = "https://jsonmock.hackerrank.com/api/inventory?barcode=";
13+
14+
String item;
15+
int price;
16+
int available;
17+
int discount;
18+
String category;
19+
int barcode;
20+
21+
public String getItem() {
22+
return item;
23+
}
24+
25+
public void setItem(String item) {
26+
this.item = item;
27+
}
28+
29+
public int getPrice() {
30+
return price;
31+
}
32+
33+
public void setPrice(int price) {
34+
this.price = price;
35+
}
36+
37+
public int getAvailable() {
38+
return available;
39+
}
40+
41+
public void setAvailable(int available) {
42+
this.available = available;
43+
}
44+
45+
public int getDiscount() {
46+
return discount;
47+
}
48+
49+
public void setDiscount(int discount) {
50+
this.discount = discount;
51+
}
52+
53+
public String getCategory() {
54+
return category;
55+
}
56+
57+
public void setCategory(String category) {
58+
this.category = category;
59+
}
60+
61+
public int getBarcode() {
62+
return barcode;
63+
}
64+
65+
public void setBarcode(int barcode) {
66+
this.barcode = barcode;
67+
}
68+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hackerranktest.api;
2+
3+
import hackerrank.api.BarcodeNotFoundException;
4+
import hackerrank.api.BarcodeReader;
5+
import hackerrank.api.Inventory;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class BarcodeReaderTest {
10+
11+
@Test
12+
void givenBarCodeListData() {
13+
14+
int barcode = 74001755;
15+
BarcodeReader obj = new BarcodeReader();
16+
Inventory actualArr = obj.read(barcode);
17+
18+
Assertions.assertEquals(74001755, actualArr.getBarcode());
19+
}
20+
21+
@Test
22+
void barcodeEmptyException() {
23+
Exception exception = Assertions.assertThrows(
24+
BarcodeNotFoundException.class, () -> {
25+
BarcodeReader br = new BarcodeReader();
26+
br.read(0);
27+
});
28+
29+
String expectedMessage = "Barcode Not Found!";
30+
String actualMessage = exception.getMessage();
31+
32+
Assertions.assertTrue(actualMessage.contains(expectedMessage));
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /