|
1 | 1 | package org.couchbase.quickstart.userProfile; |
2 | 2 |
|
| 3 | +import com.couchbase.client.core.error.CollectionExistsException; |
3 | 4 | import com.couchbase.client.core.error.DocumentNotFoundException; |
| 5 | +import com.couchbase.client.core.error.IndexExistsException; |
4 | 6 | import com.couchbase.client.java.Bucket; |
5 | 7 | import com.couchbase.client.java.Cluster; |
6 | 8 | import com.couchbase.client.java.json.JsonObject; |
| 9 | +import com.couchbase.client.java.manager.collection.CollectionManager; |
| 10 | +import com.couchbase.client.java.manager.collection.CollectionSpec; |
| 11 | +import com.couchbase.client.java.query.QueryResult; |
7 | 12 | import org.couchbase.quickstart.configs.CollectionNames; |
8 | 13 | import org.couchbase.quickstart.configs.DBProperties; |
9 | 14 | import org.couchbase.quickstart.models.Profile; |
@@ -50,6 +55,37 @@ public class UserProfileTest { |
50 | 55 |
|
51 | 56 |
|
52 | 57 | @Before |
| 58 | + public void init() { |
| 59 | + try { |
| 60 | + cluster.queryIndexes().createPrimaryIndex(prop.getBucketName()); |
| 61 | + } catch (Exception e) { |
| 62 | + System.out.println("Primary index already exists on bucket "+prop.getBucketName()); |
| 63 | + } |
| 64 | + |
| 65 | + CollectionManager collectionManager = bucket.collections(); |
| 66 | + try { |
| 67 | + CollectionSpec spec = CollectionSpec.create(CollectionNames.PROFILE, bucket.defaultScope().name()); |
| 68 | + collectionManager.createCollection(spec); |
| 69 | + } catch (CollectionExistsException e){ |
| 70 | + System.out.println(String.format("Collection <%s> already exists", CollectionNames.PROFILE)); |
| 71 | + } catch (Exception e) { |
| 72 | + System.out.println(String.format("Generic error <%s>",e.getMessage())); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + final QueryResult result = cluster.query("CREATE PRIMARY INDEX default_profile_index ON "+prop.getBucketName()+"._default."+ CollectionNames.PROFILE); |
| 77 | + for (JsonObject row : result.rowsAsObject()){ |
| 78 | + System.out.println(String.format("Index Creation Status %s",row.getObject("meta").getString("status"))); |
| 79 | + } |
| 80 | + } catch (IndexExistsException e){ |
| 81 | + System.out.println(String.format("Collection's primary index already exists")); |
| 82 | + } catch (Exception e){ |
| 83 | + System.out.println(String.format("General error <%s> when trying to create index ",e.getMessage())); |
| 84 | + } |
| 85 | + |
| 86 | + cluster.query("DELETE FROM "+prop.getBucketName()+"._default.profile "); |
| 87 | + } |
| 88 | + |
53 | 89 | @AfterEach |
54 | 90 | public void cleanDB() { |
55 | 91 | cluster.query("DELETE FROM "+prop.getBucketName()+"._default.profile "); |
|
0 commit comments