Skip to content

Commit 148de06

Browse files
author
Gopinath Langote
committed
iluwatar#348 - Data Tranfer Object : Implement Data Transfer Object pattern simple version.
1 parent 67d4477 commit 148de06

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
/**
28+
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to client
29+
* We can send related information together in POJO.
30+
* <p>
31+
* Dto will not have any business logic in it.
32+
*/
33+
public class CustomerDto {
34+
private String id;
35+
private String firstName;
36+
private String lastName;
37+
38+
/**
39+
* @param id customer id
40+
* @param firstName customer first name
41+
* @param lastName customer last name
42+
*/
43+
public CustomerDto(String id, String firstName, String lastName) {
44+
this.id = id;
45+
this.firstName = firstName;
46+
this.lastName = lastName;
47+
}
48+
49+
public String getId() {
50+
return id;
51+
}
52+
53+
public String getFirstName() {
54+
return firstName;
55+
}
56+
57+
public String getLastName() {
58+
return lastName;
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
import java.util.List;
28+
29+
/**
30+
* The resource class which serves customer information.
31+
* This class act as server in the demo. Which has all customer details.
32+
*/
33+
public class CustomerResource {
34+
private List<CustomerDto> customers;
35+
36+
/**
37+
* @param customers initialize resource with existing customers. Act as database.
38+
*/
39+
public CustomerResource(List<CustomerDto> customers) {
40+
this.customers = customers;
41+
}
42+
43+
/**
44+
* @return : all customers in list.
45+
*/
46+
public List<CustomerDto> getAllCustomers() {
47+
return customers;
48+
}
49+
50+
/**
51+
* @param customer save new customer to list.
52+
*/
53+
public void save(CustomerDto customer) {
54+
customers.add(customer);
55+
}
56+
57+
/**
58+
* @param customerId delete customer with id {@code customerId}
59+
*/
60+
public void delete(String customerId) {
61+
customers.removeIf(customer -> customer.getId().equals(customerId));
62+
}
63+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Gopinath Langote
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package com.iluwatar.datatransfer;
26+
27+
import org.junit.Test;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
import static org.junit.Assert.assertEquals;
33+
34+
/**
35+
* tests {@link CustomerResource}.
36+
*/
37+
public class CustomerResourceTest {
38+
@Test
39+
public void shouldGetAllCustomers() {
40+
CustomerDto customer = new CustomerDto("1", "David", "Roy");
41+
List<CustomerDto> customers = new ArrayList<>();
42+
customers.add(customer);
43+
44+
CustomerResource customerResource = new CustomerResource(customers);
45+
46+
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
47+
48+
assertEquals(allCustomers.size(), 1);
49+
assertEquals(allCustomers.get(0).getId(), "1");
50+
assertEquals(allCustomers.get(0).getFirstName(), "David");
51+
assertEquals(allCustomers.get(0).getLastName(), "Roy");
52+
}
53+
54+
@Test
55+
public void shouldSaveCustomer() {
56+
CustomerDto customer = new CustomerDto("1", "David", "Roy");
57+
CustomerResource customerResource = new CustomerResource(new ArrayList<>());
58+
59+
customerResource.save(customer);
60+
61+
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
62+
assertEquals(allCustomers.get(0).getId(), "1");
63+
assertEquals(allCustomers.get(0).getFirstName(), "David");
64+
assertEquals(allCustomers.get(0).getLastName(), "Roy");
65+
}
66+
67+
@Test
68+
public void shouldDeleteCustomer() {
69+
CustomerDto customer = new CustomerDto("1", "David", "Roy");
70+
List<CustomerDto> customers = new ArrayList<>();
71+
customers.add(customer);
72+
73+
CustomerResource customerResource = new CustomerResource(customers);
74+
75+
customerResource.delete(customer.getId());
76+
77+
List<CustomerDto> allCustomers = customerResource.getAllCustomers();
78+
assertEquals(allCustomers.size(), 0);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)