You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by js...@apache.org on 2013/03/11 20:26:50 UTC
svn commit: r1455293 [3/4] - in /incubator/ambari/trunk: ./
ambari-server/src/main/java/org/apache/ambari/server/api/handlers/
ambari-server/src/main/java/org/apache/ambari/server/api/services/
ambari-server/src/main/java/org/apache/ambari/server/api/s...
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ConfigurationServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ConfigurationServiceTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ConfigurationServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ConfigurationServiceTest.java Mon Mar 11 19:26:49 2013
@@ -21,60 +21,64 @@ import static org.junit.Assert.assertEqu
import javax.ws.rs.core.*;
-import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.junit.Test;
+import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
public class ConfigurationServiceTest extends BaseServiceTest {
-
- @Test
- public void testCreateConfiguration() throws Exception{
- String clusterName = "clusterName";
-
- String body = "{ \"type\":\"hdfs-site\", \"tag\":\"my-new-tag\"," +
- "\"properties\":{ \"key1\":\"value1\", \"key2\":\"value2\" } }";
-
- registerExpectations(Request.Type.POST, body, 200, false);
- replayMocks();
-
- //test
- ConfigurationService configService = new TestConfigurationService(getResource(), clusterName, getRequestFactory(), getRequestHandler());
- Response response = configService.createConfigurations(body, getHttpHeaders(), getUriInfo());
- verifyResults(response, 200);
+ public List<ServiceTestInvocation> getTestInvocations() throws Exception {
+ List<ServiceTestInvocation> listInvocations = new ArrayList<ServiceTestInvocation>();
+
+ //getConfigurations
+ ConfigurationService service = new TestConfigurationService("clusterName");
+ Method m = service.getClass().getMethod("getConfigurations", HttpHeaders.class, UriInfo.class);
+ Object[] args = new Object[] {getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //createConfigurations
+ service = new TestConfigurationService("clusterName");
+ m = service.getClass().getMethod("createConfigurations", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body"));
+
+ return listInvocations;
}
-
+
+
private class TestConfigurationService extends ConfigurationService {
- private RequestFactory m_requestFactory;
- private RequestHandler m_requestHandler;
- private ResourceInstance m_resourceInstance;
private String m_clusterId;
- private TestConfigurationService(ResourceInstance resourceInstance, String clusterId, RequestFactory requestFactory,
- RequestHandler handler) {
+ private TestConfigurationService(String clusterId) {
super(clusterId);
- m_resourceInstance = resourceInstance;
m_clusterId = clusterId;
- m_requestFactory = requestFactory;
- m_requestHandler = handler;
}
@Override
ResourceInstance createConfigurationResource(String clusterName) {
assertEquals(m_clusterId, clusterName);
- return m_resourceInstance;
+ return getTestResource();
}
@Override
RequestFactory getRequestFactory() {
- return m_requestFactory;
+ return getTestRequestFactory();
}
@Override
- RequestHandler getRequestHandler(Request.Type requestType) {
- return m_requestHandler;
- }
+ protected RequestBodyParser getBodyParser() {
+ return getTestBodyParser();
+ }
+
+ @Override
+ protected ResultSerializer getResultSerializer() {
+ return getTestResultSerializer();
+ }
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/DeleteRequestTest.java Mon Mar 11 19:26:49 2013
@@ -18,7 +18,9 @@
package org.apache.ambari.server.api.services;
+import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.predicate.PredicateCompiler;
+import org.apache.ambari.server.api.resources.ResourceInstance;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -31,28 +33,28 @@ import javax.ws.rs.core.UriInfo;
*/
public class DeleteRequestTest extends BaseRequestTest {
@Test
- public void testRequestType() {
- assertSame(Request.Type.DELETE, new DeleteRequest(null, null, null, null).getRequestType());
+ public void testRequestType() throws Exception {
+ Request r = new DeleteRequest(null, new RequestBody(), null, null);
+ assertSame(Request.Type.DELETE, r.getRequestType());
}
- @Test
- public void testGetQueryPredicate() throws Exception {
- String uri = "http://foo.bar.com/api/v1/clusters?foo=bar&orProp1=5|orProp2!=6|orProp3<100&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2";
- super.testGetQueryPredicate(uri);
- }
-
- @Test
- public void testGetFields() {
- String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
- super.testGetFields(fields);
- }
-
- protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
- return new DeleteRequest(headers, body, uriInfo, null) {
+ protected Request getTestRequest(HttpHeaders headers, RequestBody body, UriInfo uriInfo, final PredicateCompiler compiler,
+ final RequestHandler handler, final ResultPostProcessor processor, ResourceInstance resource) {
+ return new DeleteRequest(headers, body, uriInfo, resource) {
@Override
protected PredicateCompiler getPredicateCompiler() {
return compiler;
}
+
+ @Override
+ protected RequestHandler getRequestHandler() {
+ return handler;
+ }
+
+ @Override
+ public ResultPostProcessor getResultPostProcessor() {
+ return processor;
+ }
};
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/GetRequestTest.java Mon Mar 11 19:26:49 2013
@@ -18,7 +18,9 @@
package org.apache.ambari.server.api.services;
+import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.predicate.PredicateCompiler;
+import org.apache.ambari.server.api.resources.ResourceInstance;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -32,28 +34,29 @@ import javax.ws.rs.core.UriInfo;
public class GetRequestTest extends BaseRequestTest {
@Test
- public void testRequestType() {
- assertSame(Request.Type.GET, new GetRequest(null, null, null, null).getRequestType());
+ public void testRequestType() throws Exception {
+ Request r = new GetRequest(null, new RequestBody(), null, null);
+ assertSame(Request.Type.GET, r.getRequestType());
}
- @Test
- public void testGetQueryPredicate() throws Exception {
- String uri = "http://foo.bar.com/api/v1/clusters?foo=bar&orProp1=5|orProp2!=6|orProp3<100&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2";
- super.testGetQueryPredicate(uri);
- }
-
- @Test
- public void testGetFields() {
- String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
- super.testGetFields(fields);
- }
- protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
- return new GetRequest(headers, body, uriInfo, null) {
+ protected Request getTestRequest(HttpHeaders headers, RequestBody body, UriInfo uriInfo, final PredicateCompiler compiler,
+ final RequestHandler handler, final ResultPostProcessor processor, ResourceInstance resource) {
+ return new GetRequest(headers, body, uriInfo, resource) {
@Override
protected PredicateCompiler getPredicateCompiler() {
return compiler;
}
+
+ @Override
+ protected RequestHandler getRequestHandler() {
+ return handler;
+ }
+
+ @Override
+ public ResultPostProcessor getResultPostProcessor() {
+ return processor;
+ }
};
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostComponentServiceTest.java Mon Mar 11 19:26:49 2013
@@ -19,11 +19,16 @@
package org.apache.ambari.server.api.services;
-import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.junit.Test;
+import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
-import javax.ws.rs.core.Response;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
import static org.junit.Assert.assertEquals;
@@ -31,248 +36,88 @@ import static org.junit.Assert.assertEqu
* Unit tests for HostComponentService.
*/
public class HostComponentServiceTest extends BaseServiceTest {
- @Test
- public void testGetHostComponent() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.getHostComponent(getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetHostComponent__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
-
- registerExpectations(Request.Type.GET, null, 404, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.getHostComponent(getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 404);
- }
-
- @Test
- public void testGetHostComponents() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, null, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.getHostComponents(getHttpHeaders(), getUriInfo());
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetHostComponents__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, null, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.getHostComponents(getHttpHeaders(), getUriInfo());
- verifyResults(response, 500);
- }
-
- @Test
- public void testCreateHostComponent() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
- String body = "body";
-
- registerExpectations(Request.Type.POST, body, 201, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.createHostComponent(body, getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 201);
- }
-
- @Test
- public void testCreateHostComponent__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
- String body = "body";
-
- registerExpectations(Request.Type.POST, body, 500, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.createHostComponent(body, getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 500);
- }
- @Test
- public void testUpdateHostComponent() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
+ public List<ServiceTestInvocation> getTestInvocations() throws Exception {
+ List<ServiceTestInvocation> listInvocations = new ArrayList<ServiceTestInvocation>();
- Response response = hostComponentService.updateHostComponent(body, getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateHostComponent__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.updateHostComponent(body, getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 500);
- }
-
- @Test
- public void testUpdateHostComponents() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, null, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.updateHostComponents(body, getHttpHeaders(), getUriInfo());
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateHostComponents__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, null, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.updateHostComponents(body, getHttpHeaders(), getUriInfo());
- verifyResults(response, 500);
- }
-
- @Test
- public void testDeleteHostComponent() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
-
- registerExpectations(Request.Type.DELETE, null, 200, false);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
-
- Response response = hostComponentService.deleteHostComponent(getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 200);
- }
-
- @Test
- public void testDeleteHostComponent__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String hostComponentName = "hostComponentName";
-
- registerExpectations(Request.Type.DELETE, null, 500, true);
- replayMocks();
-
- //test
- HostComponentService hostComponentService = new TestHostComponentService(getResource(), clusterName,
- hostName, hostComponentName, getRequestFactory(), getRequestHandler());
+ //getHostComponent
+ HostComponentService componentService = new TestHostComponentService("clusterName", "serviceName", "componentName");
+ Method m = componentService.getClass().getMethod("getHostComponent", HttpHeaders.class, UriInfo.class, String.class);
+ Object[] args = new Object[] {getHttpHeaders(), getUriInfo(), "componentName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, componentService, m, args, null));
+
+ //getHostComponents
+ componentService = new TestHostComponentService("clusterName", "serviceName", null);
+ m = componentService.getClass().getMethod("getHostComponents", HttpHeaders.class, UriInfo.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, componentService, m, args, null));
+
+ //createHostComponent
+ componentService = new TestHostComponentService("clusterName", "serviceName", "componentName");
+ m = componentService.getClass().getMethod("createHostComponent", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "componentName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, componentService, m, args, "body"));
+
+ //createHostComponents
+ componentService = new TestHostComponentService("clusterName", "serviceName", null);
+ m = componentService.getClass().getMethod("createHostComponents", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, componentService, m, args, "body"));
+
+ //updateHostComponent
+ componentService = new TestHostComponentService("clusterName", "serviceName", "componentName");
+ m = componentService.getClass().getMethod("updateHostComponent", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "componentName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, componentService, m, args, "body"));
+
+ //updateHostComponents
+ componentService = new TestHostComponentService("clusterName", "serviceName", null);
+ m = componentService.getClass().getMethod("updateHostComponents", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, componentService, m, args, "body"));
+
+ //deleteHostComponent
+ componentService = new TestHostComponentService("clusterName", "serviceName", "componentName");
+ m = componentService.getClass().getMethod("deleteHostComponent", HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo(), "componentName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.DELETE, componentService, m, args, null));
- Response response = hostComponentService.deleteHostComponent(getHttpHeaders(), getUriInfo(), hostComponentName);
- verifyResults(response, 500);
+ return listInvocations;
}
private class TestHostComponentService extends HostComponentService {
- private RequestFactory m_requestFactory;
- private RequestHandler m_requestHandler;
- private ResourceInstance m_resourceDef;
private String m_clusterId;
private String m_hostId;
private String m_hostComponentId;
- private TestHostComponentService(ResourceInstance resourceDef, String clusterId, String hostId, String hostComponentId,
- RequestFactory requestFactory, RequestHandler handler) {
+ private TestHostComponentService(String clusterId, String hostId, String hostComponentId) {
super(clusterId, hostId);
- m_resourceDef = resourceDef;
m_clusterId = clusterId;
m_hostId = hostId;
m_hostComponentId = hostComponentId;
- m_requestFactory = requestFactory;
- m_requestHandler = handler;
}
-
@Override
ResourceInstance createHostComponentResource(String clusterName, String hostName, String hostComponentName) {
assertEquals(m_clusterId, clusterName);
assertEquals(m_hostId, hostName);
assertEquals(m_hostComponentId, hostComponentName);
- return m_resourceDef;
+ return getTestResource();
}
@Override
RequestFactory getRequestFactory() {
- return m_requestFactory;
+ return getTestRequestFactory();
+ }
+
+ @Override
+ protected RequestBodyParser getBodyParser() {
+ return getTestBodyParser();
}
@Override
- RequestHandler getRequestHandler(Request.Type requestType) {
- return m_requestHandler;
+ protected ResultSerializer getResultSerializer() {
+ return getTestResultSerializer();
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/HostServiceTest.java Mon Mar 11 19:26:49 2013
@@ -19,13 +19,17 @@
package org.apache.ambari.server.api.services;
-
-import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.junit.Test;
-import javax.ws.rs.core.Response;
+import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
+
+import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
import static org.junit.Assert.assertEquals;
/**
@@ -33,266 +37,84 @@ import static org.junit.Assert.assertEqu
*/
public class HostServiceTest extends BaseServiceTest {
- @Test
- public void testGetHost() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.getHost(getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetHost__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.getHost(getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetHosts() {
- String clusterName = "clusterName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.getHosts(getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetHosts__ErrorState() {
- String clusterName = "clusterName";
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.getHosts(getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testCreateHost() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.POST, body, 201, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.createHost(body, getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 201);
- }
-
- @Test
- public void testCreateHost__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.POST, body, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.createHost(body, getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testCreateHosts() {
- String clusterName = "clusterName";
- String body = "[ " +
- "{\"Hosts\" : {" +
- " \"cluster_name\" : \"mycluster\"," +
- " \"host_name\" : \"host1\"" +
- " }" +
- "}," +
- "{\"Hosts\" : {" +
- " \"cluster_name\" : \"mycluster\"," +
- " \"host_name\" : \"host2\"" +
- " }" +
- "}," +
- "{\"Hosts\" : {" +
- " \"cluster_name\" : \"mycluster\"," +
- " \"host_name\" : \"host3\"" +
- " }" +
- "}]";
-
- registerExpectations(Request.Type.POST, body, 201, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.createHosts(body, getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 201);
- }
+ public List<ServiceTestInvocation> getTestInvocations() throws Exception {
+ List<ServiceTestInvocation> listInvocations = new ArrayList<ServiceTestInvocation>();
- @Test
- public void testCreateHosts__ErrorState() {
- String clusterName = "clusterName";
- String body = "body";
-
- registerExpectations(Request.Type.POST, body, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.createHosts(body, getHttpHeaders(), getUriInfo());
+ //getHost
+ HostService service = new TestHostService("clusterName", "hostName");
+ Method m = service.getClass().getMethod("getHost", HttpHeaders.class, UriInfo.class, String.class);
+ Object[] args = new Object[] {getHttpHeaders(), getUriInfo(), "hostName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //getHosts
+ service = new TestHostService("clusterName", null);
+ m = service.getClass().getMethod("getHosts", HttpHeaders.class, UriInfo.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //createHost
+ service = new TestHostService("clusterName", "hostName");
+ m = service.getClass().getMethod("createHost", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "hostName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body"));
+
+ //createHosts
+ service = new TestHostService("clusterName", null);
+ m = service.getClass().getMethod("createHosts", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body"));
+
+ //updateHost
+ service = new TestHostService("clusterName", "hostName");
+ m = service.getClass().getMethod("updateHost", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "hostName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, service, m, args, "body"));
+
+ //updateHosts
+ service = new TestHostService("clusterName", null);
+ m = service.getClass().getMethod("updateHosts", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, service, m, args, "body"));
+
+ //deleteHost
+ service = new TestHostService("clusterName", "hostName");
+ m = service.getClass().getMethod("deleteHost", HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo(), "hostName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.DELETE, service, m, args, null));
- verifyResults(response, 500);
+ return listInvocations;
}
- @Test
- public void testUpdateHost() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateHost(body, getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateHost__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateHost(body, getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testUpdateHosts() {
- String clusterName = "clusterName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateHosts(body, getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateHosts__ErrorState() {
- String clusterName = "clusterName";
- String body = "body";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateHosts(body, getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testDeleteHost() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.DELETE, null, 200, false);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.deleteHost(getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testDeleteHost__ErrorState() {
- String clusterName = "clusterName";
- String hostName = "hostName";
-
- registerExpectations(Request.Type.DELETE, null, 500, true);
- replayMocks();
-
- //test
- HostService hostService = new TestHostService(getResource(), clusterName, hostName, getRequestFactory(), getRequestHandler());
- Response response = hostService.deleteHost(getHttpHeaders(), getUriInfo(), hostName);
-
- verifyResults(response, 500);
- }
-
-
private class TestHostService extends HostService {
- private RequestFactory m_requestFactory;
- private RequestHandler m_requestHandler;
- private ResourceInstance m_resourceDef;
private String m_clusterId;
private String m_hostId;
- private TestHostService(ResourceInstance resourceDef, String clusterId, String hostId, RequestFactory requestFactory,
- RequestHandler handler) {
+ private TestHostService(String clusterId, String hostId) {
super(clusterId);
- m_resourceDef = resourceDef;
m_clusterId = clusterId;
m_hostId = hostId;
- m_requestFactory = requestFactory;
- m_requestHandler = handler;
}
@Override
ResourceInstance createHostResource(String clusterName, String hostName, UriInfo ui) {
assertEquals(m_clusterId, clusterName);
assertEquals(m_hostId, hostName);
- return m_resourceDef;
+ return getTestResource();
}
@Override
RequestFactory getRequestFactory() {
- return m_requestFactory;
+ return getTestRequestFactory();
+ }
+
+ @Override
+ protected RequestBodyParser getBodyParser() {
+ return getTestBodyParser();
}
@Override
- RequestHandler getRequestHandler(Request.Type requestType) {
- return m_requestHandler;
+ protected ResultSerializer getResultSerializer() {
+ return getTestResultSerializer();
}
}
}
Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/NamedPropertySetTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/NamedPropertySetTest.java?rev=1455293&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/NamedPropertySetTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/NamedPropertySetTest.java Mon Mar 11 19:26:49 2013
@@ -0,0 +1,68 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.api.services;
+
+import org.junit.Test;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * NamedPropertySet unit tests.
+ */
+public class NamedPropertySetTest {
+
+ @Test
+ public void testGetters() {
+ Map<String, Object> mapProps = new HashMap<String, Object>();
+ mapProps.put("foo", "bar");
+
+ NamedPropertySet propertySet = new NamedPropertySet("foo", mapProps);
+ assertEquals("foo", propertySet.getName());
+ assertEquals(mapProps, propertySet.getProperties());
+ }
+
+ @Test
+ public void testEquals() {
+ Map<String, Object> mapProps = new HashMap<String, Object>();
+ mapProps.put("foo", "bar");
+
+ NamedPropertySet propertySet = new NamedPropertySet("foo", mapProps);
+ NamedPropertySet propertySet2 = new NamedPropertySet("foo", mapProps);
+
+ assertEquals(propertySet, propertySet2);
+
+ NamedPropertySet propertySet3 = new NamedPropertySet("bar", mapProps);
+ assertFalse(propertySet.equals(propertySet3));
+
+ NamedPropertySet propertySet4 = new NamedPropertySet("foo", new HashMap<String, Object>());
+ assertFalse(propertySet.equals(propertySet4));
+ }
+
+ @Test
+ public void testHashCode() {
+ Map<String, Object> mapProps = new HashMap<String, Object>();
+
+ NamedPropertySet propertySet = new NamedPropertySet("foo", mapProps);
+ NamedPropertySet propertySet2 = new NamedPropertySet("foo", mapProps);
+
+ assertEquals(propertySet.hashCode(), propertySet2.hashCode());
+ }
+}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PostRequestTest.java Mon Mar 11 19:26:49 2013
@@ -18,7 +18,9 @@
package org.apache.ambari.server.api.services;
+import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.predicate.PredicateCompiler;
+import org.apache.ambari.server.api.resources.ResourceInstance;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -31,28 +33,28 @@ import javax.ws.rs.core.UriInfo;
*/
public class PostRequestTest extends BaseRequestTest {
@Test
- public void testRequestType() {
- assertSame(Request.Type.POST, new PostRequest(null, null, null, null).getRequestType());
+ public void testRequestType() throws Exception {
+ Request r = new PostRequest(null, new RequestBody(), null, null);
+ assertSame(Request.Type.POST, r.getRequestType());
}
- @Test
- public void testGetQueryPredicate() throws Exception {
- String uri = "http://foo.bar.com/api/v1/clusters?foo=bar&orProp1=5|orProp2!=6|orProp3<100&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2";
- super.testGetQueryPredicate(uri);
- }
-
- @Test
- public void testGetFields() {
- String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
- super.testGetFields(fields);
- }
-
- protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
- return new PostRequest(headers, body, uriInfo, null) {
+ protected Request getTestRequest(HttpHeaders headers, RequestBody body, UriInfo uriInfo, final PredicateCompiler compiler,
+ final RequestHandler handler, final ResultPostProcessor processor, ResourceInstance resource) {
+ return new PostRequest(headers, body, uriInfo, resource) {
@Override
protected PredicateCompiler getPredicateCompiler() {
return compiler;
}
+
+ @Override
+ protected RequestHandler getRequestHandler() {
+ return handler;
+ }
+
+ @Override
+ public ResultPostProcessor getResultPostProcessor() {
+ return processor;
+ }
};
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/PutRequestTest.java Mon Mar 11 19:26:49 2013
@@ -18,7 +18,9 @@
package org.apache.ambari.server.api.services;
+import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.predicate.PredicateCompiler;
+import org.apache.ambari.server.api.resources.ResourceInstance;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -31,28 +33,28 @@ import javax.ws.rs.core.UriInfo;
*/
public class PutRequestTest extends BaseRequestTest {
@Test
- public void testRequestType() {
- assertSame(Request.Type.PUT, new PutRequest(null, null, null, null).getRequestType());
+ public void testRequestType() throws Exception {
+ Request r = new PutRequest(null, new RequestBody(), null, null);
+ assertSame(Request.Type.PUT, r.getRequestType());
}
- @Test
- public void testGetQueryPredicate() throws Exception {
- String uri = "http://foo.bar.com/api/v1/clusters?foo=bar&orProp1=5|orProp2!=6|orProp3<100&prop!=5&prop2>10&prop3>=20&prop4<500&prop5<=1&fields=field1,category/field2";
- super.testGetQueryPredicate(uri);
- }
-
- @Test
- public void testGetFields() {
- String fields = "prop,category/prop1,category2/category3/prop2[1,2,3],prop3[4,5,6],category4[7,8,9],sub-resource/*[10,11,12],finalProp";
- super.testGetFields(fields);
- }
-
- protected Request getTestRequest(HttpHeaders headers, String body, UriInfo uriInfo, final PredicateCompiler compiler) {
- return new PutRequest(headers, body, uriInfo, null) {
+ protected Request getTestRequest(HttpHeaders headers, RequestBody body, UriInfo uriInfo, final PredicateCompiler compiler,
+ final RequestHandler handler, final ResultPostProcessor processor, ResourceInstance resource) {
+ return new PutRequest(headers, body, uriInfo, resource) {
@Override
protected PredicateCompiler getPredicateCompiler() {
return compiler;
}
+
+ @Override
+ protected RequestHandler getRequestHandler() {
+ return handler;
+ }
+
+ @Override
+ public ResultPostProcessor getResultPostProcessor() {
+ return processor;
+ }
};
}
}
Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/QueryPostRequestTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/QueryPostRequestTest.java?rev=1455293&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/QueryPostRequestTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/QueryPostRequestTest.java Mon Mar 11 19:26:49 2013
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.api.services;
+
+import org.apache.ambari.server.api.handlers.RequestHandler;
+import org.apache.ambari.server.api.predicate.PredicateCompiler;
+import org.apache.ambari.server.api.resources.ResourceInstance;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
+import org.junit.Test;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * QueryPostRequest unit tests.
+ */
+public class QueryPostRequestTest extends BaseRequestTest {
+ @Test
+ public void testRequestType() throws Exception {
+ Request r = new QueryPostRequest(null, new RequestBody(), null, null);
+ assertSame(Request.Type.QUERY_POST, r.getRequestType());
+ }
+
+ protected Request getTestRequest(HttpHeaders headers, RequestBody body, UriInfo uriInfo, final PredicateCompiler compiler,
+ final RequestHandler handler, final ResultPostProcessor processor, ResourceInstance resource) {
+ return new QueryPostRequest(headers, body, uriInfo, resource) {
+ @Override
+ protected PredicateCompiler getPredicateCompiler() {
+ return compiler;
+ }
+
+ @Override
+ protected RequestHandler getRequestHandler() {
+ return handler;
+ }
+
+ @Override
+ public ResultPostProcessor getResultPostProcessor() {
+ return processor;
+ }
+ };
+ }
+}
Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestBodyTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestBodyTest.java?rev=1455293&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestBodyTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/RequestBodyTest.java Mon Mar 11 19:26:49 2013
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.api.services;
+
+import org.junit.Test;
+import java.util.HashMap;
+import static org.junit.Assert.*;
+
+/**
+ * RequestBody unit tests.
+ */
+public class RequestBodyTest {
+
+ @Test
+ public void testSetGetQueryString() {
+ RequestBody body = new RequestBody();
+ assertNull(body.getQueryString());
+ body.setQueryString("foo=bar");
+ assertEquals("foo=bar", body.getQueryString());
+ }
+
+ @Test
+ public void testSetGetPartialResponseFields() {
+ RequestBody body = new RequestBody();
+ assertNull(body.getPartialResponseFields());
+ body.setPartialResponseFields("foo,bar");
+ assertEquals("foo,bar", body.getPartialResponseFields());
+ }
+
+ @Test
+ public void testAddGetPropertySets() {
+ RequestBody body = new RequestBody();
+ assertEquals(0, body.getPropertySets().size());
+ NamedPropertySet ps = new NamedPropertySet("foo", new HashMap<String, Object>());
+ body.addPropertySet(ps);
+ assertEquals(1, body.getPropertySets().size());
+ assertSame(ps, body.getPropertySets().iterator().next());
+ }
+
+ @Test
+ public void testSetGetBody() {
+ RequestBody body = new RequestBody();
+ assertNull(body.getBody());
+ body.setBody("{\"foo\" : \"value\" }");
+ assertEquals("{\"foo\" : \"value\" }", body.getBody());
+ }
+}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/ServiceServiceTest.java Mon Mar 11 19:26:49 2013
@@ -18,11 +18,16 @@
package org.apache.ambari.server.api.services;
-import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.junit.Test;
+import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
-import javax.ws.rs.core.Response;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
import static org.junit.Assert.assertEquals;
@@ -31,220 +36,84 @@ import static org.junit.Assert.assertEqu
*/
public class ServiceServiceTest extends BaseServiceTest {
- @Test
- public void testGetService() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.getService(getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetService__ErrorState() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.getService(getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetServices() {
- String clusterName = "clusterName";
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.getServices(getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetServices__ErrorState(){
- String clusterName = "clusterName";
-
- registerExpectations(Request.Type.GET, null, 400, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.getServices(getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 400);
- }
-
- @Test
- public void testCreateService() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
- String body = "{body}";
-
- registerExpectations(Request.Type.POST, body, 201, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.createService(body, getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 201);
- }
-
- @Test
- public void testCreateService__ErrorState() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
- String body = "{body}";
-
- registerExpectations(Request.Type.POST, body, 500, true);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.createService(body, getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testUpdateServices() {
- String clusterName = "clusterName";
- String body = "{body}";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateServices(body, getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateServices__ErrorState() {
- String clusterName = "clusterName";
- String body = "{body}";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, null, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateServices(body, getHttpHeaders(), getUriInfo());
-
- verifyResults(response, 500);
- }
-
- @Test
- public void testUpdateService() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
- String body = "{body}";
-
- registerExpectations(Request.Type.PUT, body, 200, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateService(body, getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 200);
- }
-
- @Test
- public void testUpdateService__ErrorState() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
- String body = "{body}";
-
- registerExpectations(Request.Type.PUT, body, 500, true);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.updateService(body, getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 500);
- }
+ public List<ServiceTestInvocation> getTestInvocations() throws Exception {
+ List<ServiceTestInvocation> listInvocations = new ArrayList<ServiceTestInvocation>();
- @Test
- public void testDeleteService() {
- String clusterName = "clusterName";
- String serviceName = "serviceName";
-
- registerExpectations(Request.Type.DELETE, null, 200, false);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.deleteService(getHttpHeaders(), getUriInfo(), serviceName);
+ //getService
+ ServiceService service = new TestServiceService("clusterName", "serviceName");
+ Method m = service.getClass().getMethod("getService", HttpHeaders.class, UriInfo.class, String.class);
+ Object[] args = new Object[] {getHttpHeaders(), getUriInfo(), "serviceName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //getService
+ service = new TestServiceService("clusterName", null);
+ m = service.getClass().getMethod("getServices", HttpHeaders.class, UriInfo.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //createService
+ service = new TestServiceService("clusterName", "serviceName");
+ m = service.getClass().getMethod("createService", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "serviceName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body"));
+
+ //createServices
+ service = new TestServiceService("clusterName", null);
+ m = service.getClass().getMethod("createServices", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.POST, service, m, args, "body"));
+
+ //updateServices
+ service = new TestServiceService("clusterName", "serviceName");
+ m = service.getClass().getMethod("updateService", String.class, HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo(), "serviceName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, service, m, args, "body"));
+
+ //updateServices
+ service = new TestServiceService("clusterName", null);
+ m = service.getClass().getMethod("updateServices", String.class, HttpHeaders.class, UriInfo.class);
+ args = new Object[] {"body", getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.PUT, service, m, args, "body"));
+
+ //deleteServices
+ service = new TestServiceService("clusterName", "serviceName");
+ m = service.getClass().getMethod("deleteService", HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo(), "serviceName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.DELETE, service, m, args, null));
- verifyResults(response, 200);
- }
-
- @Test
- public void testDeleteService__ErrorState(){
- String clusterName = "clusterName";
- String serviceName = "serviceName";
-
- registerExpectations(Request.Type.DELETE, null, 500, true);
- replayMocks();
-
- //test
- ServiceService hostService = new TestServiceService(getResource(), clusterName, serviceName, getRequestFactory(), getRequestHandler());
- Response response = hostService.deleteService(getHttpHeaders(), getUriInfo(), serviceName);
-
- verifyResults(response, 500);
+ return listInvocations;
}
private class TestServiceService extends ServiceService {
- private RequestFactory m_requestFactory;
- private RequestHandler m_requestHandler;
- private ResourceInstance m_resourceDef;
private String m_clusterId;
private String m_serviceId;
- private TestServiceService(ResourceInstance resourceDef, String clusterId, String serviceId, RequestFactory requestFactory,
- RequestHandler handler) {
+ private TestServiceService(String clusterId, String serviceId) {
super(clusterId);
- m_resourceDef = resourceDef;
m_clusterId = clusterId;
m_serviceId = serviceId;
- m_requestFactory = requestFactory;
- m_requestHandler = handler;
}
@Override
ResourceInstance createServiceResource(String clusterName, String serviceName) {
assertEquals(m_clusterId, clusterName);
assertEquals(m_serviceId, serviceName);
- return m_resourceDef;
+ return getTestResource();
}
@Override
RequestFactory getRequestFactory() {
- return m_requestFactory;
+ return getTestRequestFactory();
+ }
+
+ @Override
+ protected RequestBodyParser getBodyParser() {
+ return getTestBodyParser();
}
@Override
- RequestHandler getRequestHandler(Request.Type requestType) {
- return m_requestHandler;
+ protected ResultSerializer getResultSerializer() {
+ return getTestResultSerializer();
}
}
}
Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java?rev=1455293&r1=1455292&r2=1455293&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/StacksServiceTest.java Mon Mar 11 19:26:49 2013
@@ -18,487 +18,122 @@
package org.apache.ambari.server.api.services;
-import org.apache.ambari.server.api.handlers.RequestHandler;
import org.apache.ambari.server.api.resources.ResourceInstance;
-import org.junit.Test;
+import org.apache.ambari.server.api.services.parsers.RequestBodyParser;
+import org.apache.ambari.server.api.services.serializers.ResultSerializer;
-import javax.ws.rs.core.Response;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.UriInfo;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
-/**
- * Unit tests for StacksService.
- */
-public class StacksServiceTest extends BaseServiceTest {
-
- private static final String STACK_NAME = "stackName";
- private static final String STACK_VERSION = "stackVersion";
- private static final String OS_TYPE = "osType";
- private static final String REPO_ID = "repoId";
- private static final String SERVICE_NAME = "serviceName";
- private static final String PROPERTY_NAME = "propertyName";
- private static final String COMPONENT_NAME = "componentName";
-
- @Test
- public void testGetStacks() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStacks(getHttpHeaders(), getUriInfo());
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStacks__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStacks(getHttpHeaders(), getUriInfo());
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStack() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStack(getHttpHeaders(), getUriInfo(),
- STACK_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStack__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStack(getHttpHeaders(), getUriInfo(),
- STACK_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStackVersion() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackVersion(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackVersion__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackVersion(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStackVersions() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackVersions(getHttpHeaders(),
- getUriInfo(), STACK_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackVersions__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackVersions(getHttpHeaders(),
- getUriInfo(), STACK_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetRepositories() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getRepositories(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_NAME, OS_TYPE);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetRepositories__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getRepositories(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, OS_TYPE);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetRepository() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getRepository(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_NAME, OS_TYPE, REPO_ID);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetRepository__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getRepository(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, OS_TYPE, REPO_ID);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStackServices() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackServices(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackServices__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackServices(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 500);
- }
- @Test
- public void testGetStackService() {
+import static org.junit.Assert.assertEquals;
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackService(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackService__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackService(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStackConfigurations() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackConfigurations(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackConfigurations__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackConfigurations(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetStackConfiguration() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackConfiguration(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, PROPERTY_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetStackConfiguration__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackConfiguration(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, PROPERTY_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetServiceComponent() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getServiceComponent(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, COMPONENT_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetServiceComponent__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getServiceComponent(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, COMPONENT_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetServiceComponents() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getStackConfiguration(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, PROPERTY_NAME);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetServiceComponents__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getStackConfiguration(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, SERVICE_NAME, PROPERTY_NAME);
- verifyResults(response, 500);
- }
-
- @Test
- public void testGetOperatingSystems() {
-
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getOperatingSystems(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 200);
- }
-
- @Test
- public void testGetOperatingSystems__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
-
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getOperatingSystems(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION);
- verifyResults(response, 500);
- }
+/**
+* Unit tests for StacksService.
+*/
+public class StacksServiceTest extends BaseServiceTest {
- @Test
- public void testGetOperatingSystem() {
+ @Override
+ public List<ServiceTestInvocation> getTestInvocations() throws Exception {
+ List<ServiceTestInvocation> listInvocations = new ArrayList<ServiceTestInvocation>();
+
+ //getStack
+ StacksService service = new TestStacksService("stackName", null);
+ Method m = service.getClass().getMethod("getStack", HttpHeaders.class, UriInfo.class, String.class);
+ Object[] args = new Object[] {getHttpHeaders(), getUriInfo(), "stackName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //getStacks
+ service = new TestStacksService(null, null);
+ m = service.getClass().getMethod("getStacks", HttpHeaders.class, UriInfo.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo()};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //getStackVersion
+ service = new TestStacksService("stackName", "stackVersion");
+ m = service.getClass().getMethod("getStackVersion", HttpHeaders.class, UriInfo.class, String.class, String.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo(), "stackName", "stackVersion"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
+
+ //getStackVersions
+ service = new TestStacksService("stackName", null);
+ m = service.getClass().getMethod("getStackVersions", HttpHeaders.class, UriInfo.class, String.class);
+ args = new Object[] {getHttpHeaders(), getUriInfo(), "stackName"};
+ listInvocations.add(new ServiceTestInvocation(Request.Type.GET, service, m, args, null));
- registerExpectations(Request.Type.GET, null, 200, false);
- replayMocks();
+ //todo: other methods
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
-
- Response response = stacksService.getOperatingSystem(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, OS_TYPE);
- verifyResults(response, 200);
+ return listInvocations;
}
- @Test
- public void testGetOperatingSystem__ErrorState() {
-
- registerExpectations(Request.Type.GET, null, 500, true);
- replayMocks();
+ private class TestStacksService extends StacksService {
- // test
- StacksService stacksService = new TestStacksService(getResource(),
- getRequestFactory(), getRequestHandler());
- Response response = stacksService.getOperatingSystem(getHttpHeaders(),
- getUriInfo(), STACK_NAME, STACK_VERSION, OS_TYPE);
- verifyResults(response, 500);
- }
+ private String m_stackId;
+ private String m_stackVersion;
- private class TestStacksService extends StacksService {
- private RequestFactory m_requestFactory;
- private RequestHandler m_requestHandler;
- private ResourceInstance m_resourceDef;
-
- private TestStacksService(ResourceInstance resourceDef,
- RequestFactory requestFactory, RequestHandler handler) {
- m_resourceDef = resourceDef;
- m_requestFactory = requestFactory;
- m_requestHandler = handler;
+ private TestStacksService(String stackName, String stackVersion) {
+ m_stackId = stackName;
+ m_stackVersion = stackVersion;
}
@Override
ResourceInstance createStackResource(String stackName) {
- return m_resourceDef;
+ assertEquals(m_stackId, stackName);
+ return getTestResource();
}
@Override
- ResourceInstance createStackVersionResource(String stackName,
- String stackVersion) {
- return m_resourceDef;
+ ResourceInstance createStackVersionResource(String stackName, String stackVersion) {
+ assertEquals(m_stackId, stackName);
+ assertEquals(m_stackVersion, stackVersion);
+ return getTestResource();
}
@Override
ResourceInstance createRepositoryResource(String stackName,
String stackVersion, String osType, String repoId) {
- return m_resourceDef;
+ return getTestResource();
}
@Override
ResourceInstance createStackServiceResource(String stackName,
String stackVersion, String serviceName) {
- return m_resourceDef;
+ return getTestResource();
}
ResourceInstance createStackConfigurationResource(String stackName,
String stackVersion, String serviceName, String propertyName) {
- return m_resourceDef;
+ return getTestResource();
}
ResourceInstance createStackServiceComponentResource(String stackName,
String stackVersion, String serviceName, String componentName) {
- return m_resourceDef;
+ return getTestResource();
}
ResourceInstance createOperatingSystemResource(String stackName,
String stackVersion, String osType) {
- return m_resourceDef;
+ return getTestResource();
}
+
@Override
RequestFactory getRequestFactory() {
- return m_requestFactory;
+ return getTestRequestFactory();
+ }
+
+ @Override
+ protected RequestBodyParser getBodyParser() {
+ return getTestBodyParser();
}
@Override
- RequestHandler getRequestHandler(Request.Type requestType) {
- return m_requestHandler;
+ protected ResultSerializer getResultSerializer() {
+ return getTestResultSerializer();
}
}
- // todo: test getHostHandler, getServiceHandler, getHostComponentHandler
}
Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/parsers/BodyParseExceptionTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/parsers/BodyParseExceptionTest.java?rev=1455293&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/parsers/BodyParseExceptionTest.java (added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/api/services/parsers/BodyParseExceptionTest.java Mon Mar 11 19:26:49 2013
@@ -0,0 +1,45 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ambari.server.api.services.parsers;
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * BodyParseException unit tests.
+ */
+public class BodyParseExceptionTest {
+
+ @Test
+ public void testCreateFromString() {
+ String msg = "some msg";
+ BodyParseException e = new BodyParseException(msg);
+
+ assertEquals(msg, e.getMessage());
+ }
+
+ @Test
+ public void testCreateFromException() {
+ Exception e = new Exception("test error msg");
+ BodyParseException bpe = new BodyParseException(e);
+
+ assertEquals("Invalid Request: Malformed Request Body. An exception occurred parsing the request body: " +
+ e.getMessage(), bpe.getMessage());
+ }
+}