Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.serialization

File MOXyJsonGraphContext.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
10% of files have more coverage

Code metrics

2
32
8
1
123
111
13
0.41
4
8
1.62

Classes

Class Line # Actions
MOXyJsonGraphContext 18 32 13
0.833333383.3%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package io.sunshower.service.serialization;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.model.core.faults.SystemException;
5    import io.sunshower.persistence.core.DistributableEntity;
6    import io.sunshower.service.hal.core.*;
7    import java.io.*;
8    import java.lang.annotation.Annotation;
9    import java.util.ArrayList;
10    import java.util.LinkedHashSet;
11    import java.util.Set;
12    import javax.ws.rs.core.MediaType;
13    import javax.ws.rs.core.MultivaluedHashMap;
14    import org.springframework.util.FastByteArrayOutputStream;
15   
16    /** Created by haswell on 5/24/17. */
17    @SuppressWarnings("unchecked")
 
18    public class MOXyJsonGraphContext implements GraphSerializationContext {
19   
20    private final DynamicJaxrsProviders providers;
21    private final DynamicResolvingMoxyJsonProvider provider;
22   
 
23  2 toggle public MOXyJsonGraphContext(
24    DynamicResolvingMoxyJsonProvider provider, DynamicJaxrsProviders providers) {
25  2 this.provider = provider;
26  2 this.providers = providers;
27    }
28   
 
29  1 toggle @Override
30    public GraphSummary loadSummary(InputStream inputStream) {
31  1 try {
32  1 return (GraphSummary)
33    provider.readFrom(
34    (Class) GraphSummary.class,
35    GraphSummary.class,
36    new Annotation[0],
37    MediaType.APPLICATION_JSON_TYPE,
38    new MultivaluedHashMap<>(),
39    inputStream);
40    } catch (IOException e) {
41  0 throw new RuntimeException(e);
42    }
43    }
44   
 
45  32 toggle @Override
46    public InputStream open(Graph graph) {
47  32 final FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(1024);
48  32 try {
49  32 provider.writeTo(
50    graph,
51    Graph.class,
52    Graph.class,
53    new Annotation[0],
54    MediaType.APPLICATION_JSON_TYPE,
55    new MultivaluedHashMap<>(),
56    outputStream);
57  32 return outputStream.getInputStream();
58    } catch (IOException e) {
59  0 throw new SystemException(e);
60    }
61    }
62   
 
63  17 toggle public InputStream openSummary(Graph graph) {
64  17 final FastByteArrayOutputStream outputStream = new FastByteArrayOutputStream(1024);
65  17 try {
66  17 provider.writeTo(
67    summarize(graph),
68    GraphSummary.class,
69    GraphSummary.class,
70    new Annotation[0],
71    MediaType.APPLICATION_JSON_TYPE,
72    new MultivaluedHashMap<>(),
73    outputStream);
74  17 return outputStream.getInputStream();
75    } catch (IOException e) {
76  0 throw new SystemException(e);
77    }
78    }
79   
 
80  17 toggle @Override
81    public GraphSummary summarize(Graph graph) {
82  17 final GraphSummary graphSummary = new GraphSummary();
83  17 final Set<? extends DistributableEntity> edges = graph.getEdges();
84  17 final Set<? extends DistributableEntity> vertices = graph.getVertices();
85  17 graphSummary.setEdgeSummary(summarize(edges));
86  17 graphSummary.setVertexSummary(summarize(vertices));
87  17 return graphSummary;
88    }
89   
 
90  34 toggle private ElementSummary summarize(Set<? extends DistributableEntity> elements) {
91  34 ElementSummary result = new ElementSummary();
92  34 if (elements != null) {
93  34 LinkedHashSet<Identifier> ids = new LinkedHashSet<>();
94  34 for (DistributableEntity element : elements) {
95  22 ids.add(element.getId());
96    }
97  34 result.setIdentifiers(new ArrayList<>(ids));
98    }
99  34 return result;
100    }
101   
 
102  27 toggle @Override
103    @SuppressWarnings("unchecked")
104    public Graph load(InputStream inputStream) {
105  27 try {
106  27 return (Graph)
107    provider.readFrom(
108    (Class) Graph.class,
109    Graph.class,
110    new Annotation[0],
111    MediaType.APPLICATION_JSON_TYPE,
112    new MultivaluedHashMap<>(),
113    inputStream);
114    } catch (IOException e) {
115  0 throw new RuntimeException(e);
116    }
117    }
118   
 
119  0 toggle @Override
120    public <T> void registerComponent(Class<T> type) {
121  0 provider.register(Graph.class, type);
122    }
123    }