22
33import com .mongodb .client .MongoClient ;
44import com .mongodb .client .MongoClients ;
5+ import com .mongodb .client .MongoCollection ;
6+ import org .bson .Document ;
7+ import org .bson .types .ObjectId ;
58import org .evomaster .client .java .controller .EmbeddedSutController ;
69import org .evomaster .client .java .controller .InstrumentedSutStarter ;
710import org .evomaster .client .java .controller .api .dto .AuthenticationDto ;
11+ import org .evomaster .client .java .controller .api .dto .JsonTokenPostLoginDto ;
812import org .evomaster .client .java .controller .api .dto .SutInfoDto ;
913import org .evomaster .client .java .sql .DbSpecification ;
1014import org .evomaster .client .java .controller .problem .ProblemInfo ;
1418import org .testcontainers .containers .GenericContainer ;
1519import sk .cyrilgavala .reservationsApi .ReservationsApi ;
1620
21+ import java .util .Arrays ;
1722import java .util .Collections ;
1823import java .util .List ;
1924import java .util .Map ;
@@ -45,12 +50,22 @@ public static void main(String[] args) {
4550 //https://www.mongodb.com/docs/drivers/java/sync/current/compatibility/
4651 private static final String MONGODB_VERSION = "4.4" ;
4752
48- private static final String MONGODB_DATABASE_NAME = "Reservations " ;
53+ private static final String MONGODB_DATABASE_NAME = "reservations-api " ;
4954
50- private static final GenericContainer mongodbContainer = new GenericContainer ("mongo:" + MONGODB_VERSION )
51- .withTmpFs (Collections .singletonMap ("/data/db" , "rw" ))
55+ // docker run -p 27017:27017 -e MONGODB_REPLICA_SET_MODE=primary -e ALLOW_EMPTY_PASSWORD=yes bitnami/mongodb:4.4
56+ // https://hub.docker.com/r/bitnami/mongodb
57+ // cannot use standard Mongo image, due ridiculous handling of transaction that requires a cluster...
58+
59+ private static final GenericContainer mongodbContainer = new GenericContainer ("bitnami/mongodb:" + MONGODB_VERSION )
60+ .withTmpFs (Collections .singletonMap ("/bitnami/mongodb" , "rw" ))
61+ .withEnv ("MONGODB_REPLICA_SET_MODE" , "primary" )
62+ .withEnv ("ALLOW_EMPTY_PASSWORD" , "yes" )
5263 .withExposedPorts (MONGODB_PORT );
5364
65+
66+ private static final String rawPassword = "bar123" ;
67+ private static final String hashedPassword = "$2a$10$nEDY5j731yXGnQHyM39PWurJWr1FukegmKYYarK5WOoAMmgDs6D3u" ;
68+
5469 private String mongoDbUrl ;
5570
5671 private MongoClient mongoClient ;
@@ -71,13 +86,12 @@ public String startSut() {
7186 mongoDbUrl = "mongodb://" + mongodbContainer .getContainerIpAddress () + ":" + mongodbContainer .getMappedPort (MONGODB_PORT ) + "/" + MONGODB_DATABASE_NAME ;
7287 mongoClient = MongoClients .create (mongoDbUrl );
7388
74-
75-
7689 ctx = SpringApplication .run (ReservationsApi .class ,
7790 new String []{"--server.port=0" ,
78- "--databaseUrl=" +mongoDbUrl ,
79- "--spring.data.mongodb.uri=" +mongoDbUrl ,
80- "--spring.cache.type=NONE"
91+ "--databaseUrl=" + mongoDbUrl ,
92+ "--spring.data.mongodb.uri=" + mongoDbUrl ,
93+ "--spring.cache.type=NONE" ,
94+ "--app.jwt.secret=abcdef012345678901234567890123456789abcdef012345678901234567890123456789"
8195 });
8296
8397 return "http://localhost:" + getSutPort ();
@@ -111,6 +125,35 @@ public String getPackagePrefixesToCover() {
111125 @ Override
112126 public void resetStateOfSUT () {
113127 mongoClient .getDatabase (MONGODB_DATABASE_NAME ).drop ();
128+
129+ mongoClient .getDatabase (MONGODB_DATABASE_NAME ).createCollection ("users" );
130+
131+ MongoCollection <Document > users = mongoClient .getDatabase (MONGODB_DATABASE_NAME ).getCollection ("users" );
132+ users .insertMany (Arrays .asList (
133+ new Document ()
134+ .append ("_id" , new ObjectId ())
135+ .append ("_class" , "sk.cyrilgavala.reservationsApi.model.User" )
136+ .append ("username" , "foo" )
137+ .append ("email" , "foo@foo.com" )
138+ .append ("password" , hashedPassword )
139+ .append ("role" , "USER" ),
140+ new Document ()
141+ .append ("_id" , new ObjectId ())
142+ .append ("_class" , "sk.cyrilgavala.reservationsApi.model.User" )
143+ .append ("username" , "bar" )
144+ .append ("email" , "bar@foo.com" )
145+ .append ("password" , hashedPassword )
146+ .append ("role" , "USER" ),
147+ new Document ()
148+ .append ("_id" , new ObjectId ())
149+ .append ("_class" , "sk.cyrilgavala.reservationsApi.model.User" )
150+ .append ("username" , "admin" )
151+ .append ("email" , "admin@foo.com" )
152+ .append ("password" , hashedPassword )
153+ .append ("role" , "ADMIN" )
154+ ));
155+
156+
114157 }
115158
116159 @ Override
@@ -121,11 +164,40 @@ public List<DbSpecification> getDbSpecifications() {
121164
122165 @ Override
123166 public List <AuthenticationDto > getInfoForAuthentication () {
124- //TODO might need to setup JWT headers here
125- return null ;
126- }
127-
128167
168+ return Arrays .asList (
169+ new AuthenticationDto () {{
170+ name = "admin" ;
171+ jsonTokenPostLogin = new JsonTokenPostLoginDto () {{
172+ userId = "admin" ;
173+ endpoint = "/api/user/login" ;
174+ jsonPayload = "{\" username\" :\" admin\" , \" password\" :\" " +rawPassword +"\" }" ;
175+ extractTokenField = "/accessToken" ;
176+ headerPrefix = "Bearer " ;
177+ }};
178+ }},
179+ new AuthenticationDto () {{
180+ name = "foo" ;
181+ jsonTokenPostLogin = new JsonTokenPostLoginDto () {{
182+ userId = "foo" ;
183+ endpoint = "/api/user/login" ;
184+ jsonPayload = "{\" username\" :\" foo\" , \" password\" :\" " +rawPassword +"\" }" ;
185+ extractTokenField = "/accessToken" ;
186+ headerPrefix = "Bearer " ;
187+ }};
188+ }},
189+ new AuthenticationDto () {{
190+ name = "bar" ;
191+ jsonTokenPostLogin = new JsonTokenPostLoginDto () {{
192+ userId = "bar" ;
193+ endpoint = "/api/user/login" ;
194+ jsonPayload = "{\" username\" :\" bar\" , \" password\" :\" " +rawPassword +"\" }" ;
195+ extractTokenField = "/accessToken" ;
196+ headerPrefix = "Bearer " ;
197+ }};
198+ }}
199+ );
200+ }
129201
130202
131203 @ Override
@@ -142,6 +214,8 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() {
142214 }
143215
144216 @ Override
145- public Object getMongoConnection () {return mongoClient ;}
217+ public Object getMongoConnection () {
218+ return mongoClient ;
219+ }
146220
147221}
0 commit comments