Commit 22d6c284c39cb8e63bdf111efd721b9ec9abf324
Exists in
master
Merge branch '2.5' of https://github.com/demoiselle/framework.git into 2.5
Showing
10 changed files
with
569 additions
and
14 deletions
Show diff stats
archetype/html-rest/src/main/resources/archetype-resources/pom.xml
| @@ -42,5 +42,13 @@ | @@ -42,5 +42,13 @@ | ||
| 42 | <version>1.9.9</version> | 42 | <version>1.9.9</version> |
| 43 | <scope>provided</scope> | 43 | <scope>provided</scope> |
| 44 | </dependency> | 44 | </dependency> |
| 45 | + | ||
| 46 | + <!-- Tests dependencies --> | ||
| 47 | + <dependency> | ||
| 48 | + <groupId>org.apache.httpcomponents</groupId> | ||
| 49 | + <artifactId>httpclient</artifactId> | ||
| 50 | + <version>4.3.4</version><!--$NO-MVN-MAN-VER$--> | ||
| 51 | + <scope>test</scope> | ||
| 52 | + </dependency> | ||
| 45 | </dependencies> | 53 | </dependencies> |
| 46 | </project> | 54 | </project> |
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/business/BookmarkBC.java
| 1 | package ${package}.business; | 1 | package ${package}.business; |
| 2 | 2 | ||
| 3 | +import java.util.List; | ||
| 4 | + | ||
| 3 | import ${package}.entity.Bookmark; | 5 | import ${package}.entity.Bookmark; |
| 4 | import ${package}.persistence.BookmarkDAO; | 6 | import ${package}.persistence.BookmarkDAO; |
| 5 | import br.gov.frameworkdemoiselle.lifecycle.Startup; | 7 | import br.gov.frameworkdemoiselle.lifecycle.Startup; |
| @@ -31,4 +33,8 @@ public class BookmarkBC extends DelegateCrud<Bookmark, Long, BookmarkDAO> { | @@ -31,4 +33,8 @@ public class BookmarkBC extends DelegateCrud<Bookmark, Long, BookmarkDAO> { | ||
| 31 | insert(new Bookmark("Binários", "http://sourceforge.net/projects/demoiselle/files/framework")); | 33 | insert(new Bookmark("Binários", "http://sourceforge.net/projects/demoiselle/files/framework")); |
| 32 | } | 34 | } |
| 33 | } | 35 | } |
| 36 | + | ||
| 37 | + public List<Bookmark> find(String filter) { | ||
| 38 | + return getDelegate().find(filter); | ||
| 39 | + } | ||
| 34 | } | 40 | } |
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/entity/Bookmark.java
| @@ -63,4 +63,29 @@ public class Bookmark implements Serializable { | @@ -63,4 +63,29 @@ public class Bookmark implements Serializable { | ||
| 63 | public void setLink(String link) { | 63 | public void setLink(String link) { |
| 64 | this.link = link; | 64 | this.link = link; |
| 65 | } | 65 | } |
| 66 | + | ||
| 67 | + @Override | ||
| 68 | + public int hashCode() { | ||
| 69 | + final int prime = 31; | ||
| 70 | + int result = 1; | ||
| 71 | + result = prime * result + ((id == null) ? 0 : id.hashCode()); | ||
| 72 | + return result; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + @Override | ||
| 76 | + public boolean equals(Object obj) { | ||
| 77 | + if (this == obj) | ||
| 78 | + return true; | ||
| 79 | + if (obj == null) | ||
| 80 | + return false; | ||
| 81 | + if (getClass() != obj.getClass()) | ||
| 82 | + return false; | ||
| 83 | + Bookmark other = (Bookmark) obj; | ||
| 84 | + if (id == null) { | ||
| 85 | + if (other.id != null) | ||
| 86 | + return false; | ||
| 87 | + } else if (!id.equals(other.id)) | ||
| 88 | + return false; | ||
| 89 | + return true; | ||
| 90 | + } | ||
| 66 | } | 91 | } |
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/persistence/BookmarkDAO.java
| 1 | package ${package}.persistence; | 1 | package ${package}.persistence; |
| 2 | 2 | ||
| 3 | -import br.gov.frameworkdemoiselle.stereotype.PersistenceController; | ||
| 4 | -import br.gov.frameworkdemoiselle.template.JPACrud; | 3 | +import java.util.List; |
| 4 | + | ||
| 5 | +import javax.persistence.TypedQuery; | ||
| 5 | 6 | ||
| 6 | import ${package}.entity.Bookmark; | 7 | import ${package}.entity.Bookmark; |
| 8 | +import br.gov.frameworkdemoiselle.stereotype.PersistenceController; | ||
| 9 | +import br.gov.frameworkdemoiselle.template.JPACrud; | ||
| 7 | 10 | ||
| 8 | @PersistenceController | 11 | @PersistenceController |
| 9 | public class BookmarkDAO extends JPACrud<Bookmark, Long> { | 12 | public class BookmarkDAO extends JPACrud<Bookmark, Long> { |
| 10 | - | 13 | + |
| 11 | private static final long serialVersionUID = 1L; | 14 | private static final long serialVersionUID = 1L; |
| 12 | - | 15 | + |
| 16 | + public List<Bookmark> find(String filter) { | ||
| 17 | + StringBuffer ql = new StringBuffer(); | ||
| 18 | + ql.append(" from Bookmark b "); | ||
| 19 | + ql.append(" where lower(b.description) like :description "); | ||
| 20 | + ql.append(" or lower(b.link) like :link "); | ||
| 21 | + | ||
| 22 | + TypedQuery<Bookmark> query = getEntityManager().createQuery(ql.toString(), Bookmark.class); | ||
| 23 | + query.setParameter("description", "%" + filter.toLowerCase() + "%"); | ||
| 24 | + query.setParameter("link", "%" + filter.toLowerCase() + "%"); | ||
| 25 | + | ||
| 26 | + return query.getResultList(); | ||
| 27 | + } | ||
| 13 | } | 28 | } |
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/AuthREST.java
| @@ -4,7 +4,6 @@ import javax.inject.Inject; | @@ -4,7 +4,6 @@ import javax.inject.Inject; | ||
| 4 | import javax.validation.constraints.NotNull; | 4 | import javax.validation.constraints.NotNull; |
| 5 | import javax.validation.constraints.Size; | 5 | import javax.validation.constraints.Size; |
| 6 | import javax.ws.rs.Consumes; | 6 | import javax.ws.rs.Consumes; |
| 7 | -import javax.ws.rs.DELETE; | ||
| 8 | import javax.ws.rs.GET; | 7 | import javax.ws.rs.GET; |
| 9 | import javax.ws.rs.POST; | 8 | import javax.ws.rs.POST; |
| 10 | import javax.ws.rs.Path; | 9 | import javax.ws.rs.Path; |
| @@ -42,12 +41,6 @@ public class AuthREST { | @@ -42,12 +41,6 @@ public class AuthREST { | ||
| 42 | return securityContext.getUser(); | 41 | return securityContext.getUser(); |
| 43 | } | 42 | } |
| 44 | 43 | ||
| 45 | - @DELETE | ||
| 46 | - @LoggedIn | ||
| 47 | - public void logout() { | ||
| 48 | - securityContext.logout(); | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | public static class CredentialsForm { | 44 | public static class CredentialsForm { |
| 52 | 45 | ||
| 53 | @NotNull(message = "{required.field}") | 46 | @NotNull(message = "{required.field}") |
archetype/html-rest/src/main/resources/archetype-resources/src/main/java/rest/BookmarkREST.java
| @@ -12,6 +12,7 @@ import javax.ws.rs.PUT; | @@ -12,6 +12,7 @@ import javax.ws.rs.PUT; | ||
| 12 | import javax.ws.rs.Path; | 12 | import javax.ws.rs.Path; |
| 13 | import javax.ws.rs.PathParam; | 13 | import javax.ws.rs.PathParam; |
| 14 | import javax.ws.rs.Produces; | 14 | import javax.ws.rs.Produces; |
| 15 | +import javax.ws.rs.QueryParam; | ||
| 15 | import javax.ws.rs.core.Context; | 16 | import javax.ws.rs.core.Context; |
| 16 | import javax.ws.rs.core.Response; | 17 | import javax.ws.rs.core.Response; |
| 17 | import javax.ws.rs.core.UriInfo; | 18 | import javax.ws.rs.core.UriInfo; |
| @@ -22,6 +23,7 @@ import br.gov.frameworkdemoiselle.BadRequestException; | @@ -22,6 +23,7 @@ import br.gov.frameworkdemoiselle.BadRequestException; | ||
| 22 | import br.gov.frameworkdemoiselle.NotFoundException; | 23 | import br.gov.frameworkdemoiselle.NotFoundException; |
| 23 | import br.gov.frameworkdemoiselle.security.LoggedIn; | 24 | import br.gov.frameworkdemoiselle.security.LoggedIn; |
| 24 | import br.gov.frameworkdemoiselle.transaction.Transactional; | 25 | import br.gov.frameworkdemoiselle.transaction.Transactional; |
| 26 | +import br.gov.frameworkdemoiselle.util.Strings; | ||
| 25 | import br.gov.frameworkdemoiselle.util.ValidatePayload; | 27 | import br.gov.frameworkdemoiselle.util.ValidatePayload; |
| 26 | 28 | ||
| 27 | @Path("bookmark") | 29 | @Path("bookmark") |
| @@ -32,8 +34,16 @@ public class BookmarkREST { | @@ -32,8 +34,16 @@ public class BookmarkREST { | ||
| 32 | 34 | ||
| 33 | @GET | 35 | @GET |
| 34 | @Produces("application/json") | 36 | @Produces("application/json") |
| 35 | - public List<Bookmark> find() throws Exception { | ||
| 36 | - return bc.findAll(); | 37 | + public List<Bookmark> find(@QueryParam("q") String query) throws Exception { |
| 38 | + List<Bookmark> result; | ||
| 39 | + | ||
| 40 | + if (Strings.isEmpty(query)) { | ||
| 41 | + result = bc.findAll(); | ||
| 42 | + } else { | ||
| 43 | + result = bc.find(query); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + return result; | ||
| 37 | } | 47 | } |
| 38 | 48 | ||
| 39 | @GET | 49 | @GET |
archetype/html-rest/src/main/resources/archetype-resources/src/test/java/rest/BookmarkRESTTest.java
0 → 100644
| @@ -0,0 +1,355 @@ | @@ -0,0 +1,355 @@ | ||
| 1 | +package ${package}.rest; | ||
| 2 | + | ||
| 3 | +import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; | ||
| 4 | +import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; | ||
| 5 | +import static javax.servlet.http.HttpServletResponse.SC_NO_CONTENT; | ||
| 6 | +import static javax.servlet.http.HttpServletResponse.SC_OK; | ||
| 7 | +import static javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED; | ||
| 8 | +import static javax.servlet.http.HttpServletResponse.SC_UNAUTHORIZED; | ||
| 9 | +import static org.junit.Assert.assertEquals; | ||
| 10 | +import static org.junit.Assert.assertNotNull; | ||
| 11 | +import static org.junit.Assert.assertTrue; | ||
| 12 | + | ||
| 13 | +import java.io.IOException; | ||
| 14 | +import java.util.List; | ||
| 15 | +import java.util.Set; | ||
| 16 | + | ||
| 17 | +import org.apache.commons.codec.binary.Base64; | ||
| 18 | +import org.apache.http.HttpEntity; | ||
| 19 | +import org.apache.http.HttpHost; | ||
| 20 | +import org.apache.http.client.ClientProtocolException; | ||
| 21 | +import org.apache.http.client.entity.EntityBuilder; | ||
| 22 | +import org.apache.http.client.methods.CloseableHttpResponse; | ||
| 23 | +import org.apache.http.client.methods.HttpDelete; | ||
| 24 | +import org.apache.http.client.methods.HttpGet; | ||
| 25 | +import org.apache.http.client.methods.HttpPost; | ||
| 26 | +import org.apache.http.client.methods.HttpPut; | ||
| 27 | +import org.apache.http.client.methods.HttpRequestBase; | ||
| 28 | +import org.apache.http.impl.client.CloseableHttpClient; | ||
| 29 | +import org.apache.http.impl.client.HttpClientBuilder; | ||
| 30 | +import org.codehaus.jackson.map.ObjectMapper; | ||
| 31 | +import org.codehaus.jackson.type.TypeReference; | ||
| 32 | +import org.junit.After; | ||
| 33 | +import org.junit.Before; | ||
| 34 | +import org.junit.Test; | ||
| 35 | + | ||
| 36 | +import ${package}.entity.Bookmark; | ||
| 37 | +import br.gov.frameworkdemoiselle.PreconditionFailedException; | ||
| 38 | + | ||
| 39 | +public class BookmarkRESTTest { | ||
| 40 | + | ||
| 41 | + private HttpHost host; | ||
| 42 | + | ||
| 43 | + private static final String BASIC_CREDENTIALS = "Basic " + Base64.encodeBase64String("admin:admin".getBytes()); | ||
| 44 | + | ||
| 45 | + private CloseableHttpClient client; | ||
| 46 | + | ||
| 47 | + private ObjectMapper mapper; | ||
| 48 | + | ||
| 49 | + @Before | ||
| 50 | + public void before() { | ||
| 51 | + host = new HttpHost("localhost", 8080, "http"); | ||
| 52 | + client = HttpClientBuilder.create().build(); | ||
| 53 | + mapper = new ObjectMapper(); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + @After | ||
| 57 | + public void after() throws Exception { | ||
| 58 | + client.close(); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + @Test | ||
| 62 | + public void findSuccessful() throws ClientProtocolException, IOException { | ||
| 63 | + HttpGet request; | ||
| 64 | + CloseableHttpResponse response; | ||
| 65 | + | ||
| 66 | + request = new HttpGet("/a15/api/bookmark"); | ||
| 67 | + response = client.execute(host, request); | ||
| 68 | + response.close(); | ||
| 69 | + List<Bookmark> listAll = mapper.readValue(response.getEntity().getContent(), | ||
| 70 | + new TypeReference<List<Bookmark>>() { | ||
| 71 | + }); | ||
| 72 | + assertEquals(SC_OK, response.getStatusLine().getStatusCode()); | ||
| 73 | + | ||
| 74 | + String filter = "po"; | ||
| 75 | + request = new HttpGet("/a15/api/bookmark?q=" + filter); | ||
| 76 | + response = client.execute(host, request); | ||
| 77 | + response.close(); | ||
| 78 | + List<Bookmark> filteredList = mapper.readValue(response.getEntity().getContent(), | ||
| 79 | + new TypeReference<List<Bookmark>>() { | ||
| 80 | + }); | ||
| 81 | + assertEquals(SC_OK, response.getStatusLine().getStatusCode()); | ||
| 82 | + | ||
| 83 | + for (Bookmark bookmark : filteredList) { | ||
| 84 | + assertTrue(bookmark.getDescription().toLowerCase().contains(filter) | ||
| 85 | + || bookmark.getLink().toLowerCase().contains(filter)); | ||
| 86 | + assertTrue(listAll.contains(bookmark)); | ||
| 87 | + } | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + @Test | ||
| 91 | + public void loadSuccessful() throws Exception { | ||
| 92 | + Long id = parseEntity(createSample().getEntity(), Long.class); | ||
| 93 | + | ||
| 94 | + HttpGet request = new HttpGet("/a15/api/bookmark/" + id); | ||
| 95 | + CloseableHttpResponse response = client.execute(host, request); | ||
| 96 | + response.close(); | ||
| 97 | + assertEquals(SC_OK, response.getStatusLine().getStatusCode()); | ||
| 98 | + | ||
| 99 | + Bookmark bookmark = parseEntity(response.getEntity(), Bookmark.class); | ||
| 100 | + assertEquals(Long.valueOf(id), bookmark.getId()); | ||
| 101 | + assertEquals("Google", bookmark.getDescription()); | ||
| 102 | + assertEquals("http://google.com", bookmark.getLink()); | ||
| 103 | + | ||
| 104 | + destroySample(id); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + @Test | ||
| 108 | + public void loadFailed() throws ClientProtocolException, IOException { | ||
| 109 | + HttpGet get = new HttpGet("/a15/api/bookmark/99999999"); | ||
| 110 | + CloseableHttpResponse response = client.execute(host, get); | ||
| 111 | + response.close(); | ||
| 112 | + assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode()); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + @Test | ||
| 116 | + public void deleteSuccessful() throws Exception { | ||
| 117 | + Long id = parseEntity(createSample().getEntity(), Long.class); | ||
| 118 | + | ||
| 119 | + HttpDelete request = new HttpDelete("/a15/api/bookmark/" + id); | ||
| 120 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 121 | + CloseableHttpResponse response = client.execute(host, request); | ||
| 122 | + response.close(); | ||
| 123 | + assertEquals(SC_NO_CONTENT, response.getStatusLine().getStatusCode()); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + @Test | ||
| 127 | + public void deleteFailed() throws Exception { | ||
| 128 | + HttpDelete request; | ||
| 129 | + CloseableHttpResponse response; | ||
| 130 | + | ||
| 131 | + Long id = parseEntity(createSample().getEntity(), Long.class); | ||
| 132 | + request = new HttpDelete("/a15/api/bookmark/" + id); | ||
| 133 | + response = client.execute(host, request); | ||
| 134 | + response.close(); | ||
| 135 | + assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); | ||
| 136 | + destroySample(id); | ||
| 137 | + | ||
| 138 | + request = new HttpDelete("/a15/api/bookmark/99999999"); | ||
| 139 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 140 | + response = client.execute(host, request); | ||
| 141 | + response.close(); | ||
| 142 | + assertEquals(SC_NOT_FOUND, response.getStatusLine().getStatusCode()); | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + @Test | ||
| 146 | + public void insertSuccessful() throws Exception { | ||
| 147 | + CloseableHttpResponse response = createSample(); | ||
| 148 | + response.close(); | ||
| 149 | + | ||
| 150 | + Long id = parseEntity(response.getEntity(), Long.class); | ||
| 151 | + assertNotNull(id); | ||
| 152 | + | ||
| 153 | + String expectedLocation = host.toString() + "/a15/api/bookmark/" + id; | ||
| 154 | + String returnedLocation = response.getHeaders("Location")[0].getValue(); | ||
| 155 | + assertEquals(expectedLocation, returnedLocation); | ||
| 156 | + | ||
| 157 | + HttpGet request = new HttpGet(returnedLocation); | ||
| 158 | + response = client.execute(host, request); | ||
| 159 | + response.close(); | ||
| 160 | + | ||
| 161 | + destroySample(id); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + @Test | ||
| 165 | + public void insertFailed() throws Exception { | ||
| 166 | + HttpPost request; | ||
| 167 | + CloseableHttpResponse response; | ||
| 168 | + Bookmark bookmark; | ||
| 169 | + Set<PreconditionFailedException.Violation> violations; | ||
| 170 | + PreconditionFailedException expected; | ||
| 171 | + | ||
| 172 | + bookmark = new Bookmark(); | ||
| 173 | + bookmark.setDescription("Google"); | ||
| 174 | + bookmark.setLink("http://google.com"); | ||
| 175 | + request = new HttpPost("/a15/api/bookmark"); | ||
| 176 | + request.setEntity(createEntity(bookmark)); | ||
| 177 | + request.addHeader("Content-Type", "application/json"); | ||
| 178 | + response = client.execute(host, request); | ||
| 179 | + response.close(); | ||
| 180 | + assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); | ||
| 181 | + | ||
| 182 | + bookmark = new Bookmark(); | ||
| 183 | + request = new HttpPost("/a15/api/bookmark"); | ||
| 184 | + request.setEntity(createEntity(bookmark)); | ||
| 185 | + request.addHeader("Content-Type", "application/json"); | ||
| 186 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 187 | + response = client.execute(host, request); | ||
| 188 | + response.close(); | ||
| 189 | + assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); | ||
| 190 | + violations = mapper.readValue(response.getEntity().getContent(), | ||
| 191 | + new TypeReference<Set<PreconditionFailedException.Violation>>() { | ||
| 192 | + }); | ||
| 193 | + expected = new PreconditionFailedException(); | ||
| 194 | + expected.addViolation("description", "não pode ser nulo"); | ||
| 195 | + expected.addViolation("link", "não pode ser nulo"); | ||
| 196 | + assertEquals(expected.getViolations(), violations); | ||
| 197 | + | ||
| 198 | + bookmark = new Bookmark(); | ||
| 199 | + bookmark.setDescription("Google"); | ||
| 200 | + bookmark.setLink("http: // google . com"); | ||
| 201 | + request = new HttpPost("/a15/api/bookmark"); | ||
| 202 | + request.setEntity(createEntity(bookmark)); | ||
| 203 | + request.addHeader("Content-Type", "application/json"); | ||
| 204 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 205 | + response = client.execute(host, request); | ||
| 206 | + response.close(); | ||
| 207 | + assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); | ||
| 208 | + violations = mapper.readValue(response.getEntity().getContent(), | ||
| 209 | + new TypeReference<Set<PreconditionFailedException.Violation>>() { | ||
| 210 | + }); | ||
| 211 | + expected = new PreconditionFailedException().addViolation("link", "formato inválido"); | ||
| 212 | + assertEquals(expected.getViolations(), violations); | ||
| 213 | + | ||
| 214 | + bookmark = new Bookmark(); | ||
| 215 | + bookmark.setId(Long.valueOf(123456789)); | ||
| 216 | + bookmark.setDescription("Test"); | ||
| 217 | + bookmark.setLink("http://test.com"); | ||
| 218 | + request = new HttpPost("/a15/api/bookmark"); | ||
| 219 | + request.setEntity(createEntity(bookmark)); | ||
| 220 | + request.addHeader("Content-Type", "application/json"); | ||
| 221 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 222 | + response = client.execute(host, request); | ||
| 223 | + response.close(); | ||
| 224 | + assertEquals(SC_BAD_REQUEST, response.getStatusLine().getStatusCode()); | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + @Test | ||
| 228 | + public void updateSuccessful() throws Exception { | ||
| 229 | + HttpRequestBase request; | ||
| 230 | + CloseableHttpResponse response = createSample(); | ||
| 231 | + response.close(); | ||
| 232 | + | ||
| 233 | + Bookmark bookmark = new Bookmark(); | ||
| 234 | + bookmark.setDescription("Google Maps"); | ||
| 235 | + bookmark.setLink("http://maps.google.com"); | ||
| 236 | + | ||
| 237 | + Long id = parseEntity(response.getEntity(), Long.class); | ||
| 238 | + String url = "/a15/api/bookmark/" + id; | ||
| 239 | + | ||
| 240 | + request = new HttpPut(url); | ||
| 241 | + ((HttpPut) request).setEntity(createEntity(bookmark)); | ||
| 242 | + request.addHeader("Content-Type", "application/json"); | ||
| 243 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 244 | + response = client.execute(host, request); | ||
| 245 | + response.close(); | ||
| 246 | + assertEquals(SC_NO_CONTENT, response.getStatusLine().getStatusCode()); | ||
| 247 | + | ||
| 248 | + request = new HttpGet(url); | ||
| 249 | + response = client.execute(host, request); | ||
| 250 | + response.close(); | ||
| 251 | + Bookmark result = parseEntity(response.getEntity(), Bookmark.class); | ||
| 252 | + assertEquals(id, result.getId()); | ||
| 253 | + assertEquals(bookmark.getDescription(), result.getDescription()); | ||
| 254 | + assertEquals(bookmark.getLink(), result.getLink()); | ||
| 255 | + | ||
| 256 | + destroySample(id); | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + @Test | ||
| 260 | + public void updateFailed() throws Exception { | ||
| 261 | + HttpPut request; | ||
| 262 | + CloseableHttpResponse response = createSample(); | ||
| 263 | + response.close(); | ||
| 264 | + Long id = parseEntity(response.getEntity(), Long.class); | ||
| 265 | + Bookmark bookmark; | ||
| 266 | + Set<PreconditionFailedException.Violation> violations; | ||
| 267 | + PreconditionFailedException expected; | ||
| 268 | + | ||
| 269 | + bookmark = new Bookmark(); | ||
| 270 | + bookmark.setDescription("Google"); | ||
| 271 | + bookmark.setLink("http://google.com"); | ||
| 272 | + request = new HttpPut("/a15/api/bookmark/" + id); | ||
| 273 | + request.setEntity(createEntity(bookmark)); | ||
| 274 | + request.addHeader("Content-Type", "application/json"); | ||
| 275 | + response = client.execute(host, request); | ||
| 276 | + response.close(); | ||
| 277 | + assertEquals(SC_UNAUTHORIZED, response.getStatusLine().getStatusCode()); | ||
| 278 | + | ||
| 279 | + bookmark = new Bookmark(); | ||
| 280 | + request = new HttpPut("/a15/api/bookmark/" + id); | ||
| 281 | + request.setEntity(createEntity(bookmark)); | ||
| 282 | + request.addHeader("Content-Type", "application/json"); | ||
| 283 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 284 | + response = client.execute(host, request); | ||
| 285 | + response.close(); | ||
| 286 | + assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); | ||
| 287 | + violations = mapper.readValue(response.getEntity().getContent(), | ||
| 288 | + new TypeReference<Set<PreconditionFailedException.Violation>>() { | ||
| 289 | + }); | ||
| 290 | + expected = new PreconditionFailedException(); | ||
| 291 | + expected.addViolation("description", "não pode ser nulo"); | ||
| 292 | + expected.addViolation("link", "não pode ser nulo"); | ||
| 293 | + assertEquals(expected.getViolations(), violations); | ||
| 294 | + | ||
| 295 | + bookmark = new Bookmark(); | ||
| 296 | + bookmark.setDescription("Google"); | ||
| 297 | + bookmark.setLink("http: // google . com"); | ||
| 298 | + request = new HttpPut("/a15/api/bookmark/" + id); | ||
| 299 | + request.setEntity(createEntity(bookmark)); | ||
| 300 | + request.addHeader("Content-Type", "application/json"); | ||
| 301 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 302 | + response = client.execute(host, request); | ||
| 303 | + response.close(); | ||
| 304 | + assertEquals(SC_PRECONDITION_FAILED, response.getStatusLine().getStatusCode()); | ||
| 305 | + violations = mapper.readValue(response.getEntity().getContent(), | ||
| 306 | + new TypeReference<Set<PreconditionFailedException.Violation>>() { | ||
| 307 | + }); | ||
| 308 | + expected = new PreconditionFailedException().addViolation("link", "formato inválido"); | ||
| 309 | + assertEquals(expected.getViolations(), violations); | ||
| 310 | + | ||
| 311 | + bookmark = new Bookmark(); | ||
| 312 | + bookmark.setId(Long.valueOf(123456789)); | ||
| 313 | + bookmark.setDescription("Test"); | ||
| 314 | + bookmark.setLink("http://test.com"); | ||
| 315 | + request = new HttpPut("/a15/api/bookmark/" + id); | ||
| 316 | + request.setEntity(createEntity(bookmark)); | ||
| 317 | + request.addHeader("Content-Type", "application/json"); | ||
| 318 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 319 | + response = client.execute(host, request); | ||
| 320 | + response.close(); | ||
| 321 | + assertEquals(SC_BAD_REQUEST, response.getStatusLine().getStatusCode()); | ||
| 322 | + | ||
| 323 | + destroySample(id); | ||
| 324 | + } | ||
| 325 | + | ||
| 326 | + private CloseableHttpResponse createSample() throws Exception { | ||
| 327 | + Bookmark bookmark = new Bookmark(); | ||
| 328 | + bookmark.setDescription("Google"); | ||
| 329 | + bookmark.setLink("http://google.com"); | ||
| 330 | + | ||
| 331 | + HttpPost request = new HttpPost("/a15/api/bookmark"); | ||
| 332 | + request.setEntity(EntityBuilder.create().setText(mapper.writeValueAsString(bookmark)).build()); | ||
| 333 | + request.addHeader("Content-Type", "application/json"); | ||
| 334 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 335 | + | ||
| 336 | + CloseableHttpResponse response = client.execute(host, request); | ||
| 337 | + response.close(); | ||
| 338 | + | ||
| 339 | + return response; | ||
| 340 | + } | ||
| 341 | + | ||
| 342 | + private void destroySample(Long id) throws Exception { | ||
| 343 | + HttpDelete request = new HttpDelete("/a15/api/bookmark/" + id); | ||
| 344 | + request.addHeader("Authorization", BASIC_CREDENTIALS); | ||
| 345 | + client.execute(host, request).close(); | ||
| 346 | + } | ||
| 347 | + | ||
| 348 | + private <T> T parseEntity(HttpEntity entity, Class<T> type) throws Exception { | ||
| 349 | + return mapper.readValue(entity.getContent(), type); | ||
| 350 | + } | ||
| 351 | + | ||
| 352 | + private HttpEntity createEntity(Object object) throws Exception { | ||
| 353 | + return EntityBuilder.create().setText(mapper.writeValueAsString(object)).build(); | ||
| 354 | + } | ||
| 355 | +} |
archetype/html-rest/src/main/resources/archetype-resources/src/test/resources/ValidationMessages.properties
0 → 100644
impl/extension/servlet/src/test/java/security/authentication/basic/BasicAuthenticationFilterTest.java
| @@ -9,9 +9,11 @@ import java.io.IOException; | @@ -9,9 +9,11 @@ import java.io.IOException; | ||
| 9 | import java.net.URL; | 9 | import java.net.URL; |
| 10 | 10 | ||
| 11 | import org.apache.commons.codec.binary.Base64; | 11 | import org.apache.commons.codec.binary.Base64; |
| 12 | +import org.apache.http.HttpEntity; | ||
| 12 | import org.apache.http.HttpResponse; | 13 | import org.apache.http.HttpResponse; |
| 13 | import org.apache.http.client.ClientProtocolException; | 14 | import org.apache.http.client.ClientProtocolException; |
| 14 | import org.apache.http.client.methods.HttpGet; | 15 | import org.apache.http.client.methods.HttpGet; |
| 16 | +import org.apache.http.client.methods.HttpPost; | ||
| 15 | import org.apache.http.impl.client.CloseableHttpClient; | 17 | import org.apache.http.impl.client.CloseableHttpClient; |
| 16 | import org.apache.http.impl.client.HttpClientBuilder; | 18 | import org.apache.http.impl.client.HttpClientBuilder; |
| 17 | import org.jboss.arquillian.container.test.api.Deployment; | 19 | import org.jboss.arquillian.container.test.api.Deployment; |
| @@ -63,6 +65,12 @@ public class BasicAuthenticationFilterTest { | @@ -63,6 +65,12 @@ public class BasicAuthenticationFilterTest { | ||
| 63 | public void loginFailed() throws ClientProtocolException, IOException { | 65 | public void loginFailed() throws ClientProtocolException, IOException { |
| 64 | String username = "invalid"; | 66 | String username = "invalid"; |
| 65 | String password = "invalid"; | 67 | String password = "invalid"; |
| 68 | + | ||
| 69 | + | ||
| 70 | + HttpPost x = new HttpPost(); | ||
| 71 | + x.setEntity(null); | ||
| 72 | + | ||
| 73 | + //HttpEntity entity | ||
| 66 | 74 | ||
| 67 | HttpGet get = new HttpGet(deploymentUrl + "/helper"); | 75 | HttpGet get = new HttpGet(deploymentUrl + "/helper"); |
| 68 | byte[] encoded = Base64.encodeBase64((username + ":" + password).getBytes()); | 76 | byte[] encoded = Base64.encodeBase64((username + ":" + password).getBytes()); |
parent/rest/pom.xml
| @@ -34,7 +34,8 @@ | @@ -34,7 +34,8 @@ | ||
| 34 | ou escreva para a Fundação do Software Livre (FSF) Inc., | 34 | ou escreva para a Fundação do Software Livre (FSF) Inc., |
| 35 | 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. | 35 | 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA. |
| 36 | --> | 36 | --> |
| 37 | -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | 37 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 38 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| 38 | 39 | ||
| 39 | <modelVersion>4.0.0</modelVersion> | 40 | <modelVersion>4.0.0</modelVersion> |
| 40 | 41 | ||
| @@ -66,12 +67,44 @@ | @@ -66,12 +67,44 @@ | ||
| 66 | <url>http://www.serpro.gov.br</url> | 67 | <url>http://www.serpro.gov.br</url> |
| 67 | </organization> | 68 | </organization> |
| 68 | 69 | ||
| 70 | + <!-- | ||
| 71 | + <dependencyManagement> | ||
| 72 | + <dependencies> | ||
| 73 | + <dependency> | ||
| 74 | + <groupId>org.jboss.arquillian</groupId> | ||
| 75 | + <artifactId>arquillian-bom</artifactId> | ||
| 76 | + <version>${arquillian.bom.version}</version> | ||
| 77 | + <scope>import</scope> | ||
| 78 | + <type>pom</type> | ||
| 79 | + </dependency> | ||
| 80 | + </dependencies> | ||
| 81 | + </dependencyManagement> | ||
| 82 | + --> | ||
| 83 | + | ||
| 69 | <dependencies> | 84 | <dependencies> |
| 70 | <dependency> | 85 | <dependency> |
| 71 | <groupId>br.gov.frameworkdemoiselle</groupId> | 86 | <groupId>br.gov.frameworkdemoiselle</groupId> |
| 72 | <artifactId>demoiselle-rest</artifactId> | 87 | <artifactId>demoiselle-rest</artifactId> |
| 73 | <scope>compile</scope> | 88 | <scope>compile</scope> |
| 74 | </dependency> | 89 | </dependency> |
| 90 | + | ||
| 91 | + | ||
| 92 | + <!-- Test dependencies --> | ||
| 93 | + <dependency> | ||
| 94 | + <groupId>org.jboss.arquillian.junit</groupId> | ||
| 95 | + <artifactId>arquillian-junit-container</artifactId> | ||
| 96 | + <scope>test</scope> | ||
| 97 | + </dependency> | ||
| 98 | + <dependency> | ||
| 99 | + <groupId>org.jboss.shrinkwrap.resolver</groupId> | ||
| 100 | + <artifactId>shrinkwrap-resolver-impl-maven</artifactId> | ||
| 101 | + <scope>test</scope> | ||
| 102 | + </dependency> | ||
| 103 | + <dependency> | ||
| 104 | + <groupId>org.jboss.arquillian.protocol</groupId> | ||
| 105 | + <artifactId>arquillian-protocol-servlet</artifactId> | ||
| 106 | + <scope>test</scope> | ||
| 107 | + </dependency> | ||
| 75 | </dependencies> | 108 | </dependencies> |
| 76 | 109 | ||
| 77 | <profiles> | 110 | <profiles> |
| @@ -95,14 +128,108 @@ | @@ -95,14 +128,108 @@ | ||
| 95 | <activation> | 128 | <activation> |
| 96 | <activeByDefault>true</activeByDefault> | 129 | <activeByDefault>true</activeByDefault> |
| 97 | </activation> | 130 | </activation> |
| 131 | + <!-- | ||
| 132 | + <dependencyManagement> | ||
| 133 | + <dependencies> | ||
| 134 | + <dependency> | ||
| 135 | + <groupId>org.jboss.as</groupId> | ||
| 136 | + <artifactId>jboss-as-parent</artifactId> | ||
| 137 | + <version>${jboss.as.version}</version> | ||
| 138 | + <type>pom</type> | ||
| 139 | + <scope>import</scope> | ||
| 140 | + </dependency> | ||
| 141 | + </dependencies> | ||
| 142 | + </dependencyManagement> | ||
| 143 | + --> | ||
| 98 | <dependencies> | 144 | <dependencies> |
| 99 | <dependency> | 145 | <dependency> |
| 100 | <groupId>javax.ws.rs</groupId> | 146 | <groupId>javax.ws.rs</groupId> |
| 101 | <artifactId>jsr311-api</artifactId> | 147 | <artifactId>jsr311-api</artifactId> |
| 102 | <scope>provided</scope> | 148 | <scope>provided</scope> |
| 103 | </dependency> | 149 | </dependency> |
| 150 | + | ||
| 151 | + <!-- Test dependencies --> | ||
| 152 | + <!-- | ||
| 153 | + <dependency> | ||
| 154 | + <groupId>org.jboss.as</groupId> | ||
| 155 | + <artifactId>jboss-as-arquillian-container-remote</artifactId> | ||
| 156 | + <scope>test</scope> | ||
| 157 | + </dependency> | ||
| 158 | + --> | ||
| 159 | + </dependencies> | ||
| 160 | + <!-- | ||
| 161 | + <properties> | ||
| 162 | + <jboss.as.version>7.2.0.Final</jboss.as.version> | ||
| 163 | + </properties> | ||
| 164 | + --> | ||
| 165 | + </profile> | ||
| 166 | + <!-- | ||
| 167 | + <profile> | ||
| 168 | + <id>arquillian-test</id> | ||
| 169 | + <dependencies> | ||
| 170 | + <dependency> | ||
| 171 | + <groupId>br.gov.frameworkdemoiselle</groupId> | ||
| 172 | + <artifactId>demoiselle-core</artifactId> | ||
| 173 | + <exclusions> | ||
| 174 | + <exclusion> | ||
| 175 | + <groupId>javax.enterprise</groupId> | ||
| 176 | + <artifactId>cdi-api</artifactId> | ||
| 177 | + </exclusion> | ||
| 178 | + <exclusion> | ||
| 179 | + <artifactId>validation-api</artifactId> | ||
| 180 | + <groupId>javax.validation</groupId> | ||
| 181 | + </exclusion> | ||
| 182 | + <exclusion> | ||
| 183 | + <groupId>org.slf4j</groupId> | ||
| 184 | + <artifactId>slf4j-api</artifactId> | ||
| 185 | + </exclusion> | ||
| 186 | + <exclusion> | ||
| 187 | + <groupId>org.javassist</groupId> | ||
| 188 | + <artifactId>javassist</artifactId> | ||
| 189 | + </exclusion> | ||
| 190 | + <exclusion> | ||
| 191 | + <groupId>commons-configuration</groupId> | ||
| 192 | + <artifactId>commons-configuration</artifactId> | ||
| 193 | + </exclusion> | ||
| 194 | + </exclusions> | ||
| 195 | + </dependency> | ||
| 196 | + <dependency> | ||
| 197 | + <groupId>br.gov.frameworkdemoiselle</groupId> | ||
| 198 | + <artifactId>demoiselle-servlet</artifactId> | ||
| 199 | + <exclusions> | ||
| 200 | + <exclusion> | ||
| 201 | + <groupId>javax.servlet</groupId> | ||
| 202 | + <artifactId>javax.servlet-api</artifactId> | ||
| 203 | + </exclusion> | ||
| 204 | + <exclusion> | ||
| 205 | + <groupId>commons-codec</groupId> | ||
| 206 | + <artifactId>commons-codec</artifactId> | ||
| 207 | + </exclusion> | ||
| 208 | + </exclusions> | ||
| 209 | + </dependency> | ||
| 210 | + <dependency> | ||
| 211 | + <groupId>br.gov.frameworkdemoiselle</groupId> | ||
| 212 | + <artifactId>demoiselle-jpa</artifactId> | ||
| 213 | + <exclusions> | ||
| 214 | + <exclusion> | ||
| 215 | + <groupId>org.eclipse.persistence</groupId> | ||
| 216 | + <artifactId>javax.persistence</artifactId> | ||
| 217 | + </exclusion> | ||
| 218 | + </exclusions> | ||
| 219 | + </dependency> | ||
| 220 | + <dependency> | ||
| 221 | + <groupId>br.gov.frameworkdemoiselle</groupId> | ||
| 222 | + <artifactId>demoiselle-jta</artifactId> | ||
| 223 | + <exclusions> | ||
| 224 | + <exclusion> | ||
| 225 | + <groupId>javax.transaction</groupId> | ||
| 226 | + <artifactId>jta</artifactId> | ||
| 227 | + </exclusion> | ||
| 228 | + </exclusions> | ||
| 229 | + </dependency> | ||
| 104 | </dependencies> | 230 | </dependencies> |
| 105 | </profile> | 231 | </profile> |
| 232 | + --> | ||
| 106 | </profiles> | 233 | </profiles> |
| 107 | 234 | ||
| 108 | <repositories> | 235 | <repositories> |
| @@ -129,4 +256,10 @@ | @@ -129,4 +256,10 @@ | ||
| 129 | </releases> | 256 | </releases> |
| 130 | </repository> | 257 | </repository> |
| 131 | </repositories> | 258 | </repositories> |
| 259 | + | ||
| 260 | + <!-- | ||
| 261 | + <properties> | ||
| 262 | + <arquillian.bom.version>1.1.1.Final</arquillian.bom.version> | ||
| 263 | + </properties> | ||
| 264 | + --> | ||
| 132 | </project> | 265 | </project> |