Phaneendra Manda
Committed by Thomas Vachuska

[ONOS-3161]Port pair UT updated for reivew comments

Change-Id: Ia442bc098f1382f6347c0aa301b03b452df4e5f7
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 16
17 package org.onosproject.vtnweb.resources; 17 package org.onosproject.vtnweb.resources;
18 18
19 -import static javax.ws.rs.core.Response.Status.NOT_FOUND;
20 import static javax.ws.rs.core.Response.Status.OK; 19 import static javax.ws.rs.core.Response.Status.OK;
21 import static org.onlab.util.Tools.nullIsNotFound; 20 import static org.onlab.util.Tools.nullIsNotFound;
22 21
...@@ -38,12 +37,10 @@ import org.onosproject.rest.AbstractWebResource; ...@@ -38,12 +37,10 @@ import org.onosproject.rest.AbstractWebResource;
38 import org.onosproject.vtnrsc.PortPair; 37 import org.onosproject.vtnrsc.PortPair;
39 import org.onosproject.vtnrsc.PortPairId; 38 import org.onosproject.vtnrsc.PortPairId;
40 import org.onosproject.vtnrsc.portpair.PortPairService; 39 import org.onosproject.vtnrsc.portpair.PortPairService;
41 -import org.onosproject.vtnweb.web.PortPairCodec;
42 import org.slf4j.Logger; 40 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory; 41 import org.slf4j.LoggerFactory;
44 42
45 import com.fasterxml.jackson.databind.JsonNode; 43 import com.fasterxml.jackson.databind.JsonNode;
46 -import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.node.ArrayNode; 44 import com.fasterxml.jackson.databind.node.ArrayNode;
48 import com.fasterxml.jackson.databind.node.ObjectNode; 45 import com.fasterxml.jackson.databind.node.ObjectNode;
49 46
...@@ -54,7 +51,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode; ...@@ -54,7 +51,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
54 public class PortPairWebResource extends AbstractWebResource { 51 public class PortPairWebResource extends AbstractWebResource {
55 52
56 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class); 53 private final Logger log = LoggerFactory.getLogger(PortPairWebResource.class);
57 - private final PortPairService service = get(PortPairService.class);
58 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found"; 54 public static final String PORT_PAIR_NOT_FOUND = "Port pair not found";
59 public static final String PORT_PAIR_ID_EXIST = "Port pair exists"; 55 public static final String PORT_PAIR_ID_EXIST = "Port pair exists";
60 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier"; 56 public static final String PORT_PAIR_ID_NOT_EXIST = "Port pair does not exist with identifier";
...@@ -67,12 +63,12 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -67,12 +63,12 @@ public class PortPairWebResource extends AbstractWebResource {
67 @GET 63 @GET
68 @Produces(MediaType.APPLICATION_JSON) 64 @Produces(MediaType.APPLICATION_JSON)
69 public Response getPortPairs() { 65 public Response getPortPairs() {
70 - Iterable<PortPair> portPairs = service.getPortPairs(); 66 + Iterable<PortPair> portPairs = get(PortPairService.class).getPortPairs();
71 - ObjectNode result = new ObjectMapper().createObjectNode(); 67 + ObjectNode result = mapper().createObjectNode();
72 ArrayNode portPairEntry = result.putArray("port_pairs"); 68 ArrayNode portPairEntry = result.putArray("port_pairs");
73 if (portPairs != null) { 69 if (portPairs != null) {
74 for (final PortPair portPair : portPairs) { 70 for (final PortPair portPair : portPairs) {
75 - portPairEntry.add(new PortPairCodec().encode(portPair, this)); 71 + portPairEntry.add(codec(PortPair.class).encode(portPair, this));
76 } 72 }
77 } 73 }
78 return ok(result.toString()).build(); 74 return ok(result.toString()).build();
...@@ -88,13 +84,10 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -88,13 +84,10 @@ public class PortPairWebResource extends AbstractWebResource {
88 @Path("{pair_id}") 84 @Path("{pair_id}")
89 @Produces(MediaType.APPLICATION_JSON) 85 @Produces(MediaType.APPLICATION_JSON)
90 public Response getPortPair(@PathParam("pair_id") String id) { 86 public Response getPortPair(@PathParam("pair_id") String id) {
91 - 87 + PortPair portPair = nullIsNotFound(get(PortPairService.class).getPortPair(PortPairId.of(id)),
92 - if (!service.exists(PortPairId.of(id))) { 88 + PORT_PAIR_NOT_FOUND);
93 - return Response.status(NOT_FOUND).entity(PORT_PAIR_NOT_FOUND).build(); 89 + ObjectNode result = mapper().createObjectNode();
94 - } 90 + result.set("port_pair", codec(PortPair.class).encode(portPair, this));
95 - PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), PORT_PAIR_NOT_FOUND);
96 - ObjectNode result = new ObjectMapper().createObjectNode();
97 - result.set("port_pair", new PortPairCodec().encode(portPair, this));
98 return ok(result.toString()).build(); 91 return ok(result.toString()).build();
99 } 92 }
100 93
...@@ -110,11 +103,11 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -110,11 +103,11 @@ public class PortPairWebResource extends AbstractWebResource {
110 @Produces(MediaType.APPLICATION_JSON) 103 @Produces(MediaType.APPLICATION_JSON)
111 public Response createPortPair(InputStream stream) { 104 public Response createPortPair(InputStream stream) {
112 try { 105 try {
113 - ObjectMapper mapper = new ObjectMapper(); 106 + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
114 - ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
115 JsonNode port = jsonTree.get("port_pair"); 107 JsonNode port = jsonTree.get("port_pair");
116 - PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this); 108 + PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
117 - Boolean isSuccess = nullIsNotFound(service.createPortPair(portPair), PORT_PAIR_NOT_FOUND); 109 + Boolean isSuccess = nullIsNotFound(get(PortPairService.class).createPortPair(portPair),
110 + PORT_PAIR_NOT_FOUND);
118 return Response.status(OK).entity(isSuccess.toString()).build(); 111 return Response.status(OK).entity(isSuccess.toString()).build();
119 } catch (IOException e) { 112 } catch (IOException e) {
120 log.error("Exception while creating port pair {}.", e.toString()); 113 log.error("Exception while creating port pair {}.", e.toString());
...@@ -136,11 +129,11 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -136,11 +129,11 @@ public class PortPairWebResource extends AbstractWebResource {
136 public Response updatePortPair(@PathParam("pair_id") String id, 129 public Response updatePortPair(@PathParam("pair_id") String id,
137 final InputStream stream) { 130 final InputStream stream) {
138 try { 131 try {
139 - ObjectMapper mapper = new ObjectMapper(); 132 + ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
140 - ObjectNode jsonTree = (ObjectNode) mapper.readTree(stream);
141 JsonNode port = jsonTree.get("port_pair"); 133 JsonNode port = jsonTree.get("port_pair");
142 - PortPair portPair = new PortPairCodec().decode((ObjectNode) port, this); 134 + PortPair portPair = codec(PortPair.class).decode((ObjectNode) port, this);
143 - Boolean isSuccess = nullIsNotFound(service.updatePortPair(portPair), PORT_PAIR_NOT_FOUND); 135 + Boolean isSuccess = nullIsNotFound(get(PortPairService.class).updatePortPair(portPair),
136 + PORT_PAIR_NOT_FOUND);
144 return Response.status(OK).entity(isSuccess.toString()).build(); 137 return Response.status(OK).entity(isSuccess.toString()).build();
145 } catch (IOException e) { 138 } catch (IOException e) {
146 log.error("Update port pair failed because of exception {}.", e.toString()); 139 log.error("Update port pair failed because of exception {}.", e.toString());
...@@ -158,7 +151,7 @@ public class PortPairWebResource extends AbstractWebResource { ...@@ -158,7 +151,7 @@ public class PortPairWebResource extends AbstractWebResource {
158 public void deletePortPair(@PathParam("pair_id") String id) { 151 public void deletePortPair(@PathParam("pair_id") String id) {
159 152
160 PortPairId portPairId = PortPairId.of(id); 153 PortPairId portPairId = PortPairId.of(id);
161 - Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), PORT_PAIR_NOT_FOUND); 154 + Boolean isSuccess = nullIsNotFound(get(PortPairService.class).removePortPair(portPairId), PORT_PAIR_NOT_FOUND);
162 if (!isSuccess) { 155 if (!isSuccess) {
163 log.debug("Port pair identifier {} does not exist", id); 156 log.debug("Port pair identifier {} does not exist", id);
164 } 157 }
......
...@@ -39,10 +39,12 @@ import org.junit.Test; ...@@ -39,10 +39,12 @@ import org.junit.Test;
39 import org.onlab.osgi.ServiceDirectory; 39 import org.onlab.osgi.ServiceDirectory;
40 import org.onlab.osgi.TestServiceDirectory; 40 import org.onlab.osgi.TestServiceDirectory;
41 import org.onlab.rest.BaseResource; 41 import org.onlab.rest.BaseResource;
42 +import org.onosproject.codec.CodecService;
42 import org.onosproject.vtnrsc.PortPair; 43 import org.onosproject.vtnrsc.PortPair;
43 import org.onosproject.vtnrsc.PortPairId; 44 import org.onosproject.vtnrsc.PortPairId;
44 import org.onosproject.vtnrsc.TenantId; 45 import org.onosproject.vtnrsc.TenantId;
45 import org.onosproject.vtnrsc.portpair.PortPairService; 46 import org.onosproject.vtnrsc.portpair.PortPairService;
47 +import org.onosproject.vtnweb.web.SfcCodecContext;
46 48
47 import com.eclipsesource.json.JsonObject; 49 import com.eclipsesource.json.JsonObject;
48 import com.sun.jersey.api.client.ClientResponse; 50 import com.sun.jersey.api.client.ClientResponse;
...@@ -129,7 +131,10 @@ public class PortPairResourceTest extends VtnResourceTest { ...@@ -129,7 +131,10 @@ public class PortPairResourceTest extends VtnResourceTest {
129 */ 131 */
130 @Before 132 @Before
131 public void setUpTest() { 133 public void setUpTest() {
132 - ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService); 134 +
135 + SfcCodecContext context = new SfcCodecContext();
136 + ServiceDirectory testDirectory = new TestServiceDirectory().add(PortPairService.class, portPairService)
137 + .add(CodecService.class, context.codecManager());
133 BaseResource.setServiceDirectory(testDirectory); 138 BaseResource.setServiceDirectory(testDirectory);
134 139
135 } 140 }
......