Skip to content

Commit feae755

Browse files
committed
fix tests. export users. cleanup & refactoring
1 parent aa59ccf commit feae755

File tree

7 files changed

+126
-193
lines changed

7 files changed

+126
-193
lines changed

all.png

-42.2 KB
Loading

all.ucls

+29-141
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,81 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<class-diagram version="1.1.9" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3-
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4-
<class id="1" language="java" name="ro.dcsi.internship.OpenCsvFileUserDaoTest" project="usersync"
5-
file="/usersync/src/test/java/ro/dcsi/internship/OpenCsvFileUserDaoTest.java" binary="false" corner="BOTTOM_RIGHT">
6-
<position height="-1" width="-1" x="537" y="-148"/>
7-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8-
sort-features="false" accessors="true" visibility="true">
9-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10-
<operations public="true" package="true" protected="true" private="true" static="true"/>
11-
</display>
12-
</class>
13-
<class id="2" language="java" name="ro.dcsi.internship.ForgeRockUsersDao" project="usersync"
14-
file="/usersync/src/main/java/ro/dcsi/internship/ForgeRockUsersDao.java" binary="false" corner="BOTTOM_RIGHT">
15-
<position height="-1" width="-1" x="933" y="368"/>
2+
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="true" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="ro.dcsi.internship.Header" project="usersync"
5+
file="/usersync/src/main/java/ro/dcsi/internship/Header.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="297" width="185" x="42" y="464"/>
167
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
178
sort-features="false" accessors="true" visibility="true">
189
<attributes public="true" package="true" protected="true" private="true" static="true"/>
1910
<operations public="true" package="true" protected="true" private="true" static="true"/>
2011
</display>
2112
</class>
22-
<class id="3" language="java" name="ro.dcsi.internship.User" project="usersync"
13+
<class id="2" language="java" name="ro.dcsi.internship.User" project="usersync"
2314
file="/usersync/src/main/java/ro/dcsi/internship/User.java" binary="false" corner="BOTTOM_RIGHT">
24-
<position height="-1" width="-1" x="549" y="779"/>
15+
<position height="207" width="178" x="474" y="801"/>
2516
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2617
sort-features="false" accessors="true" visibility="true">
2718
<attributes public="true" package="true" protected="true" private="true" static="true"/>
2819
<operations public="true" package="true" protected="true" private="true" static="true"/>
2920
</display>
3021
</class>
31-
<class id="4" language="java" name="ro.dcsi.internship.OpenCsvFileUserDao" project="usersync"
32-
file="/usersync/src/main/java/ro/dcsi/internship/OpenCsvFileUserDao.java" binary="false" corner="BOTTOM_RIGHT">
33-
<position height="153" width="164" x="254" y="283"/>
22+
<class id="3" language="java" name="ro.dcsi.internship.App" project="usersync"
23+
file="/usersync/src/main/java/ro/dcsi/internship/App.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="117" width="233" x="121" y="37"/>
3425
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
3526
sort-features="false" accessors="true" visibility="true">
3627
<attributes public="true" package="true" protected="true" private="true" static="true"/>
3728
<operations public="true" package="true" protected="true" private="true" static="true"/>
3829
</display>
3930
</class>
40-
<interface id="5" language="java" name="ro.dcsi.internship.UserDao" project="usersync"
41-
file="/usersync/src/main/java/ro/dcsi/internship/UserDao.java" binary="false" corner="BOTTOM_RIGHT">
42-
<position height="-1" width="-1" x="541" y="540"/>
31+
<class id="4" language="java" name="ro.dcsi.internship.OpenCsvFileUserDaoTest" project="usersync"
32+
file="/usersync/src/test/java/ro/dcsi/internship/OpenCsvFileUserDaoTest.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="99" width="167" x="267" y="464"/>
4334
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
4435
sort-features="false" accessors="true" visibility="true">
4536
<attributes public="true" package="true" protected="true" private="true" static="true"/>
4637
<operations public="true" package="true" protected="true" private="true" static="true"/>
4738
</display>
48-
</interface>
49-
<class id="6" language="java" name="ro.dcsi.internship.CsvFileUserDaoTest" project="usersync"
39+
</class>
40+
<class id="5" language="java" name="ro.dcsi.internship.CsvFileUserDaoTest" project="usersync"
5041
file="/usersync/src/test/java/ro/dcsi/internship/CsvFileUserDaoTest.java" binary="false" corner="BOTTOM_RIGHT">
51-
<position height="-1" width="-1" x="977" y="-66"/>
42+
<position height="387" width="257" x="474" y="37"/>
5243
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
5344
sort-features="false" accessors="true" visibility="true">
5445
<attributes public="true" package="true" protected="true" private="true" static="true"/>
5546
<operations public="true" package="true" protected="true" private="true" static="true"/>
5647
</display>
5748
</class>
58-
<class id="7" language="java" name="ro.dcsi.internship.ForgeRockUserDaoTest" project="usersync"
49+
<class id="6" language="java" name="ro.dcsi.internship.ForgeRockUserDaoTest" project="usersync"
5950
file="/usersync/src/test/java/ro/dcsi/internship/ForgeRockUserDaoTest.java" binary="false" corner="BOTTOM_RIGHT">
60-
<position height="-1" width="-1" x="751" y="-147"/>
61-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
62-
sort-features="false" accessors="true" visibility="true">
63-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
64-
<operations public="true" package="true" protected="true" private="true" static="true"/>
65-
</display>
66-
</class>
67-
<class id="8" language="java" name="ro.dcsi.internship.App" project="usersync"
68-
file="/usersync/src/main/java/ro/dcsi/internship/App.java" binary="false" corner="BOTTOM_RIGHT">
69-
<position height="-1" width="-1" x="255" y="-105"/>
70-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
71-
sort-features="false" accessors="true" visibility="true">
72-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
73-
<operations public="true" package="true" protected="true" private="true" static="true"/>
74-
</display>
75-
</class>
76-
<class id="9" language="java" name="ro.dcsi.internship.CsvFileUserDao" project="usersync"
77-
file="/usersync/src/main/java/ro/dcsi/internship/CsvFileUserDao.java" binary="false" corner="BOTTOM_RIGHT">
78-
<position height="-1" width="-1" x="603" y="344"/>
51+
<position height="189" width="178" x="474" y="464"/>
7952
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8053
sort-features="false" accessors="true" visibility="true">
8154
<attributes public="true" package="true" protected="true" private="true" static="true"/>
8255
<operations public="true" package="true" protected="true" private="true" static="true"/>
8356
</display>
8457
</class>
85-
<class id="10" language="java" name="ro.dcsi.internship.Header" project="usersync"
86-
file="/usersync/src/main/java/ro/dcsi/internship/Header.java" binary="false" corner="BOTTOM_RIGHT">
87-
<position height="-1" width="-1" x="261" y="646"/>
88-
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
89-
sort-features="false" accessors="true" visibility="true">
90-
<attributes public="true" package="true" protected="true" private="true" static="true"/>
91-
<operations public="true" package="true" protected="true" private="true" static="true"/>
92-
</display>
93-
</class>
94-
<realization id="11">
95-
<end type="SOURCE" refId="2"/>
96-
<end type="TARGET" refId="5"/>
97-
</realization>
98-
<dependency id="12">
99-
<bendpoint x="777" y="283"/>
58+
<dependency id="7">
10059
<end type="SOURCE" refId="1"/>
101-
<end type="TARGET" refId="5"/>
102-
</dependency>
103-
<dependency id="13">
104-
<bendpoint x="756" y="283"/>
105-
<end type="SOURCE" refId="8"/>
106-
<end type="TARGET" refId="5"/>
107-
</dependency>
108-
<realization id="14">
109-
<end type="SOURCE" refId="4"/>
110-
<end type="TARGET" refId="5"/>
111-
</realization>
112-
<generalization id="15">
113-
<end type="SOURCE" refId="7"/>
114-
<end type="TARGET" refId="6"/>
115-
</generalization>
116-
<dependency id="16">
117-
<bendpoint x="820" y="700"/>
118-
<end type="SOURCE" refId="2"/>
119-
<end type="TARGET" refId="3"/>
120-
</dependency>
121-
<dependency id="17">
122-
<end type="SOURCE" refId="4"/>
123-
<end type="TARGET" refId="3"/>
124-
</dependency>
125-
<dependency id="18">
126-
<bendpoint x="798" y="283"/>
127-
<end type="SOURCE" refId="7"/>
128-
<end type="TARGET" refId="5"/>
129-
</dependency>
130-
<dependency id="19">
131-
<end type="SOURCE" refId="2"/>
132-
<end type="TARGET" refId="10"/>
133-
</dependency>
134-
<dependency id="20">
135-
<bendpoint x="1023" y="126"/>
136-
<bendpoint x="1023" y="283"/>
137-
<end type="SOURCE" refId="6"/>
138-
<end type="TARGET" refId="5"/>
139-
</dependency>
140-
<dependency id="21">
141-
<end type="SOURCE" refId="9"/>
142-
<end type="TARGET" refId="10"/>
143-
</dependency>
144-
<dependency id="22">
145-
<end type="SOURCE" refId="10"/>
146-
<end type="TARGET" refId="3"/>
147-
</dependency>
148-
<dependency id="23">
149-
<end type="SOURCE" refId="7"/>
15060
<end type="TARGET" refId="2"/>
15161
</dependency>
152-
<dependency id="24">
153-
<end type="SOURCE" refId="1"/>
154-
<end type="TARGET" refId="4"/>
155-
</dependency>
156-
<realization id="25">
157-
<end type="SOURCE" refId="9"/>
62+
<generalization id="8">
63+
<end type="SOURCE" refId="6"/>
15864
<end type="TARGET" refId="5"/>
159-
</realization>
160-
<generalization id="26">
161-
<end type="SOURCE" refId="1"/>
162-
<end type="TARGET" refId="6"/>
16365
</generalization>
164-
<dependency id="27">
165-
<end type="SOURCE" refId="9"/>
166-
<end type="TARGET" refId="3"/>
167-
</dependency>
168-
<dependency id="28">
169-
<end type="SOURCE" refId="8"/>
170-
<end type="TARGET" refId="4"/>
171-
</dependency>
172-
<dependency id="29">
173-
<bendpoint x="978" y="126"/>
66+
<dependency id="9">
17467
<end type="SOURCE" refId="6"/>
175-
<end type="TARGET" refId="9"/>
68+
<end type="TARGET" refId="2"/>
17669
</dependency>
177-
<dependency id="30">
70+
<dependency id="10">
71+
<bendpoint x="682" y="464"/>
17872
<end type="SOURCE" refId="5"/>
179-
<end type="TARGET" refId="3"/>
180-
</dependency>
181-
<dependency id="31">
182-
<bendpoint x="1044" y="126"/>
183-
<bendpoint x="1044" y="283"/>
184-
<end type="SOURCE" refId="6"/>
185-
<end type="TARGET" refId="3"/>
73+
<end type="TARGET" refId="2"/>
18674
</dependency>
187-
<dependency id="32">
75+
<generalization id="11">
18876
<end type="SOURCE" refId="4"/>
189-
<end type="TARGET" refId="10"/>
190-
</dependency>
77+
<end type="TARGET" refId="5"/>
78+
</generalization>
19179
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
19280
sort-features="false" accessors="true" visibility="true">
19381
<attributes public="true" package="true" protected="true" private="true" static="true"/>

