标签:
package com.example; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import com.example.dao.DeviceDao; import com.example.domain.Device; @Path("device") public class DeviceResource { private final DeviceDao deviceDao; public DeviceResource() { deviceDao = new DeviceDao(); } @GET @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Device get(@QueryParam("ip") final String deviceIp) { Device result = null; if (deviceIp != null) { result = deviceDao.getDevice(deviceIp); } return result; } @PUT @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Device put(final Device device) { Device result = null; if (device != null) { result = deviceDao.updateDevice(device); } return result; } }
package com.example.domain; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "device") public class Device { private String deviceIp; private int deviceStatus; public Device() { } public Device(String deviceIp) { super(); this.deviceIp = deviceIp; } @XmlAttribute public String getIp() { return deviceIp; } public void setIp(String deviceIp) { this.deviceIp = deviceIp; } @XmlAttribute public int getStatus() { return deviceStatus; } public void setStatus(int deviceStatus) { this.deviceStatus = deviceStatus; } }
package com.example.dao; import java.util.concurrent.ConcurrentHashMap; import com.example.domain.Device; public class DeviceDao { ConcurrentHashMap<String, Device> fakeDB = new ConcurrentHashMap<>(); public DeviceDao() { fakeDB.put("10.11.58.163", new Device("10.11.58.163")); fakeDB.put("10.11.58.184", new Device("10.11.58.184")); } public Device getDevice(String ip) { return fakeDB.get(ip); } public Device updateDevice(Device device) { String ip = device.getIp(); fakeDB.put(ip, device); return fakeDB.get(ip); } }
package com.example; import javax.ws.rs.client.Client; 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 org.glassfish.grizzly.http.server.HttpServer; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import com.example.domain.Device; public class DeviceResourceTest { private HttpServer server; private WebTarget target; @Before public void setUp() throws Exception { server = Main.startServer(); final Client c = ClientBuilder.newClient(); target = c.target(Main.BASE_URI); } @After public void tearDown() throws Exception { server.shutdownNow(); } @Test public void testGetDevice() { final String testIp = "10.11.58.184"; final Device device = target.path("device").queryParam("ip", testIp).request().get(Device.class); Assert.assertEquals(testIp, device.getIp()); } @Test public void testUpdateDevice() { final String testIp = "10.11.58.163"; final Device device = new Device(testIp); device.setStatus(1); Entity<Device> entity = Entity.entity(device, MediaType.APPLICATION_XML_TYPE); final Device result = target.path("device").request().put(entity, Device.class); Assert.assertEquals(1, result.getStatus()); } }
测试方式 1:通过DeviceResourceTest
2:通过google postman插件
标签:
原文地址:http://www.cnblogs.com/superGG/p/5156851.html