ProjectResource.java

1
package org.maxur.perfmodel.backend.rest.resources;
2
3
4
import org.jsondoc.core.annotation.*;
5
import org.jsondoc.core.pojo.ApiVerb;
6
import org.maxur.perfmodel.backend.domain.ConflictException;
7
import org.maxur.perfmodel.backend.domain.Project;
8
import org.maxur.perfmodel.backend.domain.Repository;
9
import org.maxur.perfmodel.backend.rest.dto.ProjectDto;
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
13
import javax.inject.Inject;
14
import javax.ws.rs.*;
15
import javax.ws.rs.core.MediaType;
16
import javax.ws.rs.core.Response;
17
import java.util.Collection;
18
import java.util.Optional;
19
20
import static com.google.common.base.Strings.isNullOrEmpty;
21
import static java.lang.String.format;
22
import static org.maxur.perfmodel.backend.rest.WebException.*;
23
import static org.maxur.perfmodel.backend.rest.dto.ProjectDto.dto;
24
import static org.maxur.perfmodel.backend.rest.dto.ProjectDto.dtoList;
25
26
27
/**
28
 * @author Maxim Yunusov
29
 * @version 1.0 01.09.13
30
 */
31
@Path("/{a:project}")
32
@Api(name = "Project Resource", description = "Methods for managing Project")
33
@ApiVersion(since = "1.0")
34
public class ProjectResource {
35
36
    private static final String PROJECT_IS_NOT_SAVED = "Project is not saved";
37
38
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectResource.class);
39
40
    @Inject
41
    private Repository<Project> repository;
42
43
    @GET
44
    @Produces(MediaType.APPLICATION_JSON)
45
    @ApiMethod(
46
            path = "/project",
47
            verb = ApiVerb.GET,
48
            description = "Gets all projects",
49
            produces = { MediaType.APPLICATION_JSON },
50
            responsestatuscode = "200 - OK"
51
    )
52
    @ApiErrors(apierrors={
53
            @ApiError(code="500", description="Application critical error")
54
    })
55
    public @ApiResponseObject Collection<ProjectDto> findAll() {
56 1 1. findAll : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::findAll to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return dtoList(repository.findAll());
57
    }
58
59
    @GET
60
    @Path("/{id}")
61
    @Produces(MediaType.APPLICATION_JSON)
62
    @ApiMethod(
63
            path = "/project/{id}",
64
            verb = ApiVerb.GET,
65
            description = "Get the project by it's identifier",
66
            produces = { MediaType.APPLICATION_JSON },
67
            responsestatuscode = "200 - OK"
68
    )
69
    @ApiErrors(apierrors={
70
            @ApiError(code="400", description="Bad request"),
71
            @ApiError(code="404", description="Project not found"),
72
            @ApiError(code="500", description="Application critical error")
73
    })
74
    public @ApiResponseObject ProjectDto load(
75
            @ApiPathParam(name = "id", description = "The project identifier")
76
            @PathParam("id") String id
77
    ) {
78 1 1. load : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE
        checkIdIsNotNull(id);
79
        final Optional<Project> result = repository.get(id);
80 1 1. load : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE
        checkResult(id, result);
81 1 1. load : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::load to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return dto(result.get());
82
    }
83
84
    @DELETE
85
    @Path("/{id}")
86
    @Produces(MediaType.APPLICATION_JSON)
87
    @ApiMethod(
88
            path = "/project/{id}",
89
            verb = ApiVerb.DELETE,
90
            description = "Delete the project by it's identifier",
91
            produces = { MediaType.APPLICATION_JSON },
92
            responsestatuscode = "200 - OK"
93
    )
94
    @ApiErrors(apierrors={
95
            @ApiError(code="400", description="Bad request"),
96
            @ApiError(code="404", description="Project not found"),
97
            @ApiError(code="500", description="Application critical error")
98
    })
99
    public @ApiResponseObject ProjectDto delete(
100
            @ApiPathParam(name = "id", description = "The project identifier")
101
            @PathParam("id") String id
102
    ) {
103 1 1. delete : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE
        checkIdIsNotNull(id);
104
        final Optional<Project> result = repository.remove(id);
105 1 1. delete : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE
        checkResult(id, result);
106 1 1. delete : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::delete to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return dto(result.get());
107
    }
108
109
    @POST
110
    @Consumes(MediaType.APPLICATION_JSON)
111
    @Produces(MediaType.APPLICATION_JSON)
112
    @ApiMethod(
113
            path = "/project",
114
            verb = ApiVerb.POST,
115
            description = "Create new project version by project's identifier",
116
            produces = { MediaType.APPLICATION_JSON },
117
            consumes = { MediaType.APPLICATION_JSON },
118
            responsestatuscode = "201 - Created"
119
    )
