Commit db19dd25efe52108d71d78e6e8d051e10598a1df
Exists in
master
Merge branch '2.5' of https://github.com/demoiselle/framework.git into 2.5
Showing
2 changed files
with
50 additions
and
46 deletions
Show diff stats
archetype/html-rest/src/main/resources/archetype-resources/src/test/java/rest/BookmarkRESTTest.java
| ... | ... | @@ -15,8 +15,9 @@ import java.util.List; |
| 15 | 15 | import java.util.Set; |
| 16 | 16 | |
| 17 | 17 | import org.apache.commons.codec.binary.Base64; |
| 18 | +import org.apache.commons.configuration.Configuration; | |
| 19 | +import org.apache.commons.configuration.PropertiesConfiguration; | |
| 18 | 20 | import org.apache.http.HttpEntity; |
| 19 | -import org.apache.http.HttpHost; | |
| 20 | 21 | import org.apache.http.client.ClientProtocolException; |
| 21 | 22 | import org.apache.http.client.entity.EntityBuilder; |
| 22 | 23 | import org.apache.http.client.methods.CloseableHttpResponse; |
| ... | ... | @@ -38,19 +39,21 @@ import br.gov.frameworkdemoiselle.PreconditionFailedException; |
| 38 | 39 | |
| 39 | 40 | public class BookmarkRESTTest { |
| 40 | 41 | |
| 41 | - private HttpHost host; | |
| 42 | - | |
| 43 | 42 | private static final String BASIC_CREDENTIALS = "Basic " + Base64.encodeBase64String("admin:admin".getBytes()); |
| 44 | 43 | |
| 45 | 44 | private CloseableHttpClient client; |
| 46 | 45 | |
| 47 | 46 | private ObjectMapper mapper; |
| 48 | 47 | |
| 48 | + private String url; | |
| 49 | + | |
| 49 | 50 | @Before |
| 50 | - public void before() { | |
| 51 | - host = new HttpHost("localhost", 8080, "http"); | |
| 51 | + public void before() throws Exception { | |
| 52 | 52 | client = HttpClientBuilder.create().build(); |
| 53 | 53 | mapper = new ObjectMapper(); |
| 54 | + | |
| 55 | + Configuration config = new PropertiesConfiguration("demoiselle.properties"); | |
| 56 | + url = config.getString("services.url"); | |
| 54 | 57 | } |
| 55 | 58 | |
| 56 | 59 | @After |
| ... | ... | @@ -63,8 +66,8 @@ public class BookmarkRESTTest { |
| 63 | 66 | HttpGet request; |
| 64 | 67 | CloseableHttpResponse response; |
| 65 | 68 | |
| 66 | - request = new HttpGet("/a15/api/bookmark"); | |
| 67 | - response = client.execute(host, request); | |
| 69 | + request = new HttpGet(url + "/bookmark"); | |
| 70 | + response = client.execute(request); | |
| 68 | 71 | response.close(); |
| 69 | 72 | List<Bookmark> listAll = mapper.readValue(response.getEntity().getContent(), |
| 70 | 73 | new TypeReference<List<Bookmark>>() { |
| ... | ... | @@ -72,8 +75,8 @@ public class BookmarkRESTTest { |
| 72 | 75 | assertEquals(SC_OK, response.getStatusLine().getStatusCode()); |
| 73 | 76 | |
| 74 | 77 | String filter = "po"; |
| 75 | - request = new HttpGet("/a15/api/bookmark?q=" + filter); | |
| 76 | - response = client.execute(host, request); | |
| 78 | + request = new HttpGet(url + "/bookmark?q=" + filter); | |
| 79 | + response = client.execute(request); | |
| 77 | 80 | response.close(); |
| 78 | 81 | List<Bookmark> filteredList = mapper.readValue(response.getEntity().getContent(), |
| 79 | 82 | new TypeReference<List<Bookmark>>() { |
| ... | ... | @@ -91,8 +94,8 @@ public class BookmarkRESTTest { |
| 91 | 94 | public void loadSuccessful() throws Exception { |
| 92 | 95 | Long id = parseEntity(createSample().getEntity(), Long.class); |
| 93 | 96 | |
| 94 | - HttpGet request = new HttpGet("/a15/api/bookmark/" + id); | |
| 95 | - CloseableHttpResponse response = client.execute(host, request); | |
| 97 | + HttpGet request = new HttpGet(url + "/bookmark/" + id); | |
| 98 | + CloseableHttpResponse response = client.execute(request); | |
| 96 | 99 | response.close(); |
| 97 | 100 | assertEquals(SC_OK, response.getStatusLine().getStatusCode()); |
| 98 | 101 | |
| ... | ... | @@ -106,8 +109,8 @@ public class BookmarkRESTTest { |
| 106 | 109 | |
| 107 | 110 | @Test |
| 108 | 111 | public void loadFailed() throws ClientProtocolException, IOException { |
| 109 | - HttpGet get = new HttpGet("/a15/api/bookmark/99999999"); | |
| 110 | - CloseableHttpResponse response = client.execute(host, get); | |
| 112 | + HttpGet request = new HttpGet(url + "/bookmark/99999999"); | |
| 113 | + CloseableHttpResponse response = client.execute(request); | |
| 111 | 114 | response.close(); |
| 112 | 115 | assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode()); |
| 113 | 116 | } |
| ... | ... | @@ -116,9 +119,9 @@ public class BookmarkRESTTest { |
| 116 | 119 | public void deleteSuccessful() throws Exception { |
| 117 | 120 | Long id = parseEntity(createSample().getEntity(), Long.class); |
| 118 | 121 | |
| 119 | - HttpDelete request = new HttpDelete("/a15/api/bookmark/" + id); | |
| 122 | + HttpDelete request = new HttpDelete(url + "/bookmark/" + id); | |
| 120 | 123 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 121 | - CloseableHttpResponse response = client.execute(host, request); | |
| 124 | + CloseableHttpResponse response = client.execute(request); | |
| 122 | 125 | response.close(); |
| 123 | 126 | assertEquals(SC_NO_CONTENT, response.getStatusLine().getStatusCode()); |
| 124 | 127 | } |
| ... | ... | @@ -129,15 +132,15 @@ public class BookmarkRESTTest { |
| 129 | 132 | CloseableHttpResponse response; |
| 130 | 133 | |
| 131 | 134 | Long id = parseEntity(createSample().getEntity(), Long.class); |
| 132 | - request = new HttpDelete("/a15/api/bookmark/" + id); | |
| 133 | - response = client.execute(host, request); | |
| 135 | + request = new HttpDelete(url + "/bookmark/" + id); | |
| 136 | + response = client.execute(request); | |
| 134 | 137 | response.close(); |
| 135 | 138 | assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); |
| 136 | 139 | destroySample(id); |
| 137 | 140 | |
| 138 | - request = new HttpDelete("/a15/api/bookmark/99999999"); | |
| 141 | + request = new HttpDelete(url + "/bookmark/99999999"); | |
| 139 | 142 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 140 | - response = client.execute(host, request); | |
| 143 | + response = client.execute(request); | |
| 141 | 144 | response.close(); |
| 142 | 145 | assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode()); |
| 143 | 146 | } |
| ... | ... | @@ -150,12 +153,12 @@ public class BookmarkRESTTest { |
| 150 | 153 | Long id = parseEntity(response.getEntity(), Long.class); |
| 151 | 154 | assertNotNull(id); |
| 152 | 155 | |
| 153 | - String expectedLocation = host.toString() + "/a15/api/bookmark/" + id; | |
| 156 | + String expectedLocation = url + "/bookmark/" + id; | |
| 154 | 157 | String returnedLocation = response.getHeaders("Location")[0].getValue(); |
| 155 | 158 | assertEquals(expectedLocation, returnedLocation); |
| 156 | 159 | |
| 157 | 160 | HttpGet request = new HttpGet(returnedLocation); |
| 158 | - response = client.execute(host, request); | |
| 161 | + response = client.execute(request); | |
| 159 | 162 | response.close(); |
| 160 | 163 | |
| 161 | 164 | destroySample(id); |
| ... | ... | @@ -172,19 +175,19 @@ public class BookmarkRESTTest { |
| 172 | 175 | bookmark = new Bookmark(); |
| 173 | 176 | bookmark.setDescription("Google"); |
| 174 | 177 | bookmark.setLink("http://google.com"); |
| 175 | - request = new HttpPost("/a15/api/bookmark"); | |
| 178 | + request = new HttpPost(url + "/bookmark"); | |
| 176 | 179 | request.setEntity(createEntity(bookmark)); |
| 177 | 180 | request.addHeader("Content-Type", "application/json"); |
| 178 | - response = client.execute(host, request); | |
| 181 | + response = client.execute(request); | |
| 179 | 182 | response.close(); |
| 180 | 183 | assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); |
| 181 | 184 | |
| 182 | 185 | bookmark = new Bookmark(); |
| 183 | - request = new HttpPost("/a15/api/bookmark"); | |
| 186 | + request = new HttpPost(url + "/bookmark"); | |
| 184 | 187 | request.setEntity(createEntity(bookmark)); |
| 185 | 188 | request.addHeader("Content-Type", "application/json"); |
| 186 | 189 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 187 | - response = client.execute(host, request); | |
| 190 | + response = client.execute(request); | |
| 188 | 191 | response.close(); |
| 189 | 192 | assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); |
| 190 | 193 | violations = mapper.readValue(response.getEntity().getContent(), |
| ... | ... | @@ -198,11 +201,11 @@ public class BookmarkRESTTest { |
| 198 | 201 | bookmark = new Bookmark(); |
| 199 | 202 | bookmark.setDescription("Google"); |
| 200 | 203 | bookmark.setLink("http: // google . com"); |
| 201 | - request = new HttpPost("/a15/api/bookmark"); | |
| 204 | + request = new HttpPost(url + "/bookmark"); | |
| 202 | 205 | request.setEntity(createEntity(bookmark)); |
| 203 | 206 | request.addHeader("Content-Type", "application/json"); |
| 204 | 207 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 205 | - response = client.execute(host, request); | |
| 208 | + response = client.execute(request); | |
| 206 | 209 | response.close(); |
| 207 | 210 | assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); |
| 208 | 211 | violations = mapper.readValue(response.getEntity().getContent(), |
| ... | ... | @@ -215,11 +218,11 @@ public class BookmarkRESTTest { |
| 215 | 218 | bookmark.setId(Long.valueOf(123456789)); |
| 216 | 219 | bookmark.setDescription("Test"); |
| 217 | 220 | bookmark.setLink("http://test.com"); |
| 218 | - request = new HttpPost("/a15/api/bookmark"); | |
| 221 | + request = new HttpPost(url + "/bookmark"); | |
| 219 | 222 | request.setEntity(createEntity(bookmark)); |
| 220 | 223 | request.addHeader("Content-Type", "application/json"); |
| 221 | 224 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 222 | - response = client.execute(host, request); | |
| 225 | + response = client.execute(request); | |
| 223 | 226 | response.close(); |
| 224 | 227 | assertEquals(SC_BAD_REQUEST, response.getStatusLine().getStatusCode()); |
| 225 | 228 | } |
| ... | ... | @@ -235,18 +238,18 @@ public class BookmarkRESTTest { |
| 235 | 238 | bookmark.setLink("http://maps.google.com"); |
| 236 | 239 | |
| 237 | 240 | Long id = parseEntity(response.getEntity(), Long.class); |
| 238 | - String url = "/a15/api/bookmark/" + id; | |
| 241 | + String resourceUrl = url + "/bookmark/" + id; | |
| 239 | 242 | |
| 240 | - request = new HttpPut(url); | |
| 243 | + request = new HttpPut(resourceUrl); | |
| 241 | 244 | ((HttpPut) request).setEntity(createEntity(bookmark)); |
| 242 | 245 | request.addHeader("Content-Type", "application/json"); |
| 243 | 246 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 244 | - response = client.execute(host, request); | |
| 247 | + response = client.execute(request); | |
| 245 | 248 | response.close(); |
| 246 | 249 | assertEquals(SC_NO_CONTENT, response.getStatusLine().getStatusCode()); |
| 247 | 250 | |
| 248 | - request = new HttpGet(url); | |
| 249 | - response = client.execute(host, request); | |
| 251 | + request = new HttpGet(resourceUrl); | |
| 252 | + response = client.execute(request); | |
| 250 | 253 | response.close(); |
| 251 | 254 | Bookmark result = parseEntity(response.getEntity(), Bookmark.class); |
| 252 | 255 | assertEquals(id, result.getId()); |
| ... | ... | @@ -269,19 +272,19 @@ public class BookmarkRESTTest { |
| 269 | 272 | bookmark = new Bookmark(); |
| 270 | 273 | bookmark.setDescription("Google"); |
| 271 | 274 | bookmark.setLink("http://google.com"); |
| 272 | - request = new HttpPut("/a15/api/bookmark/" + id); | |
| 275 | + request = new HttpPut(url + "/bookmark/" + id); | |
| 273 | 276 | request.setEntity(createEntity(bookmark)); |
| 274 | 277 | request.addHeader("Content-Type", "application/json"); |
| 275 | - response = client.execute(host, request); | |
| 278 | + response = client.execute(request); | |
| 276 | 279 | response.close(); |
| 277 | 280 | assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); |
| 278 | 281 | |
| 279 | 282 | bookmark = new Bookmark(); |
| 280 | - request = new HttpPut("/a15/api/bookmark/" + id); | |
| 283 | + request = new HttpPut(url + "/bookmark/" + id); | |
| 281 | 284 | request.setEntity(createEntity(bookmark)); |
| 282 | 285 | request.addHeader("Content-Type", "application/json"); |
| 283 | 286 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 284 | - response = client.execute(host, request); | |
| 287 | + response = client.execute(request); | |
| 285 | 288 | response.close(); |
| 286 | 289 | assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); |
| 287 | 290 | violations = mapper.readValue(response.getEntity().getContent(), |
| ... | ... | @@ -295,11 +298,11 @@ public class BookmarkRESTTest { |
| 295 | 298 | bookmark = new Bookmark(); |
| 296 | 299 | bookmark.setDescription("Google"); |
| 297 | 300 | bookmark.setLink("http: // google . com"); |
| 298 | - request = new HttpPut("/a15/api/bookmark/" + id); | |
| 301 | + request = new HttpPut(url + "/bookmark/" + id); | |
| 299 | 302 | request.setEntity(createEntity(bookmark)); |
| 300 | 303 | request.addHeader("Content-Type", "application/json"); |
| 301 | 304 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 302 | - response = client.execute(host, request); | |
| 305 | + response = client.execute(request); | |
| 303 | 306 | response.close(); |
| 304 | 307 | assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); |
| 305 | 308 | violations = mapper.readValue(response.getEntity().getContent(), |
| ... | ... | @@ -312,11 +315,11 @@ public class BookmarkRESTTest { |
| 312 | 315 | bookmark.setId(Long.valueOf(123456789)); |
| 313 | 316 | bookmark.setDescription("Test"); |
| 314 | 317 | bookmark.setLink("http://test.com"); |
| 315 | - request = new HttpPut("/a15/api/bookmark/" + id); | |
| 318 | + request = new HttpPut(url + "/bookmark/" + id); | |
| 316 | 319 | request.setEntity(createEntity(bookmark)); |
| 317 | 320 | request.addHeader("Content-Type", "application/json"); |
| 318 | 321 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 319 | - response = client.execute(host, request); | |
| 322 | + response = client.execute(request); | |
| 320 | 323 | response.close(); |
| 321 | 324 | assertEquals(SC_BAD_REQUEST, response.getStatusLine().getStatusCode()); |
| 322 | 325 | |
| ... | ... | @@ -328,21 +331,21 @@ public class BookmarkRESTTest { |
| 328 | 331 | bookmark.setDescription("Google"); |
| 329 | 332 | bookmark.setLink("http://google.com"); |
| 330 | 333 | |
| 331 | - HttpPost request = new HttpPost("/a15/api/bookmark"); | |
| 334 | + HttpPost request = new HttpPost(url + "/bookmark"); | |
| 332 | 335 | request.setEntity(EntityBuilder.create().setText(mapper.writeValueAsString(bookmark)).build()); |
| 333 | 336 | request.addHeader("Content-Type", "application/json"); |
| 334 | 337 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 335 | 338 | |
| 336 | - CloseableHttpResponse response = client.execute(host, request); | |
| 339 | + CloseableHttpResponse response = client.execute(request); | |
| 337 | 340 | response.close(); |
| 338 | 341 | |
| 339 | 342 | return response; |
| 340 | 343 | } |
| 341 | 344 | |
| 342 | 345 | private void destroySample(Long id) throws Exception { |
| 343 | - HttpDelete request = new HttpDelete("/a15/api/bookmark/" + id); | |
| 346 | + HttpDelete request = new HttpDelete(url + "/bookmark/" + id); | |
| 344 | 347 | request.addHeader("Authorization", BASIC_CREDENTIALS); |
| 345 | - client.execute(host, request).close(); | |
| 348 | + client.execute(request).close(); | |
| 346 | 349 | } |
| 347 | 350 | |
| 348 | 351 | private <T> T parseEntity(HttpEntity entity, Class<T> type) throws Exception { | ... | ... |
archetype/html-rest/src/main/resources/archetype-resources/src/test/resources/demoiselle.properties
0 → 100644
| ... | ... | @@ -0,0 +1 @@ |
| 1 | +services.url=http://localhost:8080/${artifactId}/api | ... | ... |