Commit f58f29ecdd5f66f0ad175716b09baef46863aaae
0 parents
Exists in
master
add new project
Showing
14 changed files
with
538 additions
and
0 deletions
Show diff stats
1 | +++ a/.classpath | |
... | ... | @@ -0,0 +1,26 @@ |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<classpath> | |
3 | + <classpathentry kind="src" output="target/classes" path="src/main/java"> | |
4 | + <attributes> | |
5 | + <attribute name="optional" value="true"/> | |
6 | + <attribute name="maven.pomderived" value="true"/> | |
7 | + </attributes> | |
8 | + </classpathentry> | |
9 | + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> | |
10 | + <attributes> | |
11 | + <attribute name="optional" value="true"/> | |
12 | + <attribute name="maven.pomderived" value="true"/> | |
13 | + </attributes> | |
14 | + </classpathentry> | |
15 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> | |
16 | + <attributes> | |
17 | + <attribute name="maven.pomderived" value="true"/> | |
18 | + </attributes> | |
19 | + </classpathentry> | |
20 | + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> | |
21 | + <attributes> | |
22 | + <attribute name="maven.pomderived" value="true"/> | |
23 | + </attributes> | |
24 | + </classpathentry> | |
25 | + <classpathentry kind="output" path="target/classes"/> | |
26 | +</classpath> | ... | ... |
1 | +++ a/.project | |
... | ... | @@ -0,0 +1,23 @@ |
1 | +<?xml version="1.0" encoding="UTF-8"?> | |
2 | +<projectDescription> | |
3 | + <name>MockUp</name> | |
4 | + <comment></comment> | |
5 | + <projects> | |
6 | + </projects> | |
7 | + <buildSpec> | |
8 | + <buildCommand> | |
9 | + <name>org.eclipse.jdt.core.javabuilder</name> | |
10 | + <arguments> | |
11 | + </arguments> | |
12 | + </buildCommand> | |
13 | + <buildCommand> | |
14 | + <name>org.eclipse.m2e.core.maven2Builder</name> | |
15 | + <arguments> | |
16 | + </arguments> | |
17 | + </buildCommand> | |
18 | + </buildSpec> | |
19 | + <natures> | |
20 | + <nature>org.eclipse.jdt.core.javanature</nature> | |
21 | + <nature>org.eclipse.m2e.core.maven2Nature</nature> | |
22 | + </natures> | |
23 | +</projectDescription> | ... | ... |
1 | +++ a/.settings/org.eclipse.jdt.core.prefs | |
... | ... | @@ -0,0 +1,12 @@ |
1 | +eclipse.preferences.version=1 | |
2 | +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | |
3 | +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | |
4 | +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | |
5 | +org.eclipse.jdt.core.compiler.compliance=1.7 | |
6 | +org.eclipse.jdt.core.compiler.debug.lineNumber=generate | |
7 | +org.eclipse.jdt.core.compiler.debug.localVariable=generate | |
8 | +org.eclipse.jdt.core.compiler.debug.sourceFile=generate | |
9 | +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | |
10 | +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | |
11 | +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning | |
12 | +org.eclipse.jdt.core.compiler.source=1.7 | ... | ... |
1 | +++ a/pom.xml | |
... | ... | @@ -0,0 +1,67 @@ |
1 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
2 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
3 | + <modelVersion>4.0.0</modelVersion> | |
4 | + | |
5 | + <groupId>sourcecode</groupId> | |
6 | + <artifactId>MockUp</artifactId> | |
7 | + <version>0.0.1-SNAPSHOT</version> | |
8 | + <packaging>jar</packaging> | |
9 | + | |
10 | + <name>MockUp</name> | |
11 | + <url>http://maven.apache.org</url> | |
12 | + | |
13 | + <properties> | |
14 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
15 | + </properties> | |
16 | + | |
17 | + <dependencies> | |
18 | + <dependency> | |
19 | + <groupId>junit</groupId> | |
20 | + <artifactId>junit</artifactId> | |
21 | + <version>3.8.1</version> | |
22 | + <scope>test</scope> | |
23 | + </dependency> | |
24 | + | |
25 | + <!-- https://mvnrepository.com/artifact/com.sun.net.httpserver/http --> | |
26 | + <dependency> | |
27 | + <groupId>com.sun.net.httpserver</groupId> | |
28 | + <artifactId>http</artifactId> | |
29 | + <version>20070405</version> | |
30 | + <scope>test</scope> | |
31 | + </dependency> | |
32 | + <dependency> | |
33 | + <groupId>com.google.code.gson</groupId> | |
34 | + <artifactId>gson</artifactId> | |
35 | + <version>2.8.0</version> | |
36 | + </dependency> | |
37 | + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> | |
38 | + <dependency> | |
39 | + <groupId>commons-io</groupId> | |
40 | + <artifactId>commons-io</artifactId> | |
41 | + <version>2.6</version> | |
42 | + </dependency> | |
43 | + <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> | |
44 | + <dependency> | |
45 | + <groupId>org.apache.httpcomponents</groupId> | |
46 | + <artifactId>httpclient</artifactId> | |
47 | + <version>4.5.4</version> | |
48 | + </dependency> | |
49 | + <dependency> | |
50 | + <groupId>org.json</groupId> | |
51 | + <artifactId>json</artifactId> | |
52 | + <version>20180130</version> | |
53 | + </dependency> | |
54 | + <dependency> | |
55 | + <groupId>org.mongodb</groupId> | |
56 | + <artifactId>mongodb-driver</artifactId> | |
57 | + <version>3.6.3</version> | |
58 | + </dependency> | |
59 | + <dependency> | |
60 | + <groupId>org.mongodb</groupId> | |
61 | + <artifactId>mongo-java-driver</artifactId> | |
62 | + <version>3.6.3</version> | |
63 | + </dependency> | |
64 | + | |
65 | + | |
66 | + </dependencies> | |
67 | +</project> | ... | ... |
1 | +++ a/src/main/java/sourcecode/MockUp/MockUp.java | |
... | ... | @@ -0,0 +1,285 @@ |
1 | +package sourcecode.MockUp; | |
2 | + | |
3 | +import java.io.IOException; | |
4 | +import java.io.OutputStream; | |
5 | +import java.io.UnsupportedEncodingException; | |
6 | +import java.net.InetSocketAddress; | |
7 | +import java.util.ArrayList; | |
8 | +import java.util.Iterator; | |
9 | +import java.util.List; | |
10 | +import java.util.Map.Entry; | |
11 | + | |
12 | +import org.apache.commons.io.IOUtils; | |
13 | +import org.apache.http.Consts; | |
14 | +import org.bson.Document; | |
15 | +import org.json.JSONObject; | |
16 | + | |
17 | +import com.google.gson.Gson; | |
18 | +import com.mongodb.BasicDBObject; | |
19 | +import com.mongodb.MongoClient; | |
20 | +import com.mongodb.MongoClientOptions; | |
21 | +import com.mongodb.MongoClientURI; | |
22 | +import com.mongodb.client.FindIterable; | |
23 | +import com.mongodb.client.MongoCollection; | |
24 | +import com.mongodb.client.MongoCursor; | |
25 | +import com.mongodb.client.MongoDatabase; | |
26 | +import com.sun.net.httpserver.HttpExchange; | |
27 | +import com.sun.net.httpserver.HttpHandler; | |
28 | +import com.sun.net.httpserver.HttpServer; | |
29 | + | |
30 | + | |
31 | + | |
32 | + | |
33 | + | |
34 | +public class MockUp | |
35 | +{ | |
36 | + private static Gson gson = new Gson(); | |
37 | + private static MongoDatabase database; | |
38 | + private final static String GET = "GET"; | |
39 | + private final static String POST = "POST"; | |
40 | + private final static String PUT = "PUT"; | |
41 | + private final static String DELETE = "DELETE"; | |
42 | + | |
43 | + | |
44 | + | |
45 | + public static void main(String[] args) throws Exception { | |
46 | + | |
47 | + int port = 8000; | |
48 | + | |
49 | + | |
50 | + try{ | |
51 | + | |
52 | + | |
53 | + if(args.length>0) | |
54 | + port = Integer.parseInt(args[0]); | |
55 | + | |
56 | + //connect mongo | |
57 | + MongoConnector("spw"); | |
58 | + | |
59 | + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); | |
60 | + server.createContext("/", new MyHandler()); | |
61 | + server.setExecutor(null); // creates a default executor | |
62 | + server.start(); | |
63 | + System.out.println("Run in port : "+port); | |
64 | + | |
65 | + }catch(Exception e) | |
66 | + { | |
67 | + e.printStackTrace(); | |
68 | + System.out.println("Fail to run in port : "+port); | |
69 | + } | |
70 | + } | |
71 | + | |
72 | + static class MyHandler implements HttpHandler { | |
73 | + public void handle(HttpExchange t) throws IOException { | |
74 | + | |
75 | + String method = t.getRequestMethod(); | |
76 | + String url = t.getRequestURI().toString(); | |
77 | + String response = ""; | |
78 | + String keyBody =""; | |
79 | + | |
80 | +// System.out.println(t.getRequestMethod()); | |
81 | + if(!method.equals(GET)) | |
82 | + { | |
83 | + String bodyData = IOUtils.toString(t.getRequestBody(),Consts.UTF_8); | |
84 | + JSONObject body = new JSONObject(bodyData); | |
85 | + String[] a = bodyData.split(","); | |
86 | + // System.out.println(a[0]); | |
87 | + String[] b = a[0].split(":"); | |
88 | + // System.out.println(b[0]); | |
89 | + String c = b[0].replace("{", "").trim().replace("\"", ""); | |
90 | + // System.out.println(c); | |
91 | + keyBody = (String) body.get(c); | |
92 | + } | |
93 | +// System.out.println(body.get(c)); | |
94 | + | |
95 | +// for(Entry<String, List<String>> row:t.getRequestHeaders().entrySet()) | |
96 | +// { | |
97 | +// System.out.println(row.getKey()); | |
98 | +// System.out.println(row.getValue()); | |
99 | +// } | |
100 | +// System.out.println(t.getRequestHeaders()); | |
101 | +// System.out.println(t.getRequestURI()); | |
102 | + | |
103 | + | |
104 | + String collectionName = "spwCustomerAccounts"; | |
105 | + | |
106 | + | |
107 | + BasicDBObject basicDBObject = new BasicDBObject(); | |
108 | + | |
109 | + switch (method) { | |
110 | + case GET: | |
111 | + basicDBObject.put("url", url); | |
112 | + break; | |
113 | + case POST: | |
114 | + case PUT: | |
115 | + basicDBObject.put("key", keyBody); | |
116 | + break; | |
117 | + case DELETE: | |
118 | + basicDBObject.put("key", url); | |
119 | + break; | |
120 | + | |
121 | + default: | |
122 | + break; | |
123 | + } | |
124 | + | |
125 | + ArrayList<String> responseList = getDBData(basicDBObject,collectionName,method); | |
126 | + | |
127 | + if(responseList.size() == 0) | |
128 | + { | |
129 | + System.out.println("===> Go to Main flow"); | |
130 | + | |
131 | + BasicDBObject basicDBObjectMain = new BasicDBObject(); | |
132 | + if(method.equals(GET)) | |
133 | + basicDBObjectMain.put("url", ""); | |
134 | + else | |
135 | + basicDBObjectMain.put("key", ""); | |
136 | + responseList = getDBData(basicDBObjectMain,collectionName,method); | |
137 | + | |
138 | + | |
139 | + } | |
140 | + | |
141 | + JSONObject responsJSON = new JSONObject(); | |
142 | + ArrayList<JSONObject> resultData = new ArrayList<JSONObject>(); | |
143 | + for(int i=0;i<responseList.size();i++) | |
144 | + { | |
145 | + | |
146 | + JSONObject rowJSON = new JSONObject(responseList.get(i)); | |
147 | + | |
148 | + if(responseList.size() > 1) | |
149 | + { | |
150 | + if(rowJSON.get("resultData")!=null) | |
151 | + resultData.add((JSONObject) rowJSON.get("resultData")); | |
152 | + else if(rowJSON.get("value")!=null) | |
153 | + resultData.add((JSONObject) rowJSON.get("value")); | |
154 | + }else | |
155 | + resultData.add(rowJSON); | |
156 | +// System.out.println(rowJSON); | |
157 | +// resultData.add((JSONObject) rowJSON.get("resultData")); | |
158 | + } | |
159 | + | |
160 | + | |
161 | + responsJSON.put("resultCode", "20000"); | |
162 | + responsJSON.put("resultDescription", "Success"); | |
163 | + if(method.equals(GET)) | |
164 | + { | |
165 | + responsJSON.put("resultData",resultData); | |
166 | + responsJSON.put("rowCount", resultData.size()); | |
167 | + } | |
168 | + | |
169 | +// System.out.println(responsJSON.toString()); | |
170 | + response = responsJSON.toString(); | |
171 | + | |
172 | + | |
173 | + t.getResponseHeaders().set("Content-Type", "application/json"); | |
174 | + t.sendResponseHeaders(200, response.length()); | |
175 | + OutputStream os = t.getResponseBody(); | |
176 | + os.write(response.getBytes()); | |
177 | + os.close(); | |
178 | + | |
179 | + System.out.println(""); | |
180 | + System.out.println("URL : " + url); | |
181 | + System.out.println("Method : " + method); | |
182 | + System.out.println("Response : " + response); | |
183 | + System.out.println(""); | |
184 | + } | |
185 | + } | |
186 | + | |
187 | + | |
188 | + private static void MongoConnector(String db) { | |
189 | + | |
190 | + String username = ""; | |
191 | + String password = ""; | |
192 | + String address = "10.1.2.155:27017"; | |
193 | + String dbname = db; | |
194 | + String authSource = db; | |
195 | + int timeoutMongoDB = 10000; | |
196 | + | |
197 | + MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder(); | |
198 | + optionsBuilder.connectTimeout(timeoutMongoDB); | |
199 | + optionsBuilder.socketTimeout(timeoutMongoDB); | |
200 | + optionsBuilder.serverSelectionTimeout(timeoutMongoDB); | |
201 | + | |
202 | +// optionsBuilder.connectionsPerHost(maxPoolSize); | |
203 | +// optionsBuilder.minConnectionsPerHost(minPoolSize); | |
204 | + | |
205 | + MongoClientURI uri = new MongoClientURI("mongodb://"+username+":"+password+"@"+address+"/?authSource="+authSource, optionsBuilder); | |
206 | + if(username.equals("")) | |
207 | + uri = new MongoClientURI("mongodb://"+address+"/?authSource="+authSource, optionsBuilder); | |
208 | + System.out.println("MongoDB Connecting to "+uri.toString()+"..."); | |
209 | + MongoClient mongoClient = new MongoClient(uri); | |
210 | + database = mongoClient.getDatabase(dbname); | |
211 | + //test connect and list collections | |
212 | + MongoCursor<String> collectionNames = database.listCollectionNames().iterator(); | |
213 | + System.out.println("MongoDB Connect to "+uri.toString()+" Success"); | |
214 | + System.out.println("MongoDB Collections in datebase"); | |
215 | + while (collectionNames.hasNext()) { | |
216 | + String collectionName = collectionNames.next(); | |
217 | + System.out.println(" |_ "+collectionName); | |
218 | + | |
219 | + } | |
220 | + } | |
221 | + | |
222 | + private static ArrayList<String> getDBData(BasicDBObject basicDBObject,String collectionName,String method) | |
223 | + { | |
224 | + collectionName = method.toLowerCase()+"_"+collectionName; | |
225 | + System.out.println("CollectionName : " + collectionName); | |
226 | + System.out.println("Find : "+basicDBObject.toJson()); | |
227 | + ArrayList<String> returnData = new ArrayList<String>(); | |
228 | + String found = ""; | |
229 | + | |
230 | + MongoCollection<Document> collection = database.getCollection(collectionName); | |
231 | + FindIterable<Document> findData = collection.find(basicDBObject); | |
232 | + MongoCursor<Document> cursor = findData.iterator(); | |
233 | + | |
234 | + | |
235 | + switch (method) { | |
236 | + case GET: | |
237 | + while(cursor.hasNext()){ | |
238 | + Document rawRow = cursor.next(); | |
239 | + System.out.println("found _id : "+rawRow.get("_id")); | |
240 | + rawRow.remove("_id"); | |
241 | + rawRow.remove("url"); | |
242 | + if(rawRow.get("value") != null) | |
243 | + { | |
244 | + | |
245 | + rawRow.append("resultData", rawRow.get("value")); | |
246 | + rawRow.append("rowCount", "1"); | |
247 | + rawRow.remove("value"); | |
248 | + } | |
249 | + found = rawRow.toJson(); | |
250 | + returnData.add(found); | |
251 | + } | |
252 | + break; | |
253 | + case POST: | |
254 | + case PUT: | |
255 | + case DELETE: | |
256 | + while(cursor.hasNext()){ | |
257 | + Document rawRow = cursor.next(); | |
258 | + System.out.println("found _id : "+rawRow.get("_id")); | |
259 | + rawRow.remove("_id"); | |
260 | + rawRow.remove("key"); | |
261 | + if(rawRow.get("value") != null) | |
262 | + { | |
263 | + | |
264 | + JSONObject rowJSON = new JSONObject(rawRow.toJson()); | |
265 | +// System.out.println(rowJSON.get("value")); | |
266 | + found = rowJSON.get("value").toString(); | |
267 | + returnData.add(found); | |
268 | + System.out.println("found : "+found); | |
269 | + }else | |
270 | + { | |
271 | + found = rawRow.toJson(); | |
272 | + returnData.add(found); | |
273 | + } | |
274 | + | |
275 | + } | |
276 | + break; | |
277 | + | |
278 | + default: | |
279 | + break; | |
280 | + } | |
281 | + | |
282 | + | |
283 | + return returnData; | |
284 | + } | |
285 | +} | ... | ... |
1 | +++ a/src/test/java/sourcecode/MockUp/AppTest.java | |
... | ... | @@ -0,0 +1,38 @@ |
1 | +package sourcecode.MockUp; | |
2 | + | |
3 | +import junit.framework.Test; | |
4 | +import junit.framework.TestCase; | |
5 | +import junit.framework.TestSuite; | |
6 | + | |
7 | +/** | |
8 | + * Unit test for simple App. | |
9 | + */ | |
10 | +public class AppTest | |
11 | + extends TestCase | |
12 | +{ | |
13 | + /** | |
14 | + * Create the test case | |
15 | + * | |
16 | + * @param testName name of the test case | |
17 | + */ | |
18 | + public AppTest( String testName ) | |
19 | + { | |
20 | + super( testName ); | |
21 | + } | |
22 | + | |
23 | + /** | |
24 | + * @return the suite of tests being tested | |
25 | + */ | |
26 | + public static Test suite() | |
27 | + { | |
28 | + return new TestSuite( AppTest.class ); | |
29 | + } | |
30 | + | |
31 | + /** | |
32 | + * Rigourous Test :-) | |
33 | + */ | |
34 | + public void testApp() | |
35 | + { | |
36 | + assertTrue( true ); | |
37 | + } | |
38 | +} | ... | ... |
target/classes/META-INF/maven/sourcecode/MockUp/pom.properties
0 → 100644
1 | +++ a/target/classes/META-INF/maven/sourcecode/MockUp/pom.properties | |
... | ... | @@ -0,0 +1,7 @@ |
1 | +#Generated by Maven Integration for Eclipse | |
2 | +#Wed Aug 29 16:58:24 ICT 2018 | |
3 | +version=0.0.1-SNAPSHOT | |
4 | +groupId=sourcecode | |
5 | +m2e.projectName=MockUp | |
6 | +m2e.projectLocation=C\:\\myWork\\eclipseCode\\MockUp | |
7 | +artifactId=MockUp | ... | ... |
1 | +++ a/target/classes/META-INF/maven/sourcecode/MockUp/pom.xml | |
... | ... | @@ -0,0 +1,67 @@ |
1 | +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
2 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
3 | + <modelVersion>4.0.0</modelVersion> | |
4 | + | |
5 | + <groupId>sourcecode</groupId> | |
6 | + <artifactId>MockUp</artifactId> | |
7 | + <version>0.0.1-SNAPSHOT</version> | |
8 | + <packaging>jar</packaging> | |
9 | + | |
10 | + <name>MockUp</name> | |
11 | + <url>http://maven.apache.org</url> | |
12 | + | |
13 | + <properties> | |
14 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
15 | + </properties> | |
16 | + | |
17 | + <dependencies> | |
18 | + <dependency> | |
19 | + <groupId>junit</groupId> | |
20 | + <artifactId>junit</artifactId> | |
21 | + <version>3.8.1</version> | |
22 | + <scope>test</scope> | |
23 | + </dependency> | |
24 | + | |
25 | + <!-- https://mvnrepository.com/artifact/com.sun.net.httpserver/http --> | |
26 | + <dependency> | |
27 | + <groupId>com.sun.net.httpserver</groupId> | |
28 | + <artifactId>http</artifactId> | |
29 | + <version>20070405</version> | |
30 | + <scope>test</scope> | |
31 | + </dependency> | |
32 | + <dependency> | |
33 | + <groupId>com.google.code.gson</groupId> | |
34 | + <artifactId>gson</artifactId> | |
35 | + <version>2.8.0</version> | |
36 | + </dependency> | |
37 | + <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> | |
38 | + <dependency> | |
39 | + <groupId>commons-io</groupId> | |
40 | + <artifactId>commons-io</artifactId> | |
41 | + <version>2.6</version> | |
42 | + </dependency> | |
43 | + <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> | |
44 | + <dependency> | |
45 | + <groupId>org.apache.httpcomponents</groupId> | |
46 | + <artifactId>httpclient</artifactId> | |
47 | + <version>4.5.4</version> | |
48 | + </dependency> | |
49 | + <dependency> | |
50 | + <groupId>org.json</groupId> | |
51 | + <artifactId>json</artifactId> | |
52 | + <version>20180130</version> | |
53 | + </dependency> | |
54 | + <dependency> | |
55 | + <groupId>org.mongodb</groupId> | |
56 | + <artifactId>mongodb-driver</artifactId> | |
57 | + <version>3.6.3</version> | |
58 | + </dependency> | |
59 | + <dependency> | |
60 | + <groupId>org.mongodb</groupId> | |
61 | + <artifactId>mongo-java-driver</artifactId> | |
62 | + <version>3.6.3</version> | |
63 | + </dependency> | |
64 | + | |
65 | + | |
66 | + </dependencies> | |
67 | +</project> | ... | ... |
No preview for this file type
No preview for this file type
No preview for this file type