|
| 1 | +package itx.examples.mongodb; |
| 2 | + |
| 3 | +import com.mongodb.MongoClient; |
| 4 | +import com.mongodb.MongoClientOptions; |
| 5 | +import com.mongodb.client.MongoDatabase; |
| 6 | +import itx.examples.mongodb.dto.Role; |
| 7 | +import itx.examples.mongodb.services.DataException; |
| 8 | +import itx.examples.mongodb.services.RoleService; |
| 9 | +import itx.examples.mongodb.services.RoleServiceImpl; |
| 10 | +import org.bson.codecs.configuration.CodecRegistry; |
| 11 | +import org.bson.codecs.pojo.PojoCodecProvider; |
| 12 | +import org.testng.Assert; |
| 13 | +import org.testng.annotations.AfterClass; |
| 14 | +import org.testng.annotations.BeforeClass; |
| 15 | +import org.testng.annotations.Test; |
| 16 | + |
| 17 | +import java.util.Collection; |
| 18 | + |
| 19 | +import static org.bson.codecs.configuration.CodecRegistries.fromProviders; |
| 20 | +import static org.bson.codecs.configuration.CodecRegistries.fromRegistries; |
| 21 | + |
| 22 | +public class RoleServiceITTest { |
| 23 | + |
| 24 | + private MongoClient mongoClient; |
| 25 | + private MongoDatabase database; |
| 26 | + private RoleService roleService; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public void init() throws DataException { |
| 30 | + CodecRegistry pojoCodecRegistry = fromRegistries(MongoClient.getDefaultCodecRegistry(), |
| 31 | + fromProviders(PojoCodecProvider.builder().automatic(true).build())); |
| 32 | + mongoClient = new MongoClient( Utils.SERVER_HOSTNAME, MongoClientOptions.builder().codecRegistry(pojoCodecRegistry).build()); |
| 33 | + database = mongoClient.getDatabase(Utils.DB_NAME); |
| 34 | + roleService = new RoleServiceImpl(database); |
| 35 | + roleService.removeAll(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testRolesService() throws DataException { |
| 40 | + |
| 41 | + Collection<Role> roles = roleService.getRoles(); |
| 42 | + Assert.assertNotNull(roles); |
| 43 | + Assert.assertTrue(roles.isEmpty()); |
| 44 | + |
| 45 | + roleService.insertRole(new Role("1", "aaa")); |
| 46 | + roles = roleService.getRoles(); |
| 47 | + Assert.assertNotNull(roles); |
| 48 | + Assert.assertTrue(roles.size() == 1); |
| 49 | + |
| 50 | + roleService.insertRole(new Role("2", "bbb")); |
| 51 | + roles = roleService.getRoles(); |
| 52 | + Assert.assertNotNull(roles); |
| 53 | + Assert.assertTrue(roles.size() == 2); |
| 54 | + |
| 55 | + roleService.removeRole("1"); |
| 56 | + roles = roleService.getRoles(); |
| 57 | + Assert.assertNotNull(roles); |
| 58 | + Assert.assertTrue(roles.size() == 1); |
| 59 | + |
| 60 | + roleService.removeRole("2"); |
| 61 | + roles = roleService.getRoles(); |
| 62 | + Assert.assertNotNull(roles); |
| 63 | + Assert.assertTrue(roles.isEmpty()); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + @AfterClass |
| 68 | + public void destroy() { |
| 69 | + try { |
| 70 | + roleService.removeAll(); |
| 71 | + } catch (DataException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + mongoClient.close(); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments