Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple micro save #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dk.sample.rest.bank.account.exposure.rs.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dk.sample.rest.bank.account.model;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand All @@ -16,7 +17,6 @@
import javax.persistence.UniqueConstraint;

import dk.sample.rest.common.persistence.jpa.AbstractAuditable;
import java.time.Instant;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dk.sample.rest.bank.virtualaccount;

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import dk.sample.rest.common.core.diagnostic.ContextInfo;
import dk.sample.rest.common.core.diagnostic.DiagnosticContext;
import java.io.UnsupportedEncodingException;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@SuppressWarnings("unchecked")
public class VirtualAccountServiceExposureIT {

private DiagnosticContext dCtx;

@Before
public void setupLogToken() {
dCtx = new DiagnosticContext(new ContextInfo() {
@Override
public String getLogToken() {
return "junit-" + System.currentTimeMillis();
}

@Override
public void setLogToken(String s) {

}
});
dCtx.start();
}

@After
public void removeLogToken() {
dCtx.stop();
}

@Test(expected = WebApplicationException.class)
public void testListAccounts() {
WebTarget target = ClientBuilder.newClient().register(JacksonJaxbJsonProvider.class).target("http://localhost:7001/sample");
Map<String, Object> response = target.path("virtualaccounts")
.request()
.accept("application/hal+json")
.header("X-Client-Version", "1.0.0")
.header("X-Service-Generation", "1")
.header("X-Log-Token", DiagnosticContext.getLogToken())
.get(Map.class);

fail("Should not find anything");
}

@Test(expected = WebApplicationException.class)
public void testListPlans() {
WebTarget target = ClientBuilder.newClient().register(JacksonJaxbJsonProvider.class).target("http://localhost:7001/sample");
Map<String, Object> response = target.path("microplans")
.request()
.accept("application/hal+json")
.header("X-Client-Version", "1.0.0")
.header("X-Service-Generation", "1")
.header("X-Log-Token", DiagnosticContext.getLogToken())
.get(Map.class);

fail("Should not find anything");
}
}
Binary file modified src/test/h2-data/virtualaccount.h2.db
Binary file not shown.
5 changes: 5 additions & 0 deletions virtual-account/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>dk.sample.rest</groupId>
<artifactId>account</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
package dk.sample.rest.bank.virtualaccount.exposure.rs;

import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.validation.constraints.Pattern;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import dk.nykredit.time.CurrentTime;

import dk.sample.rest.bank.virtualaccount.exposure.rs.model.MicroPlanRepresentation;
import dk.sample.rest.bank.virtualaccount.exposure.rs.model.MicroPlanUpdateRepresentation;
import dk.sample.rest.bank.virtualaccount.exposure.rs.model.MicroPlansRepresentation;
import dk.sample.rest.bank.virtualaccount.model.MicroPlan;
import dk.sample.rest.bank.virtualaccount.persistence.VirtualAccountArchivist;
import dk.sample.rest.common.core.logging.LogDuration;
import dk.sample.rest.common.rs.EntityResponseBuilder;
import dk.sample.rest.common.rs.error.ErrorRepresentation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.ResponseHeader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Exposing Virtul Accounts as REST service
*/
@Stateless
@Path("/microplans")
@PermitAll
@DeclareRoles("advisor")
@Api(value = "/microplans", tags = {"microplan"})
public class MicroPlanServiceExposure {
private static final Logger LOGGER = LoggerFactory.getLogger(MicroPlanServiceExposure.class);
private static final String ACCOUNT_ROOT = "/accounts/";
private static final String VIRTUAL_ACCOUNT_ROOT = "/virtualaccounts/";

private final Map<String, MicroPlansProducerMethod> plansProducer = new HashMap<>();
private final Map<String, MicroPlanProducerMethod> planProducers = new HashMap<>();


@EJB
private VirtualAccountArchivist archivist;


public MicroPlanServiceExposure() {
plansProducer.put("application/hal+json", this::listServiceGeneration1Version1);
plansProducer.put("application/hal+json;concept=microplans;v=1", this::listServiceGeneration1Version1);

planProducers.put("application/hal+json", this::getServiceGeneration1Version1);
planProducers.put("application/hal+json;concept=microplan;v=1", this::getServiceGeneration1Version1);
}

@GET
@Produces({"application/hal+json", "application/hal+json;concept=microplans;v=1"})
@ApiOperation(value = "lists accounts", response = MicroPlansRepresentation.class,
authorizations = {
@Authorization(value = "oauth2", scopes = {}),
@Authorization(value = "oauth2-cc", scopes = {}),
@Authorization(value = "oauth2-ac", scopes = {}),
@Authorization(value = "oauth2-rop", scopes = {}),
@Authorization(value = "Bearer")
},
extensions = {@Extension(name = "roles", properties = {
@ExtensionProperty(name = "advisor", value = "advisors are allowed getting every microplan"),
@ExtensionProperty(name = "customer", value = "customer only allowed getting own plans")}
)},
produces = "application/hal+json, application/hal+json;concept=microplan;v=1",
notes = "List all plans in a default projection, which is MicroPlan version 1" +
"Supported projections and versions are: " +
"MicroPlans in version 1 " +
"The Accept header for the default version is application/hal+json;concept=microplans;v=1.... " +
"The format for the default version is {....}", nickname = "listMicroPlans")
@ApiResponses(value = {
@ApiResponse(code = 415, message = "Content type not supported.")
})
public Response list(@Context UriInfo uriInfo, @Context Request request, @HeaderParam("Accept") String accept) {
return plansProducer.getOrDefault(accept, this::handleUnsupportedContentType).getResponse(uriInfo, request);
}

@GET
@Path("{microplan}")
@Produces({"application/hal+json", "application/hal+json;concept=microplan;v=1"})
@ApiOperation(value = "gets the information from a single position", response = MicroPlanRepresentation.class,
authorizations = {
@Authorization(value = "oauth2", scopes = {}),
@Authorization(value = "oauth2-cc", scopes = {}),
@Authorization(value = "oauth2-ac", scopes = {}),
@Authorization(value = "oauth2-rop", scopes = {}),
@Authorization(value = "Bearer")
},
extensions = {@Extension(name = "roles", properties = {
@ExtensionProperty(name = "customer", value = "customer allows getting own information"),
@ExtensionProperty(name = "advisor", value = "advisor allows getting all information")}
)},
produces = "application/hal+json, application/hal+json;concept=microplan;v=1",
notes = "obtain a single plan " +
" Supported projections and versions are:" +
" MicroPlan in version1 " +
" The format of the default version is .... - The Accept Header is not marked as required in the " +
"swagger - but it is needed - we are working on a solution to that", nickname = "getMicroPlan")
@ApiResponses(value = {
@ApiResponse(code = 404, message = "micro plan not found.")
})
public Response get(@Context UriInfo uriInfo, @Context Request request,
@PathParam("planname") @Pattern(regexp = "^[a_z]{50}$") String planname,
@HeaderParam("Accept") String accept) {
LOGGER.info("Default version of microplan collected");
return planProducers.getOrDefault(accept, this::handleUnsupportedContentType)
.getResponse(uriInfo, request, planname);
}

@PUT
@RolesAllowed("system")
@Path("{microplan}")
@Produces({"application/hal+json"})
@Consumes("application/json")
@LogDuration(limit = 50)
@ApiOperation(value = "Create new or update existing MicroPlan", response = MicroPlanRepresentation.class,
authorizations = {
@Authorization(value = "oauth2", scopes = {}),
@Authorization(value = "oauth2-cc", scopes = {}),
@Authorization(value = "oauth2-ac", scopes = {}),
@Authorization(value = "oauth2-rop", scopes = {}),
@Authorization(value = "Bearer")
},
extensions = {@Extension(name = "roles", properties = {
@ExtensionProperty(name = "customer", value = "customer allows getting own customer"),
@ExtensionProperty(name = "system", value = "system allows getting every customer")
})},
notes = "PUT is used to create a new microplan or used to alter the values attached to the micro plan account",
consumes = "application/json",
produces = "application/hal+json, application/hal+json;concept=microplan;v=1",
nickname = "updateMicroPlan")
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Could not update or create the plan", response = ErrorRepresentation.class),
@ApiResponse(code = 415, message = "The content-Type was not supported"),
@ApiResponse(code = 201, message = "New VirtualAccount Created", response = MicroPlanRepresentation.class,
responseHeaders = {
@ResponseHeader(name = "MicroPlan", description = "a link to the created resource"),
@ResponseHeader(name = "Content-Type", description = "a link to the created resource"),
@ResponseHeader(name = "X-Log-Token", description = "an id for reference purposes in logs etc")
})
})
public Response createOrUpdate(@Context UriInfo uriInfo, @Context Request request,
@PathParam("name") @Pattern(regexp = "^[a_z]{50}$") String name,
@ApiParam(value = "plan") MicroPlanUpdateRepresentation plan) {
if (!name.equals(plan.getName())) {
throw new WebApplicationException(Response.Status.BAD_REQUEST);
}

Optional<MicroPlan> microPlan = archivist.findPlan(name);
MicroPlan mp;
if (microPlan.isPresent()) {
mp = microPlan.get();
mp.setDescription(plan.getDescription());
} else {
mp = new MicroPlan(plan.getName(), plan.getDescription(), plan.getVirtualAccount(),
plan.getPrimaryAccount(), plan.getSecondaryAccount(), plan.getTertiaryAccount());
}
archivist.save(mp);

CacheControl cc = new CacheControl();
int maxAge = 30;
cc.setMaxAge(maxAge);

return Response.created(URI.create(uriInfo.getPath()))
.entity(new MicroPlanRepresentation(mp, uriInfo))
.cacheControl(cc).expires(Date.from(CurrentTime.now().plusSeconds(maxAge)))
.status(201)
.type("application/hal+json;concept=MicroPlan;v=1")
.build();
}

Response listServiceGeneration1Version1(UriInfo uriInfo, Request request) {
List<MicroPlan> plans = archivist.listPlans();
return new EntityResponseBuilder<>(plans, list -> new MicroPlansRepresentation(list, uriInfo))
.name("microplan")
.version("1")
.maxAge(10)
.build(request);
}

Response getServiceGeneration1Version1(UriInfo uriInfo, Request request, String planname) {
Optional<MicroPlan> plan = archivist.findPlan(planname);
if (plan.isPresent()) {
MicroPlan p = plan.get();
return new EntityResponseBuilder<>(p, pl -> new MicroPlanRepresentation(pl, uriInfo))
.name("microplan")
.version("1")
.maxAge(10)
.build(request);
} else {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
}

interface MicroPlansProducerMethod {
Response getResponse(UriInfo uriInfo, Request request);
}

interface MicroPlanProducerMethod {
Response getResponse(UriInfo uriInfo, Request request, String name);
}

Response handleUnsupportedContentType(UriInfo uriInfo, Request request, String... parms) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).build();
}

}
Loading