Changeset 3186


Ignore:
Timestamp:
07/24/13 09:41:55 (11 years ago)
Author:
olhsha
Message:

"POST annotations/" chain is done fully, except that for now POST returns Annotation but not an envelope with the server's respond. This feature should be added after Sources and Permissions are added to the Database.
AND, since at the moment the ObjectFactory?() produces Annotations without root element there is a temporary fix for it: an additional class AnnotationRooted? which extends Annotation just by adding a root node. MUST BE REMOVED after the problem with the factory is fixed as it should.

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r3180 r3186  
    1818package eu.dasish.annotation.backend.rest;
    1919
     20import eu.dasish.annotation.backend.AnnotationRooted;
    2021import eu.dasish.annotation.backend.BackendConstants;
    2122import eu.dasish.annotation.backend.dao.AnnotationDao;
     
    2425import eu.dasish.annotation.schema.ObjectFactory;
    2526import java.sql.SQLException;
    26 import java.util.ArrayList;
    2727import javax.ws.rs.Consumes;
    2828import javax.ws.rs.DELETE;
     
    4747    @Autowired
    4848    private AnnotationDao annotationDao;
    49    
    50     final private int charBufferSize = 128;
    5149   
    5250    @GET
     
    7270    @Produces(MediaType.APPLICATION_XML)
    7371    @Path("")
    74     public String createAnnotation(ArrayList<Annotation> annotation) {
    75         return "OK";
    76         /*AnnotationIdentifier newAnnotationIdentifier = annotationDao.addAnnotation(annotation);
     72    public JAXBElement<Annotation> createAnnotation(AnnotationRooted annotation) {
     73        AnnotationIdentifier newAnnotationIdentifier = annotationDao.addAnnotation(annotation);
     74        //return newAnnotationIdentifier.toString();
    7775        if (newAnnotationIdentifier == null) {
    7876            return null;
    7977        } else {
    80             return Response.status(Status.OK).entity(new ObjectFactory().createAnnotation(annotation)).build();
    81         }*/
     78            return (new ObjectFactory().createAnnotation(annotation));
     79        }
    8280    }
    8381   
    84    
    85     /* helper: stolen from StackOverflow
    86     private String getBody(HttpServletRequest request) throws IOException{
    87         String body = null;
    88         StringBuilder stringBuilder = new StringBuilder();
    89         BufferedReader bufferedReader = null;
    90 
    91         try {
    92             InputStream inputStream = request.getInputStream();
    93             if (inputStream != null) {
    94                 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    95                 char[] charBuffer = new char[charBufferSize];
    96                 int bytesRead = -1;
    97                 while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
    98                     stringBuilder.append(charBuffer, 0, bytesRead);
    99                 }
    100             } else {
    101                 stringBuilder.append("");
    102             }
    103         } catch (IOException ex) {
    104             throw ex;
    105         } finally {
    106             if (bufferedReader != null) {
    107                 try {
    108                     bufferedReader.close();
    109                 } catch (IOException ex) {
    110                     throw ex;
    111                 }
    112             }
    113         }
    114 
    115         body = stringBuilder.toString();
    116         return body;
    117 
    118     }*/
    119 
    120    
     82 
    12183}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.java

    r3180 r3186  
    1919
    2020import com.sun.jersey.api.client.GenericType;
     21import eu.dasish.annotation.backend.AnnotationRooted;
    2122import eu.dasish.annotation.backend.TestBackendConstants;
    2223import eu.dasish.annotation.backend.TestInstances;
     
    2425import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
    2526import eu.dasish.annotation.schema.Annotation;
     27import eu.dasish.annotation.schema.ObjectFactory;
    2628import java.sql.SQLException;
    27 import javax.ws.rs.core.Response;
    2829import javax.xml.bind.JAXBElement;
    2930import org.jmock.Expectations;
     
    3637import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    3738import java.lang.InstantiationException;
    38 import java.util.ArrayList;
    39 import java.util.List;
    4039/**
    4140 *
     
    123122    public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException {
    124123        System.out.println("test createAnnotation");
    125         final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance();
     124        final AnnotationRooted annotationToAdd = new GenericType<AnnotationRooted>(){}.getRawClass().newInstance();
    126125        final AnnotationIdentifier newAnnotationID = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance();
    127126       
     
    133132        });
    134133       
    135         ArrayList<Annotation> parameter = new ArrayList<Annotation>();
    136         parameter.add(annotationToAdd);
    137134       
    138         String result = annotationResource.createAnnotation(parameter);       
    139         assertTrue(result=="OK");
    140         //assertEquals(200, result.getStatus());
    141         //assertTrue(annotationToAdd.equals(result.getEntity()));
     135        JAXBElement result = annotationResource.createAnnotation(annotationToAdd);
     136        assertTrue(annotationToAdd.equals(result.getValue()));
    142137    }
    143138}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java

    r3180 r3186  
    1919
    2020import com.sun.jersey.api.client.ClientResponse;
     21import com.sun.jersey.api.client.GenericType;
     22import eu.dasish.annotation.backend.AnnotationRooted;
    2123import eu.dasish.annotation.backend.TestBackendConstants;
    2224import eu.dasish.annotation.backend.dao.AnnotationDao;
    2325import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
    2426import eu.dasish.annotation.schema.Annotation;
     27import eu.dasish.annotation.schema.Annotation;
    2528import eu.dasish.annotation.schema.ObjectFactory;
    2629import java.sql.SQLException;
    27 import java.util.ArrayList;
    28 import java.util.List;
     30import javax.ws.rs.core.GenericEntity;
    2931import javax.ws.rs.core.MediaType;
     32import javax.xml.bind.JAXBElement;
    3033import org.jmock.Expectations;
    3134import org.junit.Test;
    3235import static org.junit.Assert.*;
    33 import org.junit.Ignore;
    3436/**
    3537 *
     
    127129     */
    128130    @Test
    129     @Ignore
     131    //@Ignore
    130132    public void testAddAnnotation() throws SQLException, InstantiationException, IllegalAccessException{
    131         System.out.println("testDeleteAnnotation");
    132133        System.out.println("test createAnnotation");
    133         //final Annotation annotationToAdd = new GenericType<Annotation>(){}.getRawClass().newInstance();
    134         final Annotation annotationToAdd = new ObjectFactory().createAnnotation();
    135         ///final AnnotationIdentifier newAnnotationID = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance();
     134        //final Annotation annotationToAdd = new ObjectFactory().createAnnotation();
     135       final AnnotationRooted annotationToAdd = new GenericType<AnnotationRooted>(){}.getRawClass().newInstance();
     136       final AnnotationIdentifier newAnnotationID = new GenericType<AnnotationIdentifier>(){}.getRawClass().newInstance();
    136137       
    137 //        mockery.checking(new Expectations() {
    138 //            {
    139 //                oneOf(annotationDao).addAnnotation(annotationToAdd);
    140 //                will(returnValue(newAnnotationID));
    141 //            }
    142 //        });
     138        mockery.checking(new Expectations() {
     139            {
     140                oneOf(annotationDao).addAnnotation(with(aNonNull(AnnotationRooted.class)));
     141                will(returnValue(newAnnotationID));
     142            }
     143        });
    143144       
    144        
     145       
     146       
    145147        final String requestUrl = "annotations/";
    146148        System.out.println("requestUrl: " + requestUrl);
    147149       
    148         List<Annotation> parameter = new ArrayList<Annotation>();
    149         parameter.add(annotationToAdd);
     150        ClientResponse response = resource().path(requestUrl).type(MediaType.APPLICATION_XML).post(ClientResponse.class, new GenericEntity<AnnotationRooted>(annotationToAdd){});
     151        assertEquals(200, response.getStatus());
    150152       
    151         String response = resource().path(requestUrl).type(MediaType.APPLICATION_XML).post(String.class, parameter);
    152         //assertEquals(200, response.getStatus());
    153         //assertEquals(annotationToAdd, response.getEntity(Annotation.class));
    154        
    155          
     153        Annotation entity = response.getEntity(Annotation.class);
     154        assertEquals(annotationToAdd.getBody(), entity.getBody());
     155        assertEquals(annotationToAdd.getHeadline(), entity.getHeadline());
     156        assertEquals(annotationToAdd.getPermissions(), entity.getPermissions());
     157        assertEquals(annotationToAdd.getTargetSources(), entity.getTargetSources());
     158        assertEquals(annotationToAdd.getTimeStamp(), entity.getTimeStamp());
     159        assertEquals(annotationToAdd.getURI(), entity.getURI());
    156160    }
    157161}
Note: See TracChangeset for help on using the changeset viewer.