| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.service.full; |
| 2 | |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertTrue; |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 5 | |
| 6 | import org.eclipse.jetty.http.HttpHeaders; |
| margaretha | 74f122c | 2017-11-12 22:38:57 +0100 | [diff] [blame] | 7 | import org.junit.Ignore; |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 8 | import org.junit.Test; |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 9 | |
| 10 | import com.fasterxml.jackson.databind.JsonNode; |
| 11 | import com.sun.jersey.api.client.ClientResponse; |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 12 | |
| 13 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 14 | import de.ids_mannheim.korap.config.SpringJerseyTest; |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 15 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 16 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 17 | import de.ids_mannheim.korap.security.auth.BasicHttpAuth; |
| 18 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 19 | |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 20 | public class VirtualCorpusServiceTest extends SpringJerseyTest{ |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 21 | |
| 22 | @Test |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 23 | @Ignore |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 24 | public void testStoreVC () throws KustvaktException { |
| 25 | String json = |
| 26 | "{\"name\": \"new vc\",\"type\": \"PRIVATE\",\"createdBy\": " |
| 27 | + "\"test class\",\"collectionQuery\": \"corpusSigle=GOE\"}"; |
| 28 | |
| 29 | ClientResponse response = resource().path("vc").path("store") |
| 30 | .header(Attributes.AUTHORIZATION, |
| 31 | BasicHttpAuth.encode("kustvakt", "kustvakt2015")) |
| 32 | .header(HttpHeaders.X_FORWARDED_FOR, "149.27.0.32") |
| 33 | .entity(json) |
| 34 | .post(ClientResponse.class); |
| 35 | String entity = response.getEntity(String.class); |
| 36 | System.out.println(entity); |
| 37 | } |
| 38 | |
| 39 | @Test |
| 40 | public void testStoreVCUnauthorized () throws KustvaktException { |
| 41 | String json = |
| 42 | "{\"name\": \"new vc\",\"type\": \"PRIVATE\",\"createdBy\": " |
| 43 | + "\"test class\",\"collectionQuery\": \"pubDate eq 1982\"}"; |
| 44 | |
| 45 | ClientResponse response = resource().path("vc").path("store") |
| 46 | .entity(json).post(ClientResponse.class); |
| 47 | String entity = response.getEntity(String.class); |
| 48 | |
| 49 | JsonNode node = JsonUtils.readTree(entity); |
| 50 | assertEquals(StatusCodes.UNAUTHORIZED_OPERATION, |
| 51 | node.at("/errors/0/0").asInt()); |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testStoreVCWithWrongType () throws KustvaktException { |
| 56 | String json = |
| 57 | "{\"name\": \"new vc\",\"type\": \"PRIVAT\",\"createdBy\": " |
| 58 | + "\"test class\",\"collectionQuery\": \"pubDate eq 1982\"}"; |
| 59 | |
| 60 | ClientResponse response = resource().path("vc").path("store") |
| 61 | .entity(json).post(ClientResponse.class); |
| 62 | String entity = response.getEntity(String.class); |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 63 | System.out.println(entity); |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 64 | |
| 65 | JsonNode node = JsonUtils.readTree(entity); |
| 66 | assertEquals(StatusCodes.DESERIALIZATION_FAILED, |
| 67 | node.at("/errors/0/0").asInt()); |
| 68 | assertTrue(node.at("/errors/0/1").asText().startsWith( |
| 69 | "Cannot deserialize value of type `de.ids_mannheim.korap.constant." |
| 70 | + "VirtualCorpusType` from String \"PRIVAT\": value not one of " |
| margaretha | fde771a | 2017-11-14 15:02:10 +0100 | [diff] [blame^] | 71 | + "declared Enum instance names")); |
| margaretha | f093afb | 2017-11-12 21:38:23 +0100 | [diff] [blame] | 72 | } |
| 73 | } |