src/main/java/ro/dcsi/internship/ForgeRockUsersDao.java

+49-34
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
public class ForgeRockUsersDao implements UserDao {
2222
private final static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ForgeRockUsersDao.class);
2323
private final HttpClient httpClient = HttpClientBuilder.create().build();
24-
private final String hostAndPort;
24+
public final String hostAndPort;
25+
public final String protocol;
2526

2627
public ForgeRockUsersDao(String hostAndPort) {
28+
this("http",hostAndPort);
29+
}
30+
31+
public ForgeRockUsersDao(String protocol, String hostAndPort) {
2732
logger.info("ForgeRockUsersDao initialized");
33+
this.protocol = protocol;
2834
this.hostAndPort = hostAndPort;
2935
}
3036

@@ -37,12 +43,12 @@ public String loadHeader(String csvFile) {
3743
public List<User> load(String csvFile) {
3844
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
3945
ArrayList<User> users = new ArrayList<>();
40-
String url = "http://"+hostAndPort+"/openidm/managed/user?_prettyPrint=true&_queryId=query-all";
46+
String url = protocol+"://" + hostAndPort + "/openidm/managed/user?_prettyPrint=true&_queryId=query-all";
4147
HttpGet request = new HttpGet(url);
4248
configure(request);
4349
try (CloseableHttpResponse response = httpClient.execute(request);) {
4450
String result = EntityUtils.toString(response.getEntity());
45-
System.out.println(result);
51+
logger.trace(result);
4652
// parse the json result and find the largest id
4753
JSONObject obj = new JSONObject(result);
4854
JSONArray arr = obj.getJSONArray("result");
@@ -73,18 +79,9 @@ public void save(List<User> users, String outputFileName) {
7379
}
7480
}
7581

76-
public void deleteIfExists(String username) {
77-
try {
78-
// TODO deletion if exists shouldn't be implemented by swallowing the exception
79-
delete(username);
80-
} catch (RuntimeException e) {
81-
logger.debug("Cannot delete user ["+username+"].",e);
82-
}
83-
}
84-
8582
public void delete(String username) {
8683
try {
87-
String url = "http://"+hostAndPort+"/openidm/managed/user/" + username;
84+
String url = protocol+"://" + hostAndPort + "/openidm/managed/user/" + username;
8885
HttpDelete request = new HttpDelete(url);
8986
configure(request);
9087
HttpResponse response = httpClient.execute(request);
@@ -102,7 +99,7 @@ public void delete(String username) {
10299

103100
private void postCreateUser(String id, String mail, String userName, String lastName, String givenName) {
104101
try {
105-
String url = "http://"+hostAndPort+"/openidm/managed/user/" + id;
102+
String url = protocol+"://" + hostAndPort + "/openidm/managed/user/" + id;
106103
HttpPut request = new HttpPut(url);
107104
StringEntity params = new StringEntity(
108105
"{" + "\"_id\": \"" + id + "\", " + "\"mail\": \"" + mail + "\", " + "\"userName\": \"" + userName
@@ -135,39 +132,57 @@ private void configure(HttpRequestBase request) {
135132
}
136133

137134
private int getLastUserId() {
138-
HttpClient httpClient = HttpClientBuilder.create().build(); // Use this instead
139135
int max = -1;
140-
try {
141-
String url = "http://"+hostAndPort+"/openidm/managed/user?_queryId=query-all-ids";
136+
try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();) {
137+
String url = protocol+"://" + hostAndPort + "/openidm/managed/user?_queryId=query-all-ids";
142138
HttpGet request = new HttpGet(url);
143139
configure(request);
144140

145-
HttpResponse response = httpClient.execute(request);
146-
147-
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
141+
try (CloseableHttpResponse response = httpClient.execute(request);) {
142+
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
148143

149-
StringBuffer result = new StringBuffer();
150-
String line = "";
151-
while ((line = rd.readLine()) != null) {
152-
result.append(line);
153-
}
144+
StringBuffer result = new StringBuffer();
145+
String line = "";
146+
while ((line = rd.readLine()) != null) {
147+
result.append(line);
148+
}
154149

155-
// parse the json result and find the largest id
156-
JSONObject obj = new JSONObject(result.toString());
157-
JSONArray arr = obj.getJSONArray("result");
150+
// parse the json result and find the largest id
151+
JSONObject obj = new JSONObject(result.toString());
152+
JSONArray arr = obj.getJSONArray("result");
158153

159-
for (int i = 0; i < arr.length(); i++) {
160-
try {
161-
if (Integer.parseInt(arr.getJSONObject(i).getString("_id")) > max) {
162-
max = Integer.parseInt(arr.getJSONObject(i).getString("_id"));
154+
for (int i = 0; i < arr.length(); i++) {
155+
try {
156+
if (Integer.parseInt(arr.getJSONObject(i).getString("_id")) > max) {
157+
max = Integer.parseInt(arr.getJSONObject(i).getString("_id"));
158+
}
159+
} catch (NumberFormatException e) {
160+
throw new RuntimeException(e);
163161
}
164-
} catch (NumberFormatException e) {
165-
throw new RuntimeException(e);
166162
}
167163
}
168164
} catch (IOException e) {
169165
throw new RuntimeException(e);
170166
}
171167
return max;
172168
}
169+
170+
public void forcedCreate(List<User> users) {
171+
deleteIfExists(users);
172+
save(users, "nu conteaza");
173+
}
174+
public void deleteIfExists(List<User> users) {
175+
for (User user : users) {
176+
deleteIfExists(user.username);
177+
}
178+
}
179+
public void deleteIfExists(String username) {
180+
try {
181+
// TODO deletion if exists shouldn't be implemented by swallowing
182+
// the exception
183+
delete(username);
184+
} catch (RuntimeException e) {
185+
logger.debug("Cannot delete user [" + username + "].", e);
186+
}
187+
}
173188
}

src/main/java/ro/dcsi/internship/User.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ public class User {
99
public final String lastname;
1010

1111
private Set<String> permisions;
12+
public User(String username, String email) {
13+
this(username,email,"NoFirstName","NoLastName");
14+
}
1215
public User(String username, String email, String firstname, String lastname) {
1316
this.username = username;
1417
this.email = email;
1518
this.firstname = firstname;
1619
this.lastname = lastname;
17-
}
18-
public User(String username, String email) {
19-
this.username = username;
20-
this.email = email;
21-
this.firstname = "NoFirstName";
22-
this.lastname = "NoLastName";
20+
if(username.contains("\n"))
21+
throw new RuntimeException("A username shouldn't contain End Of Lines. The username was ["+username+"]");
2322
}
2423
@Override
2524
public String toString() {

src/main/java/ro/dcsi/internship/UserDao.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import java.util.List;
44

55
public interface UserDao {
6-
String loadHeader(String csvFile);
76
List<User> load(String csvFile);
87
void save(List<User> users, String outputFileName);
8+
String loadHeader(String csvFile);
99
}

0 commit comments

Comments
 (0)