120
    @ApiErrors(apierrors={
121
            @ApiError(code="400", description="Bad request"),
122
            @ApiError(code="404", description="Project not found"),
123
            @ApiError(code="500", description="Application critical error")
124
    })
125
    public @ApiResponseObject Response create(
126
            @ApiBodyObject final ProjectDto dto
127
    ) {
128
        try {
129
            final Project project = dto.assemble();
130 1 1. create : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE
            checkIdIsNotNull(dto.getId());
131
            final Optional<Project> result = repository.add(project);
132 1 1. create : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::create to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return created(result.get());
133
        } catch (NumberFormatException e) {
134
            LOGGER.error(PROJECT_IS_NOT_SAVED, e);
135
            throw badRequestException(PROJECT_IS_NOT_SAVED, e.getMessage());
136
        } catch (ConflictException e) {
137
            LOGGER.error(format("%s: %s", PROJECT_IS_NOT_SAVED, e.getMessage()));
138
            throw conflictException(PROJECT_IS_NOT_SAVED, e.getMessage());
139
        }
140
    }
141
142
    @PUT
143
    @Path("/{id}")
144
    @Consumes(MediaType.APPLICATION_JSON)
145
    @Produces(MediaType.APPLICATION_JSON)
146
    @ApiMethod(
147
            path = "/project/{id}",
148
            verb = ApiVerb.PUT,
149
            description = "Create new project version by project's identifier",
150
            produces = { MediaType.APPLICATION_JSON },
151
            consumes = { MediaType.APPLICATION_JSON },
152
            responsestatuscode = "200 - OK"
153
    )
154
    @ApiErrors(apierrors={
155
            @ApiError(code="400", description="Bad request"),
156
            @ApiError(code="404", description="Project not found"),
157
            @ApiError(code="500", description="Application critical error")
158
    })
159
    public @ApiResponseObject ProjectDto update(
160
            @ApiPathParam(name = "id", description = "The project identifier")
161
            @PathParam("id") String id,
162
            @ApiBodyObject final ProjectDto dto
163
    ) {
164
        try {
165 1 1. update : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE
            checkIdIsNotNull(id);
166
            final Project project = dto.assemble();
167
            final Optional<Project> result = repository.amend(project);
168 1 1. update : removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE
            checkResult(id, result);
169 1 1. update : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::update to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
            return dto(result.get());
170
        } catch (NumberFormatException e) {
171
            LOGGER.error(PROJECT_IS_NOT_SAVED, e);
172
            throw badRequestException(PROJECT_IS_NOT_SAVED, e.getMessage());
173
        } catch (ConflictException e) {
174
            LOGGER.error(format("%s: %s", PROJECT_IS_NOT_SAVED, e.getMessage()));
175
            throw conflictException(PROJECT_IS_NOT_SAVED, e.getMessage());
176
        }
177
    }
178
179
    private Response created(final Project result) {
180 1 1. created : mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::created to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
        return Response.status(Response.Status.CREATED).entity(dto(result)).build();
181
    }
182
183
    private void checkIdIsNotNull(final String id) {
184 1 1. checkIdIsNotNull : negated conditional → NO_COVERAGE
        if (isNullOrEmpty(id)) {
185
            final String msg = "Bad get request. 'Id' must not be null";
186
            LOGGER.error(msg);
187
            throw notFoundException(msg);
188
        }
189
    }
190
191
    private void checkResult(final String id, final Optional<Project> result) {
192 1 1. checkResult : negated conditional → NO_COVERAGE
        if (!result.isPresent()) {
193
            final String msg = format("Project '%s' is not founded", id);
194
            LOGGER.error(msg);
195
            throw notFoundException(msg);
196
        }
197
    }
198
199
}

Mutations

56

1.1
Location : findAll
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::findAll to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

78

1.1
Location : load
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE

80

1.1
Location : load
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE

81

1.1
Location : load
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::load to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

103

1.1
Location : delete
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE

105

1.1
Location : delete
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE

106

1.1
Location : delete
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::delete to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

130

1.1
Location : create
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE

132

1.1
Location : create
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::create to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

165

1.1
Location : update
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkIdIsNotNull → NO_COVERAGE

168

1.1
Location : update
Killed by : none
removed call to org/maxur/perfmodel/backend/rest/resources/ProjectResource::checkResult → NO_COVERAGE

169

1.1
Location : update
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::update to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

180

1.1
Location : created
Killed by : none
mutated return of Object value for org/maxur/perfmodel/backend/rest/resources/ProjectResource::created to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

184

1.1
Location : checkIdIsNotNull
Killed by : none
negated conditional → NO_COVERAGE

192

1.1
Location : checkResult
Killed by : none
negated conditional → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 1.1.6