Skip to content

Commit a7497c4

Browse files
committed
implemented Data repository, DataSource Service & added unittest-case & implemented polling-service microservice
1 parent 78f1ea9 commit a7497c4

File tree

7 files changed

+256
-23
lines changed

7 files changed

+256
-23
lines changed

Diff for: polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataRepository.java

+45-3
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,54 @@
2525

2626
package com.iluwatar.polling;
2727

28+
import java.util.HashMap;
29+
import java.util.Map;
2830
import org.springframework.stereotype.Repository;
31+
import javax.annotation.PostConstruct;
32+
2933

3034
/**
3135
* Data repository to keep or store data.
32-
* */
36+
*/
3337
@Repository
3438
public final class DataRepository {
35-
// Simulate a repository for data access
36-
}
39+
40+
private final Map<Integer, String> dataStorage = new HashMap<>();
41+
42+
@PostConstruct
43+
public void init() {
44+
// Injecting dummy data at startup
45+
dataStorage.put(2, "Initial Dummy Data - two - 2");
46+
dataStorage.put(3, "Initial Dummy Data - three - 3");
47+
dataStorage.put(4, "Initial Dummy Data - four - 4");
48+
}
49+
50+
51+
/**
52+
* Save data to the repository.
53+
*/
54+
public void save(int id, String value) {
55+
dataStorage.put(id, value);
56+
}
57+
58+
/**
59+
* Retrieve data by ID.
60+
*/
61+
public String findById(int id) {
62+
return dataStorage.getOrDefault(id, "Data not found");
63+
}
64+
65+
/**
66+
* Delete data by ID.
67+
*/
68+
public void delete(int id) {
69+
dataStorage.remove(id);
70+
}
71+
72+
/**
73+
* Get all data.
74+
*/
75+
public Map<Integer, String> findAll() {
76+
return dataStorage;
77+
}
78+
}

Diff for: polling-publisher/polling-service/src/main/java/com/iluwatar/polling/DataSourceService.java

+40-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
package com.iluwatar.polling;
2727

2828
import java.util.List;
29+
import java.util.Map;
30+
import java.util.Random;
2931
import org.springframework.stereotype.Service;
3032

3133
/**
@@ -34,11 +36,43 @@
3436
@Service
3537
public class DataSourceService {
3638

37-
/**
38-
* Function to fetch data from DataRepository.
39-
* */
40-
public String fetchData() {
41-
// Simulate fetching data from a data source
42-
return "Sample Data " + System.currentTimeMillis();
39+
private final DataRepository repository;
40+
41+
public DataSourceService(DataRepository repository) {
42+
this.repository = repository;
43+
44+
// Start a separate thread to add data every 3 seconds
45+
new Thread(() -> {
46+
Random random = new Random();
47+
while (true) {
48+
try {
49+
Thread.sleep(3000); // Add data every 3 seconds
50+
int id = random.nextInt(100); // Random ID
51+
String value = "Auto-Data-" + id;
52+
this.addData(id, value);
53+
System.out.println("🔵 Data Added: " + id + " -> " + value);
54+
} catch (InterruptedException e) {
55+
Thread.currentThread().interrupt();
56+
break;
57+
}
58+
}
59+
}).start();
60+
61+
}
62+
63+
public void addData(int id, String value) {
64+
repository.save(id, value);
65+
}
66+
67+
public String getData(int id) {
68+
return repository.findById(id);
69+
}
70+
71+
public void removeData(int id) {
72+
repository.delete(id);
73+
}
74+
75+
public Map<Integer, String> getAllData() {
76+
return repository.findAll();
4377
}
4478
}

Diff for: polling-publisher/polling-service/src/main/java/com/iluwatar/polling/PollingScheduler.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.scheduling.annotation.Scheduled;
3030
import org.springframework.stereotype.Component;
31+
import java.util.Random;
3132

3233
/**
3334
* This class is responsible for scheduling polling tasks.
@@ -42,13 +43,18 @@ public class PollingScheduler {
4243
private KafkaProducer kafkaProducer;
4344

4445
/**
45-
* schedular for poll data on each 5 second.
46+
* Scheduler for poll data on each 5 second.
4647
* */
4748
@Scheduled(fixedRate = 5000) // Poll every 5 seconds
4849
public void pollDataSource() {
49-
String data = dataSourceService.fetchData();
50+
int id = new Random().nextInt(100); // Pick a random ID
51+
String data = dataSourceService.getData(id); // Get data from service
52+
5053
if (data != null) {
54+
System.out.println("🟢 Publishing Data: " + data);
5155
kafkaProducer.sendMessage(data);
56+
} else {
57+
System.out.println("🔴 No Data Found for ID: " + id);
5258
}
5359
}
5460
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# The MIT License
3+
# Copyright © 2014-2021 Ilkka Seppälä
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
#
23+
24+
server:
25+
port: 8081
26+
spring:
27+
kafka:
28+
consumer:
29+
group-id: subscriber-group

