Skip to content

Commit

Permalink
Added RegisterUser and CreateProject to core requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marin Vila committed Mar 7, 2014
1 parent f1c51e9 commit 047ad80
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface HttpRequestBuilder {
/**
* Registers a user and sends an email confirmation to the provided address
*/
public HttpRequest registerUser(final String email);
public HttpRequest registerUser(final String token, final String email);

/**
* Verifies the syntax for the provided DSL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,27 @@ public HttpRequest renameProject(final String token, final String oldName, final
}

@Override
public HttpRequest registerUser(final String email) {
// TODO Auto-generated method stub
throw new NotImplementedException();
public HttpRequest registerUser(final String token, final String email) {
final Map<String, String> event = new LinkedHashMap<String, String>();
if (email != null) event.put("Email", email);

final HttpRequest request = HttpRequest.POST("Domain.svc/submit/Client.Register", event);
request.headers.put("Authorization", Arrays.asList(token));
request.headers.put("Content-Type", Arrays.asList("application/json"));
request.headers.put("Accept", Arrays.asList("application/json"));
return request;
}

@Override
public HttpRequest createTestProject(final String token, final String projectName) {
// TODO Auto-generated method stub
throw new NotImplementedException();
final Map<String, String> event = new LinkedHashMap<String, String>();
if (projectName != null) event.put("ProjectName", projectName);

final HttpRequest request = HttpRequest.POST("Domain.svc/submit/Client.CreateProject", event);
request.headers.put("Authorization", Arrays.asList(token));
request.headers.put("Content-Type", Arrays.asList("application/json"));
request.headers.put("Accept", Arrays.asList("application/json"));
return request;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
import com.dslplatform.compiler.client.api.core.HttpRequest;
import com.dslplatform.compiler.client.api.core.HttpResponse;
import com.dslplatform.compiler.client.api.core.io.HttpTransport;
import com.dslplatform.compiler.client.api.core.mock.processor.MockProcessor;
import com.dslplatform.compiler.client.api.core.mock.processor.ParseProcessor;
import com.dslplatform.compiler.client.api.core.mock.processor.*;

public class HttpTransportMock implements HttpTransport {
private final List<MockProcessor> mockProcessors;

public HttpTransportMock() {
mockProcessors = new ArrayList<MockProcessor>();
mockProcessors.add(new ParseProcessor());
mockProcessors.add(new RenameProjectProcessor());
mockProcessors.add(new RegisterUserProcessor());
mockProcessors.add(new CreateProjectProcessor());


}

public HttpResponse sendRequest(final HttpRequest request) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.dslplatform.compiler.client.api.core.mock.processor;

import com.dslplatform.compiler.client.api.core.HttpRequest;
import com.dslplatform.compiler.client.api.core.HttpRequest.Method;
import com.dslplatform.compiler.client.api.core.HttpResponse;
import com.dslplatform.compiler.client.api.core.impl.JsonReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CreateProjectProcessor implements MockProcessor {
@Override
public boolean isDefinedAt(final HttpRequest request) {
return request.method == Method.POST && request.path.equals("Domain.svc/submit/Client.CreteProject");
}

private static final Charset ENCODING = Charset.forName("UTF-8");

@Override
public HttpResponse apply(final HttpRequest request) throws IOException {
final Map<String, String> map;
{
final JsonReader jr =
new JsonReader(new InputStreamReader(new ByteArrayInputStream(request.body), ENCODING));
map = jr.readMap();
}

final String projectNick = map.get("ProjectNick");

final int code;
final byte[] body;

final boolean nameingFailure =
!projectNick.contains("!");
final boolean abstence =
projectNick.equals("");

final Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();

if (abstence) {
code = 400;
body = "Project name not provided.".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else if (nameingFailure) {
code = 403;
body = "Parse error - encountered an exclamation mark!".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else {
code = 200;
body = new byte[0];
}

headers.put("Content-Length", Arrays.asList(String.valueOf(body.length)));
return new HttpResponse(code, headers, body);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.dslplatform.compiler.client.api.core.mock.processor;

import com.dslplatform.compiler.client.api.core.HttpRequest;
import com.dslplatform.compiler.client.api.core.HttpRequest.Method;
import com.dslplatform.compiler.client.api.core.HttpResponse;
import com.dslplatform.compiler.client.api.core.impl.JsonReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RegisterUserProcessor implements MockProcessor {
@Override
public boolean isDefinedAt(final HttpRequest request) {
return request.method == Method.POST && request.path.equals("Domain.svc/submit/Client.RegisterUser");
}

private static final Charset ENCODING = Charset.forName("UTF-8");

@Override
public HttpResponse apply(final HttpRequest request) throws IOException {
final Map<String, String> map;
{
final JsonReader jr =
new JsonReader(new InputStreamReader(new ByteArrayInputStream(request.body), ENCODING));
map = jr.readMap();
}

final String email = map.get("Email");

final int code;
final byte[] body;

final boolean notPermitted =
!email.equals("super@user.org");
final boolean abstence =
email.equals("");

final Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();

if (abstence) {
code = 400;
body = "Project name not provided.".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else if (notPermitted) {
code = 403;
body = "You don't have authorization to perform requested action: Missing permission for user registration".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else {
code = 200;
body = new byte[0];
}

headers.put("Content-Length", Arrays.asList(String.valueOf(body.length)));
return new HttpResponse(code, headers, body);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ public HttpResponse apply(final HttpRequest request) throws IOException {
final int code;
final byte[] body;

final boolean success = !oldName.contains("!") && !newName.contains("!");
final boolean nameingFailure =
oldName.contains("!") ||
newName.contains("!");
final boolean nameingAbstence =
oldName.equals("") ||
newName.equals("");

final Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();

if (success) {
code = 200;
body = new byte[0];
} else {
if (nameingAbstence) {
code = 400;
body = "Project name not provided.".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else if (nameingFailure) {
code = 400;
body = "Parse error - encountered an exclamation mark!".getBytes(ENCODING);
headers.put("Content-Type", Arrays.asList("text/plain; charset=\"utf-8\""));
} else {
code = 201;
body = new byte[0];
}

headers.put("Content-Length", Arrays.asList(String.valueOf(body.length)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,45 @@ public void testRenameProjectBuilder() throws IOException {
assertArrayEquals(
"{\"OldName\":\"GreenLeopard\",\"NewName\":\"GreenLion\"}".getBytes(ENCODING),
renameRequest.body);
}}
}
/*
@Test
@SuppressWarnings("serial")
public void testRegisterUserBuilder() throws IOException {
final HttpRequest registerUserRequest; {
final String email = "user@test.org";
registerUserRequest = httpRequestBuilder.registerUser(email);
}
assertEquals(registerUserRequest.method, HttpRequest.Method.POST);
assertEquals(registerUserRequest.path, "Domain.svc/submit/Client.RegisterUser");
assertEquals(registerUserRequest.headers, new HashMap<String, List<String>>() {{
put("Content-Type", Arrays.asList("application/json"));
put("Accept", Arrays.asList("application/json"));
}});
assertArrayEquals(
"{\"Email\":\"user@test.org\"}".getBytes(ENCODING),
registerUserRequest.body);
}
*/
@Test
@SuppressWarnings("serial")
public void testCreateProjectBuilder() throws IOException {
final HttpRequest createProjectRequest; {
final String token = "Basic " + DatatypeConverter.printBase64Binary("ocd@dsl-platform.com:xxx".getBytes(ENCODING));
final String projectName = "NewProjectName";
createProjectRequest = httpRequestBuilder.createTestProject(token, projectName);
}

assertEquals(createProjectRequest.method, HttpRequest.Method.POST);
assertEquals(createProjectRequest.path, "Domain.svc/submit/Client.CreateProject");
assertEquals(createProjectRequest.headers, new HashMap<String, List<String>>() {{
put("Content-Type", Arrays.asList("application/json"));
put("Accept", Arrays.asList("application/json"));
put("Authorization", Arrays.asList("Basic " + DatatypeConverter.printBase64Binary("ocd@dsl-platform.com:xxx".getBytes(ENCODING))));
}});
assertArrayEquals(
"{\"ProjectName\":\"NewProjectName\"}".getBytes(ENCODING),
createProjectRequest.body);
}
}
Loading

0 comments on commit 047ad80

Please sign in to comment.