-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCreatingPrivateSellerListing.java
145 lines (120 loc) · 5.47 KB
/
CreatingPrivateSellerListing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//------------------------------------------------------------------
// Copyright 2020 mobile.de GmbH.
// Author/Developer: Philipp Bartsch
//
// This code is licensed under MIT license (see LICENSE for details)
//------------------------------------------------------------------
package scenarios;
import static org.assertj.core.api.Assertions.assertThat;
import ecg.move.sellermodel.listing.Contact;
import ecg.move.sellermodel.listing.EpsImage;
import ecg.move.sellermodel.listing.Listing;
import ecg.move.sellermodel.listing.Listing.CategoryEnum;
import ecg.move.sellermodel.listing.Listing.ConditionEnum;
import ecg.move.sellermodel.listing.Listing.FuelEnum;
import ecg.move.sellermodel.listing.Listing.VehicleClassEnum;
import ecg.move.sellermodel.listing.Location;
import ecg.move.sellermodel.listing.PriceNg;
import ecg.move.sellermodel.listing.Prices;
import ecg.move.sellermodel.listing.RefMake;
import ecg.move.sellermodel.listing.Seller;
import ecg.move.sellermodel.listing.Seller.TypeEnum;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.example.moveclient.ImageApiClient;
import org.example.moveclient.Main;
import org.example.moveclient.ReferenceDataApiClient;
import org.junit.jupiter.api.Test;
class CreatingPrivateSellerListing {
Main main = new Main();
ReferenceDataApiClient referenceDataApiClient = main.getReferenceDataApiClient();
ImageApiClient imageApiClient = main.getImageApiClient();
@Test
void create_listing() {
EpsImage image = uploadListingPicture();
RefMake carMake = pickMake();
Listing listing = assembleListing(image, carMake);
String listingId = postListing(listing);
System.out.println("You created a listing on MoVe: " + listingId);
}
private String postListing(Listing listing) {
// Listings usually have an id in the system of the caller, e.g. assigned by the local database.
// As the caller, you have to provide your own listing id, which on the MoVe side will be called
// "foreignId of the partner".
String madeUpListingId = UUID.randomUUID().toString();
String moveListingId = main.getListingLifecycleApiClient().publishListing(listing, madeUpListingId);
assertThat(moveListingId).isNotBlank();
return moveListingId;
}
private Listing assembleListing(EpsImage image, RefMake carMake) {
// the following listing contains only the most basic, mandatory fields (plus few extras)
Listing listing = new Listing();
listing.setEpsImages(List.of(image));
listing.setTitle("Awesome Car For Sale!");
listing.setHtmlDescription("<h1>Check this out!</h1><p>And here is some description</p>");
listing.setSeller(examplePrivateSeller());
listing.setPrices(examplePrices());
listing.setMake(carMake.getId());
listing.setFuel(FuelEnum.PETROL_PREMIUM); // fuel is defined as part of the API spec
listing.setVehicleClass(VehicleClassEnum.CAR);
listing.setCategory(CategoryEnum.LIMOUSINE);
listing.setCondition(ConditionEnum.NEW);
listing.setContact(exampleContact());
listing.setLocation(exampleLocation());
// feel free to extend this and set more details or a different vehicle class.
// Check the API spec and the response for validation errors. In some cases, the reference data API is
// able to provide allowed values ranges.
return listing;
}
private Contact exampleContact() {
Contact contact = new Contact();
contact.setLanguages(List.of("en"));
contact.setFirstName("John");
contact.setLastName("Doe");
contact.setEmail("[email protected]");
return contact;
}
private RefMake pickMake() {
// see what makes/models are allowed - this is subject to changes so make sure to validate often!
List<RefMake> allModels = referenceDataApiClient.getMakesAndModels(main.getConfig().getPartnerId(), "Car");
assertThat(allModels)
.describedAs("Move should return a (large) list of supported makes and models.")
.isNotEmpty();
// in reality you do not randomize this of course
return allModels.get(new Random().nextInt(allModels.size()));
}
private EpsImage uploadListingPicture() {
// Photo by Aldo Picaso from Pexels, see https://www.pexels.com/photo/yellow-car-toy-982093/
return imageApiClient.postPicture("example-car-picture.jpg")
.map(rsp -> {
EpsImage epsImage = new EpsImage();
epsImage.setBaseUrl(rsp.getImageBaseUrl());
return epsImage;
}).orElseThrow();
}
private Seller examplePrivateSeller() {
Seller seller = new Seller();
seller.setType(TypeEnum.FSBO); // private sellers are called "For Sale By Owner", FSBO
seller.setForeignId(UUID.randomUUID().toString());
return seller;
}
private Prices examplePrices() {
PriceNg price = new PriceNg();
price.setCurrency("CAD");
price.setIsNet(false);
price.setNegotiable(false);
price.setAmount("10000");
Prices prices = new Prices();
prices.setConsumerPrice(price);
return prices;
}
private Location exampleLocation() {
Location location = new Location();
location.setCity("Toronto");
location.setCountry("CA");
location.setStreet("10 Main St");
location.setZip("H0H 0H0");
return location;
}
}