diff options
Diffstat (limited to 'validate/sample-mock-generator')
6 files changed, 310 insertions, 0 deletions
diff --git a/validate/sample-mock-generator/pom.xml b/validate/sample-mock-generator/pom.xml new file mode 100644 index 00000000..c9263aff --- /dev/null +++ b/validate/sample-mock-generator/pom.xml @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2017 Huawei Technologies Co., Ltd. + + Licensed 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.onap.cli</groupId> + <artifactId>cli</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + + <artifactId>cli-sample-mock-generator</artifactId> + <name>cli/validate/sample-mock-generator</name> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.11</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>com.fasterxml.jackson.core</groupId> + <artifactId>jackson-databind</artifactId> + <version>2.6.3</version> + </dependency> + </dependencies> + +</project> diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java new file mode 100644 index 00000000..c3006e32 --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockJsonGenerator.java @@ -0,0 +1,42 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.http.mock; + +import java.io.File; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; + +import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; + +public class MockJsonGenerator { + public static void generateMocking(MockRequest mockRequest, MockResponse mockResponse, + String jsonFilePath) throws IOException { + + MockObject mockObject = new MockObject(); + mockObject.setRequest(mockRequest); + mockObject.setResponse(mockResponse); + + ObjectMapper mapper = new ObjectMapper(); + ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); + String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()); + writer.writeValue(new File(jsonFilePath + "-" + timeStamp + "-moco.json"), + Arrays.asList(mockObject)); + } +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java new file mode 100644 index 00000000..2d02e85c --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockObject.java @@ -0,0 +1,40 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.http.mock; + +public class MockObject { + private MockRequest request; + private MockResponse response; + + public MockRequest getRequest() { + return request; + } + + public void setRequest(MockRequest request) { + this.request = request; + } + + public MockResponse getResponse() { + return response; + } + + public void setResponse(MockResponse response) { + this.response = response; + } + + +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java new file mode 100644 index 00000000..cbb2a26a --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockRequest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.http.mock; + +import java.io.IOException; +import java.net.URL; +import java.util.Map; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class MockRequest { + private String method; + private String uri; + private Map<String, String> headers; + private JsonNode json; + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } + + public String getUri() { + return uri; + } + + public void setUri(String url) throws IOException { + URL urlt; + urlt = new URL(url); + this.uri = urlt.getPath(); + } + + public Map<String, String> getHeaders() { + return headers; + } + + public void setHeaders(Map<String, String> headers) { + this.headers = headers; + } + + public JsonNode getJson() { + return json; + } + + public void setJson(String json) throws IOException { + if (!json.isEmpty()) { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } + + } +} diff --git a/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java new file mode 100644 index 00000000..2b8fa826 --- /dev/null +++ b/validate/sample-mock-generator/src/main/java/org/onap/cli/http/mock/MockResponse.java @@ -0,0 +1,46 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.http.mock; + +import java.io.IOException; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class MockResponse { + private int status; + private JsonNode json; + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public JsonNode getJson() { + return json; + } + + public void setJson(String json) throws IOException { + if (!json.isEmpty()) { + ObjectMapper objectMapper = new ObjectMapper(); + this.json = objectMapper.readTree(json); + } + } +} diff --git a/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java new file mode 100644 index 00000000..3b79057f --- /dev/null +++ b/validate/sample-mock-generator/src/test/java/org/onap/cli/http/mock/MockJsonGeneratorTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2017 Huawei Technologies Co., Ltd. + * + * Licensed 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.onap.cli.http.mock; + +import java.io.IOException; + +import org.junit.Test; + +public class MockJsonGeneratorTest { + + @Test + public void mocoGenerateTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\"}"); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80/getResource"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\"}"); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } + + @Test(expected=IOException.class) + public void mocoGenerateFailedInvalidBodyTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\""); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80/getResource"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\""); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } + + @Test(expected=IOException.class) + public void mocoGenerateFailedInvalidUrlTest() throws IOException { + MockRequest mockRequest = new MockRequest(); + mockRequest.setJson("{\"value\" : \"234sdf-345\""); + mockRequest.setMethod("get"); + mockRequest.setUri("http://1.1.1.1:80:invalid"); + + MockResponse mockResponse = new MockResponse(); + mockResponse.setStatus(200); + mockResponse.setJson("{\"value\" : \"234sdf-345\""); + + MockJsonGenerator.generateMocking(mockRequest, mockResponse, "target/test"); + } +} |