001/**
002 * Copyright 2014 The Sculptor Project Team, including the original
003 * author or authors.
004 * 
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.sculptor.generator.cartridge.mongodb;
018
019import java.util.Set;
020import javax.inject.Inject;
021import org.eclipse.emf.common.util.EList;
022import org.eclipse.xtend2.lib.StringConcatenation;
023import org.eclipse.xtext.xbase.lib.Extension;
024import org.eclipse.xtext.xbase.lib.Functions.Function1;
025import org.eclipse.xtext.xbase.lib.IterableExtensions;
026import org.sculptor.generator.chain.ChainLink;
027import org.sculptor.generator.chain.ChainOverridable;
028import org.sculptor.generator.ext.Helper;
029import org.sculptor.generator.ext.Properties;
030import org.sculptor.generator.template.service.ServiceTestTmpl;
031import org.sculptor.generator.util.HelperBase;
032import org.sculptor.generator.util.OutputSlot;
033import sculptormetamodel.Service;
034import sculptormetamodel.ServiceOperation;
035
036@ChainOverridable
037@SuppressWarnings("all")
038public class MongoDbServiceTestTmpl extends ChainLink<MongoDbServiceTestTmpl> {
039  @Inject
040  @Extension
041  private HelperBase helperBase;
042  
043  @Inject
044  @Extension
045  private Helper helper;
046  
047  @Inject
048  @Extension
049  private Properties properties;
050  
051  @Inject
052  private ServiceTestTmpl serviceTestTmpl;
053  
054  public String serviceJUnitSubclassMongoDb(final Service it) {
055    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.SERVICEJUNITSUBCLASSMONGODB_SERVICE];
056    return headObj._chained_serviceJUnitSubclassMongoDb(it);
057  }
058  
059  public String dependencyInjection(final Service it) {
060    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.DEPENDENCYINJECTION_SERVICE];
061    return headObj._chained_dependencyInjection(it);
062  }
063  
064  public String initTestData(final Service it) {
065    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.INITTESTDATA_SERVICE];
066    return headObj._chained_initTestData(it);
067  }
068  
069  public String initDbManagerThreadInstance(final Service it) {
070    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.INITDBMANAGERTHREADINSTANCE_SERVICE];
071    return headObj._chained_initDbManagerThreadInstance(it);
072  }
073  
074  public String dropDatabase(final Service it) {
075    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.DROPDATABASE_SERVICE];
076    return headObj._chained_dropDatabase(it);
077  }
078  
079  public String countRows(final Service it) {
080    MongoDbServiceTestTmpl headObj = getMethodsDispatchHead()[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.COUNTROWS_SERVICE];
081    return headObj._chained_countRows(it);
082  }
083  
084  public MongoDbServiceTestTmpl(final MongoDbServiceTestTmpl next) {
085    super(next);
086  }
087  
088  public String _chained_serviceJUnitSubclassMongoDb(final Service it) {
089    String _serviceapiPackage = this.helper.getServiceapiPackage(it);
090    String _plus = (_serviceapiPackage + ".");
091    String _name = it.getName();
092    String _plus_1 = (_plus + _name);
093    String _plus_2 = (_plus_1 + "Test");
094    String _javaFileName = this.helper.javaFileName(_plus_2);
095    StringConcatenation _builder = new StringConcatenation();
096    String _javaHeader = this.properties.javaHeader();
097    _builder.append(_javaHeader, "");
098    _builder.newLineIfNotEmpty();
099    _builder.append("package ");
100    String _serviceapiPackage_1 = this.helper.getServiceapiPackage(it);
101    _builder.append(_serviceapiPackage_1, "");
102    _builder.append(";");
103    _builder.newLineIfNotEmpty();
104    _builder.newLine();
105    _builder.append("/// Sculptor code formatter imports ///");
106    _builder.newLine();
107    _builder.newLine();
108    _builder.append("import static org.junit.Assert.*;");
109    _builder.newLine();
110    _builder.newLine();
111    _builder.append("/**");
112    _builder.newLine();
113    _builder.append(" ");
114    _builder.append("* Spring based test with MongoDB.");
115    _builder.newLine();
116    _builder.append(" ");
117    _builder.append("*/");
118    _builder.newLine();
119    _builder.append("@org.junit.runner.RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)");
120    _builder.newLine();
121    _builder.append("@org.springframework.test.context.ContextConfiguration(locations = { \"classpath:applicationContext-test.xml\" })");
122    _builder.newLine();
123    _builder.append("public class ");
124    String _name_1 = it.getName();
125    _builder.append(_name_1, "");
126    _builder.append("Test extends org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests implements ");
127    String _name_2 = it.getName();
128    _builder.append(_name_2, "");
129    _builder.append("TestBase {");
130    _builder.newLineIfNotEmpty();
131    _builder.newLine();
132    _builder.newLine();
133    String _dependencyInjection = this.dependencyInjection(it);
134    _builder.append(_dependencyInjection, "");
135    _builder.newLineIfNotEmpty();
136    String _initTestData = this.initTestData(it);
137    _builder.append(_initTestData, "");
138    _builder.newLineIfNotEmpty();
139    String _initDbManagerThreadInstance = this.initDbManagerThreadInstance(it);
140    _builder.append(_initDbManagerThreadInstance, "");
141    _builder.newLineIfNotEmpty();
142    String _dropDatabase = this.dropDatabase(it);
143    _builder.append(_dropDatabase, "");
144    _builder.newLineIfNotEmpty();
145    String _countRows = this.countRows(it);
146    _builder.append(_countRows, "");
147    _builder.newLineIfNotEmpty();
148    _builder.newLine();
149    _builder.append("\t");
150    EList<ServiceOperation> _operations = it.getOperations();
151    final Function1<ServiceOperation, Boolean> _function = new Function1<ServiceOperation, Boolean>() {
152      @Override
153      public Boolean apply(final ServiceOperation op) {
154        return Boolean.valueOf(MongoDbServiceTestTmpl.this.helper.isPublicVisibility(op));
155      }
156    };
157    Iterable<ServiceOperation> _filter = IterableExtensions.<ServiceOperation>filter(_operations, _function);
158    final Function1<ServiceOperation, String> _function_1 = new Function1<ServiceOperation, String>() {
159      @Override
160      public String apply(final ServiceOperation op) {
161        return op.getName();
162      }
163    };
164    Iterable<String> _map = IterableExtensions.<ServiceOperation, String>map(_filter, _function_1);
165    Set<String> _set = IterableExtensions.<String>toSet(_map);
166    final Function1<String, String> _function_2 = new Function1<String, String>() {
167      @Override
168      public String apply(final String it) {
169        return MongoDbServiceTestTmpl.this.serviceTestTmpl.testMethod(it);
170      }
171    };
172    Iterable<String> _map_1 = IterableExtensions.<String, String>map(_set, _function_2);
173    String _join = IterableExtensions.join(_map_1);
174    _builder.append(_join, "\t");
175    _builder.newLineIfNotEmpty();
176    _builder.append("}");
177    _builder.newLine();
178    return this.helper.fileOutput(_javaFileName, OutputSlot.TO_SRC_TEST, _builder.toString());
179  }
180  
181  public String _chained_dependencyInjection(final Service it) {
182    StringConcatenation _builder = new StringConcatenation();
183    _builder.append("@org.springframework.beans.factory.annotation.Autowired");
184    _builder.newLine();
185    _builder.append("private ");
186    String _fw = this.properties.fw("accessimpl.mongodb.DbManager");
187    _builder.append(_fw, "");
188    _builder.append(" dbManager;");
189    _builder.newLineIfNotEmpty();
190    _builder.newLine();
191    _builder.append("@org.springframework.beans.factory.annotation.Autowired");
192    _builder.newLine();
193    _builder.append("\t");
194    _builder.append("private ");
195    String _serviceapiPackage = this.helper.getServiceapiPackage(it);
196    _builder.append(_serviceapiPackage, "\t");
197    _builder.append(".");
198    String _name = it.getName();
199    _builder.append(_name, "\t");
200    _builder.append(" ");
201    String _name_1 = it.getName();
202    String _firstLower = this.helperBase.toFirstLower(_name_1);
203    _builder.append(_firstLower, "\t");
204    _builder.append(";");
205    _builder.newLineIfNotEmpty();
206    return _builder.toString();
207  }
208  
209  public String _chained_initTestData(final Service it) {
210    StringConcatenation _builder = new StringConcatenation();
211    _builder.append("@org.junit.Before");
212    _builder.newLine();
213    _builder.append("public void initTestData() {");
214    _builder.newLine();
215    _builder.append("}");
216    _builder.newLine();
217    return _builder.toString();
218  }
219  
220  public String _chained_initDbManagerThreadInstance(final Service it) {
221    StringConcatenation _builder = new StringConcatenation();
222    _builder.append("@org.junit.Before");
223    _builder.newLine();
224    _builder.append("public void initDbManagerThreadInstance() throws Exception {");
225    _builder.newLine();
226    _builder.append("\t");
227    _builder.append("// to be able to do lazy loading of associations inside test class");
228    _builder.newLine();
229    _builder.append("\t");
230    String _fw = this.properties.fw("accessimpl.mongodb.DbManager");
231    _builder.append(_fw, "\t");
232    _builder.append(".setThreadInstance(dbManager);");
233    _builder.newLineIfNotEmpty();
234    _builder.append("}");
235    _builder.newLine();
236    return _builder.toString();
237  }
238  
239  public String _chained_dropDatabase(final Service it) {
240    StringConcatenation _builder = new StringConcatenation();
241    _builder.append("@org.junit.After");
242    _builder.newLine();
243    _builder.append("public void dropDatabase() {");
244    _builder.newLine();
245    _builder.append("\t");
246    _builder.append("java.util.Set<String> names = dbManager.getDB().getCollectionNames();");
247    _builder.newLine();
248    _builder.append("\t");
249    _builder.append("for (String each : names) {");
250    _builder.newLine();
251    _builder.append("\t\t");
252    _builder.append("if (!each.startsWith(\"system\")) {");
253    _builder.newLine();
254    _builder.append("\t\t    ");
255    _builder.append("dbManager.getDB().getCollection(each).drop();");
256    _builder.newLine();
257    _builder.append("\t\t");
258    _builder.append("}");
259    _builder.newLine();
260    _builder.append("\t");
261    _builder.append("}");
262    _builder.newLine();
263    _builder.append("\t");
264    _builder.append("// dbManager.getDB().dropDatabase();");
265    _builder.newLine();
266    _builder.append("}");
267    _builder.newLine();
268    return _builder.toString();
269  }
270  
271  public String _chained_countRows(final Service it) {
272    StringConcatenation _builder = new StringConcatenation();
273    _builder.append("private int countRowsInDBCollection(String name) {");
274    _builder.newLine();
275    _builder.append("\t");
276    _builder.append("return (int) dbManager.getDBCollection(name).getCount();");
277    _builder.newLine();
278    _builder.append("}");
279    _builder.newLine();
280    return _builder.toString();
281  }
282  
283  public MongoDbServiceTestTmpl[] _getOverridesDispatchArray() {
284    org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmpl[] result = new org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmpl[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.NUM_METHODS];
285    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.SERVICEJUNITSUBCLASSMONGODB_SERVICE] = this; 
286    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.DEPENDENCYINJECTION_SERVICE] = this; 
287    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.INITTESTDATA_SERVICE] = this; 
288    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.INITDBMANAGERTHREADINSTANCE_SERVICE] = this; 
289    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.DROPDATABASE_SERVICE] = this; 
290    result[org.sculptor.generator.cartridge.mongodb.MongoDbServiceTestTmplMethodIndexes.COUNTROWS_SERVICE] = this; 
291    return result;
292  }
293}