Diff for: polling-publisher/polling-service/src/test/java/com/iluwatar/polling/AppTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@
2525
package com.iluwatar.polling;
2626

2727
import org.junit.jupiter.api.Test;
28+
import org.springframework.boot.test.context.SpringBootTest;
2829

29-
public class AppTest {
30+
import static org.junit.jupiter.api.Assertions.*;
31+
32+
@SpringBootTest
33+
class AppTest {
3034

3135
@Test
32-
public void polling() {
33-
System.out.println(".(checking.. code is not running)");
36+
void polling() {
37+
assertDoesNotThrow(() -> App.main(new String[]{}));
3438
}
3539
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package com.iluwatar.polling;
26+
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
29+
30+
import static org.junit.jupiter.api.Assertions.*;
31+
32+
import java.util.Map;
33+
34+
class DataRepositoryTest {
35+
36+
private DataRepository repository;
37+
38+
@BeforeEach
39+
void setUp() {
40+
repository = new DataRepository(); // Initialize before each test
41+
}
42+
43+
@Test
44+
void testSaveAndFindById() {
45+
repository.save(1, "Test Data");
46+
47+
String result = repository.findById(1);
48+
49+
assertEquals("Test Data", result, "The retrieved data should match the stored value.");
50+
}
51+
52+
@Test
53+
void testFindById_NotFound() {
54+
String result = repository.findById(99);
55+
56+
assertEquals("Data not found", result, "Should return 'Data not found' for missing entries.");
57+
}
58+
59+
@Test
60+
void testDelete() {
61+
repository.save(2, "To be deleted");
62+
repository.delete(2);
63+
64+
String result = repository.findById(2);
65+
66+
assertEquals("Data not found", result, "Deleted data should not be retrievable.");
67+
}
68+
69+
@Test
70+
void testFindAll() {
71+
repository.save(1, "First");
72+
repository.save(2, "Second");
73+
74+
Map<Integer, String> allData = repository.findAll();
75+
76+
assertEquals(2, allData.size(), "The repository should contain two items.");
77+
assertTrue(allData.containsKey(1) && allData.containsKey(2), "Both keys should exist.");
78+
}
79+
}

Diff for: polling-publisher/polling-service/src/test/java/com/iluwatar/polling/DataSourceServiceTest.java

+48-9
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,57 @@
2525

2626
package com.iluwatar.polling;
2727

28-
import org.junit.jupiter.api.Test;
29-
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.api.*;
29+
import java.util.Map;
3030

31-
/*
32-
* this class contains the unit test for DataSoourceServiceTest class.
33-
*/
34-
public class DataSourceServiceTest {
31+
import static org.junit.jupiter.api.Assertions.*;
32+
33+
class DataSourceServiceTest {
34+
35+
private DataRepository repository;
36+
private DataSourceService service;
37+
38+
@BeforeEach
39+
void setUp() {
40+
repository = new DataRepository();
41+
service = new DataSourceService(repository);
42+
}
43+
44+
@Test
45+
void testAddData() {
46+
service.addData(1, "Test Data");
47+
48+
assertEquals("Test Data", repository.findById(1));
49+
}
50+
51+
@Test
52+
void testGetData() {
53+
repository.save(1, "Test Data");
54+
55+
String result = service.getData(1);
3556

36-
DataSourceService dataSourceService = new DataSourceService();
57+
assertEquals("Test Data", result, "The retrieved data should match.");
58+
}
3759

3860
@Test
39-
public void test1() {
40-
System.out.println(".(checking.. code is running)");
61+
void testRemoveData() {
62+
repository.save(2, "Some Data");
63+
64+
service.removeData(2);
65+
66+
assertEquals("Data not found", repository.findById(2), "Deleted data should not be retrievable.");
67+
68+
}
69+
70+
@Test
71+
void testGetAllData() {
72+
repository.save(1, "First");
73+
repository.save(2, "Second");
74+
75+
Map<Integer, String> result = service.getAllData();
76+
77+
assertEquals(2, result.size(), "Should return all stored data.");
78+
assertEquals("First", result.get(1), "Value for key 1 should be 'First'.");
79+
assertEquals("Second", result.get(2), "Value for key 2 should be 'Second'.");
4180
}
4281
}

0 commit comments

Comments
 (0)