DevfileEntityProvider implements MessageBodyReader<DevfileDto>, MessageBodyWriter<DevfileDto> { @Override public DevfileDto readFrom( Class<DevfileDto> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { try { if (mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE)) { return asDto( devfileParser.parseJson( CharStreams.toString( new InputStreamReader(entityStream, getCharsetOrUtf8(mediaType))))); } else if (mediaType.isCompatible(MediaType.valueOf("text/yaml")) || mediaType.isCompatible(MediaType.valueOf("text/x-yaml"))) { return asDto( devfileParser.parseYaml( CharStreams.toString( new InputStreamReader(entityStream, getCharsetOrUtf8(mediaType))))); } } catch (DevfileFormatException e) { throw new BadRequestException(e.getMessage()); } throw new NotSupportedException("Unknown media type " + mediaType.toString()); } @Inject  DevfileEntityProvider(DevfileParser devfileParser); @Override boolean isReadable(
      Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override DevfileDto readFrom(
      Class<DevfileDto> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, String> httpHeaders,
      InputStream entityStream); @Override boolean isWriteable(
      Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override long getSize(
      DevfileDto devfileDto,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType); @Override void writeTo(
      DevfileDto devfileDto,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders,
      OutputStream entityStream);  }
@Test public void shouldBuildDtoFromValidYaml() throws Exception { when(devfileParser.parseYaml(anyString())).thenReturn(new DevfileImpl()); devfileEntityProvider.readFrom( DevfileDto.class, DevfileDto.class, null, MediaType.valueOf("text/x-yaml"), new MultivaluedHashMap<>(), getClass().getClassLoader().getResourceAsStream("devfile/devfile.yaml")); verify(devfileParser).parseYaml(anyString()); }
@Test public void shouldBuildDtoFromValidJson() throws Exception { when(devfileParser.parseJson(anyString())).thenReturn(new DevfileImpl()); devfileEntityProvider.readFrom( DevfileDto.class, DevfileDto.class, null, MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<>(), getClass().getClassLoader().getResourceAsStream("devfile/devfile.json")); verify(devfileParser).parseJson(anyString()); }
@Test( expectedExceptions = NotSupportedException.class, expectedExceptionsMessageRegExp = "Unknown media type text/plain") public void shouldThrowErrorOnInvalidMediaType() throws Exception { devfileEntityProvider.readFrom( DevfileDto.class, DevfileDto.class, null, MediaType.TEXT_PLAIN_TYPE, new MultivaluedHashMap<>(), getClass().getClassLoader().getResourceAsStream("devfile/devfile.json")); }