21
21
import static org .junit .Assert .assertNull ;
22
22
import static org .junit .Assert .fail ;
23
23
24
+ import com .google .cloud .datastore .Datastore ;
25
+ import com .google .cloud .datastore .DatastoreOptions ;
26
+ import com .google .cloud .datastore .Entity ;
27
+ import com .google .cloud .datastore .Key ;
28
+ import com .google .cloud .datastore .Query ;
29
+ import com .google .cloud .datastore .QueryResults ;
30
+ import com .google .cloud .datastore .StructuredQuery ;
31
+ import com .google .cloud .datastore .testing .LocalDatastoreHelper ;
24
32
import com .google .common .collect .Iterators ;
25
- import com .google .gcloud .datastore .Datastore ;
26
- import com .google .gcloud .datastore .DatastoreOptions ;
27
- import com .google .gcloud .datastore .Entity ;
28
- import com .google .gcloud .datastore .Key ;
29
- import com .google .gcloud .datastore .Query ;
30
- import com .google .gcloud .datastore .QueryResults ;
31
- import com .google .gcloud .datastore .StructuredQuery ;
32
- import com .google .gcloud .datastore .testing .LocalGcdHelper ;
33
33
34
34
import org .junit .AfterClass ;
35
35
import org .junit .Before ;
41
41
42
42
public class UserServiceTest {
43
43
44
- private static final int PORT = LocalGcdHelper .findAvailablePort (LocalGcdHelper .DEFAULT_PORT );
45
- private static final String PROJECT_ID = LocalGcdHelper .DEFAULT_PROJECT_ID ;
44
+ private static final LocalDatastoreHelper HELPER = LocalDatastoreHelper .create (1.0 );
45
+ private static final DatastoreOptions DATASTORE_OPTIONS = HELPER .options ();
46
+ private static final Datastore DATASTORE = DATASTORE_OPTIONS .service ();
47
+ private static final String KIND = "DemoUser" ;
48
+ private static final UserService USER_SERVICE = new UserService (DATASTORE , KIND );
46
49
private static final String USER_ID = "myId" ;
47
50
private static final String USER_NAME = "myName" ;
48
51
private static final String USER_EMAIL = "my@email.com" ;
49
52
private static final User USER = new User (USER_ID , USER_NAME , USER_EMAIL );
50
- private static final String KIND = "DemoUser" ;
51
- private static final Key USER_KEY = Key .builder (PROJECT_ID , KIND , USER_ID ).build ();
53
+ private static final Key USER_KEY =
54
+ Key .builder (DATASTORE_OPTIONS . projectId () , KIND , USER_ID ).build ();
52
55
private static final Entity USER_RECORD = Entity .builder (USER_KEY )
53
56
.set ("id" , USER_ID )
54
57
.set ("name" , USER_NAME )
55
58
.set ("email" , USER_EMAIL )
56
59
.build ();
57
- private static LocalGcdHelper gcdHelper ;
58
- private static Datastore datastore ;
59
- private static UserService userService ;
60
60
61
61
@ BeforeClass
62
62
public static void beforeClass () throws IOException , InterruptedException {
63
- if (!LocalGcdHelper .isActive (PROJECT_ID , PORT )) {
64
- gcdHelper = LocalGcdHelper .start (PROJECT_ID , PORT , 1.0 );
65
- }
66
- datastore = DatastoreOptions .builder ()
67
- .projectId (PROJECT_ID )
68
- .host ("http://localhost:" + PORT )
69
- .build ()
70
- .service ();
71
- userService = new UserService (datastore , KIND );
63
+ HELPER .start ();
72
64
}
73
65
74
66
@ Before
75
67
public void setUp () {
76
68
StructuredQuery <Key > query = Query .keyQueryBuilder ().build ();
77
- QueryResults <Key > result = datastore .run (query );
78
- datastore .delete (Iterators .toArray (result , Key .class ));
79
- datastore .add (USER_RECORD );
69
+ QueryResults <Key > result = DATASTORE .run (query );
70
+ DATASTORE .delete (Iterators .toArray (result , Key .class ));
71
+ DATASTORE .add (USER_RECORD );
80
72
}
81
73
82
74
@ AfterClass
83
75
public static void afterClass () throws IOException , InterruptedException {
84
- if (gcdHelper != null ) {
85
- gcdHelper .stop ();
86
- }
76
+ HELPER .stop ();
87
77
}
88
78
89
79
@ Test
90
80
public void testGetAllUsers () {
91
- List <User > allUsers = userService .getAllUsers ();
81
+ List <User > allUsers = USER_SERVICE .getAllUsers ();
92
82
assertEquals (1 , allUsers .size ());
93
83
User actualUser = allUsers .get (0 );
94
84
assertEquals (USER .getId (), actualUser .getId ());
@@ -100,18 +90,18 @@ public void testGetAllUsers() {
100
90
public void testCreateUser () {
101
91
String name = "myNewName" ;
102
92
String email = "mynew@email.com" ;
103
- User actualUser = userService .createUser (name , email );
93
+ User actualUser = USER_SERVICE .createUser (name , email );
104
94
assertEquals (name , actualUser .getName ());
105
95
assertEquals (email , actualUser .getEmail ());
106
96
assertNotNull (actualUser .getId ());
107
97
try {
108
- userService .createUser (null , email );
98
+ USER_SERVICE .createUser (null , email );
109
99
fail ("Expected to fail because name is null." );
110
100
} catch (IllegalArgumentException e ) {
111
101
assertEquals ("Parameter 'name' cannot be empty" , e .getMessage ());
112
102
}
113
103
try {
114
- userService .createUser (name , null );
104
+ USER_SERVICE .createUser (name , null );
115
105
fail ("Expected to fail because email is null." );
116
106
} catch (IllegalArgumentException e ) {
117
107
assertEquals ("Parameter 'email' cannot be empty" , e .getMessage ());
@@ -120,27 +110,27 @@ public void testCreateUser() {
120
110
121
111
@ Test
122
112
public void testDeleteUser () {
123
- String result = userService .deleteUser (USER_ID );
113
+ String result = USER_SERVICE .deleteUser (USER_ID );
124
114
assertEquals ("ok" , result );
125
- assertNull (datastore .get (USER_KEY ));
115
+ assertNull (DATASTORE .get (USER_KEY ));
126
116
}
127
117
128
118
@ Test
129
119
public void testUpdateUser () {
130
120
String newName = "myNewName" ;
131
121
String newEmail = "mynew@email.com" ;
132
- User updatedUser = userService .updateUser (USER_ID , newName , newEmail );
122
+ User updatedUser = USER_SERVICE .updateUser (USER_ID , newName , newEmail );
133
123
assertEquals (USER_ID , updatedUser .getId ());
134
124
assertEquals (newName , updatedUser .getName ());
135
125
assertEquals (newEmail , updatedUser .getEmail ());
136
126
try {
137
- userService .updateUser (USER_ID , null , USER_EMAIL );
127
+ USER_SERVICE .updateUser (USER_ID , null , USER_EMAIL );
138
128
fail ("Expected to fail because name is null." );
139
129
} catch (IllegalArgumentException e ) {
140
130
assertEquals ("Parameter 'name' cannot be empty" , e .getMessage ());
141
131
}
142
132
try {
143
- userService .updateUser (USER_ID , USER_NAME , null );
133
+ USER_SERVICE .updateUser (USER_ID , USER_NAME , null );
144
134
fail ("Expected to fail because email is null." );
145
135
} catch (IllegalArgumentException e ) {
146
136
assertEquals ("Parameter 'email' cannot be empty" , e .getMessage ());
0 commit comments