1 | package org.maxur.perfmodel.backend.rest.resources; | |
2 | ||
3 | import com.fasterxml.jackson.core.JsonFactory; | |
4 | import com.fasterxml.jackson.core.type.TypeReference; | |
5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
6 | import com.google.common.base.Charsets; | |
7 | import com.google.common.io.Resources; | |
8 | import org.jsondoc.core.annotation.*; | |
9 | import org.jsondoc.core.pojo.ApiVerb; | |
10 | import org.maxur.perfmodel.backend.service.Application; | |
11 | import org.slf4j.Logger; | |
12 | import org.slf4j.LoggerFactory; | |
13 | ||
14 | import javax.inject.Inject; | |
15 | import javax.inject.Named; | |
16 | import javax.ws.rs.*; | |
17 | import javax.ws.rs.core.MediaType; | |
18 | import javax.ws.rs.core.Response; | |
19 | import java.io.IOException; | |
20 | import java.net.URL; | |
21 | import java.util.HashMap; | |
22 | import java.util.Map; | |
23 | ||
24 | import static org.maxur.perfmodel.backend.rest.WebException.badRequestException; | |
25 | import static org.maxur.perfmodel.backend.utils.DateTimeUtils.schedule; | |
26 | ||
27 | /** | |
28 | * @author Maxim Yunusov | |
29 | * @version 1.0 | |
30 | * @since <pre>11/29/13</pre> | |
31 | */ | |
32 | @Path("/{a:application}") | |
33 | @Api(name = "Application Resource", description = "Methods for managing PMC Application") | |
34 | @ApiVersion(since = "1.0") | |
35 | public class ApplicationResource { | |
36 | ||
37 | private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationResource.class); | |
38 | ||
39 | @Inject | |
40 | private Application application; | |
41 | ||
42 | ||
43 | @SuppressWarnings("unused") | |
44 | @Named("webapp.url") | |
45 | private String webappUrl; | |
46 | ||
47 | @GET | |
48 | @Path("/jsondoc") | |
49 | @Produces(MediaType.APPLICATION_JSON) | |
50 | @ApiMethod( | |
51 | path = "/application/jsondoc", | |
52 | verb = ApiVerb.GET, | |
53 | description = "Gets a application documentation", | |
54 | produces = { MediaType.APPLICATION_JSON }, | |
55 | responsestatuscode = "200 - OK" | |
56 | ) | |
57 | @ApiErrors(apierrors={ | |
58 | @ApiError(code="500", description="Application critical error") | |
59 | }) | |
60 | public @ApiResponseObject String getJsonDoc() { | |
61 | URL url = Resources.getResource("jsondoc.json"); | |
62 | try { | |
63 | final String jsonDoc = Resources.toString(url, Charsets.UTF_8); | |
64 |
1
1. getJsonDoc : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ApplicationResource::getJsonDoc to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return jsonDoc.replaceAll("http://127.0.0.1:8080/api", webappUrl + "api"); |
65 | } catch (IOException e) { | |
66 | throw new IllegalStateException(e); | |
67 | } | |
68 | } | |
69 | ||
70 | @GET | |
71 | @Path("/version") | |
72 | @Produces(MediaType.APPLICATION_JSON) | |
73 | @ApiMethod( | |
74 | path = "/application/version", | |
75 | verb = ApiVerb.GET, | |
76 | description = "Gets a application version", | |
77 | produces = { MediaType.APPLICATION_JSON }, | |
78 | responsestatuscode = "200 - OK" | |
79 | ) | |
80 | @ApiErrors(apierrors={ | |
81 | @ApiError(code="500", description="Application critical error") | |
82 | }) | |
83 | public @ApiResponseObject String getVersion() { | |
84 |
1
1. getVersion : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ApplicationResource::getVersion to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return application.version(); |
85 | } | |
86 | ||
87 | @PUT | |
88 | @Path("/status") | |
89 | @Consumes(MediaType.APPLICATION_JSON) | |
90 | @Produces(MediaType.APPLICATION_JSON) | |
91 | @ApiMethod( | |
92 | path = "/application/status", | |
93 | verb = ApiVerb.PUT, | |
94 | description = "Sets a application status to stopped", | |
95 | produces = { MediaType.APPLICATION_JSON }, | |
96 | consumes = { MediaType.APPLICATION_JSON }, | |
97 | responsestatuscode = "200 - OK" | |
98 | ) | |
99 | @ApiErrors(apierrors={ | |
100 | @ApiError(code="500", description="Application critical error") | |
101 | }) | |
102 | public Response action(@ApiBodyObject final String object) { | |
103 | try { | |
104 | final ObjectMapper mapper = new ObjectMapper(new JsonFactory()); | |
105 | final Map<String, Object> map = mapper.readValue(object, new TypeReference<HashMap<String, Object>>() { | |
106 | }); | |
107 | final String status = (String) map.get("status"); | |
108 |
1
1. action : negated conditional → NO_COVERAGE |
switch (status) { |
109 | case "stopped": | |
110 | schedule(application::stop, 100); | |
111 | break; | |
112 | default: | |
113 | throw badRequestException("Status '%s' is not valid", status); | |
114 | } | |
115 |
1
1. action : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ApplicationResource::action to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return Response.status(Response.Status.ACCEPTED).build(); |
116 | } catch (IOException e) { | |
117 | LOGGER.error("Operation is not valid", e); | |
118 | throw badRequestException("Operation is not valid", e.getMessage()); | |
119 | } | |
120 | } | |
121 | } | |
Mutations | ||
64 |
1.1 |
|
84 |
1.1 |
|
108 |
1.1 |
|
115 |
1.1 |