abstract channel network test
Fri Sep 01 2023 10:24:21 GMT+0000 (Coordinated Universal Time)
Saved by @thanuj
package com.modeln.channelnetwork.junit.graphql; import edu.emory.mathcs.backport.java.util.Collections; import java.io.IOException; import java.sql.CallableStatement; import java.sql.Connection; import java.util.ArrayList; import java.util.HashMap; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import com.modeln.cdm.junit.ParsedFileUnitTest; public class AbstractChannelNetworkTest extends ParsedFileUnitTest { protected static String RESET_OUTPUTS = "update export_output set process_status = 2 where " + "process_status != 2"; protected static String RESET_EXPORTS = "update export_status set result_code = 0 where " + "result_code = -1"; protected static final String CLEAR_EXPORT_OUTPUT_ORPHANS = "update export_output_orphans set retry_count = 12," + " process_time = sysdate where retry_count > 0"; protected static final String SLI_ID_QUERY = " select sli.sid from sales_line_item sli" + " join data_file df on df.sid = sli.data_file_sid and df" + ".customer_sid = sli.customer_sid" + " join customer c on c.sid = df.customer_sid where c.id = 'CTH'" + " and df.file_name = ?"; protected final static String UPDATE_STATUS_IN_PEH_SALES_QUEUE = "UPDATE peh_sales_queue SET queue_name = 'resign' " + "WHERE sales_line_item_sid IN (SELECT sli.SID FROM " + "sales_line_item sli join data_file df on sli.data_file_sid = " + "df.sid " + "where df.file_name = ?)"; public final static String UPDATE_STATUS_IN_PEH_INV_QUEUE = "UPDATE peh_inv_queue SET queue_name = 'resign' " + "WHERE inv_line_item_sid IN (SELECT ili.SID FROM inv_line_item " + "ili join data_file df on ili.data_file_sid = df.sid " + "where df.file_name = ?)"; protected static final String CATALOG_PRODUCT_AGGR_CNT = "select count(aggr.sid) FROM CATALOG_PRODUCT cp " + "LEFT JOIN CATALOG_PRODUCT_AGGR aggr ON " + "aggr.CATALOG_PRODUCT_SID=cp.SID AND " + "aggr.CUSTOMER_SID=cp.CUSTOMER_SID AND " + "aggr.REPORTING_PARTNER_SID IS NULL " + "WHERE cp.SID IN (select max(sid) from catalog_product) AND " + "cp.CUSTOMER_SID = 2 "; protected String generateUser1Token() { String userName = getEnvironment().getProperty("cthUser1CNUser"); String passWord = getEnvironment().getProperty("cthUser1CNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateCTHPartnerToken() { String userName = getEnvironment().getProperty("cthPartnerCNUser"); String passWord = getEnvironment().getProperty("cthPartnerCNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateCTHManufacturerToken() { String userName = getEnvironment().getProperty("cthManufacturerCNUser"); String passWord = getEnvironment().getProperty("cthManufacturerCNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateUser2Token() { String userName = getEnvironment().getProperty("cthUser2CNUser"); String passWord = getEnvironment().getProperty("cthUser2CNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateAdminToken() { String userName = getEnvironment().getProperty("cthAdminCNUser"); String passWord = getEnvironment().getProperty("cthAdminCNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateCTHBaseToken() { String userName = getEnvironment().getProperty("cthBaseCNUser"); String passWord = getEnvironment().getProperty("cthBaseCNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateCTHBaseUser2Token() { String userName = getEnvironment().getProperty("cthBaseCNUser2"); String passWord = getEnvironment().getProperty("cthBaseCNPassword2"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateCTHArrowToken() { String userName = getEnvironment().getProperty("cthArrowCNUser"); String passWord = getEnvironment().getProperty("cthArrowCNPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateInowDataOwnerBaseToken() { String userName = getEnvironment().getProperty("inowBaseCNUser"); String passWord = getEnvironment().getProperty("inowBaseCNPassword"); String url = getEnvironment().getProperty("inowKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("inowKeySecret"); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String generateTestToken() { String userName = getEnvironment().getProperty("testUser"); String passWord = getEnvironment().getProperty("testPassword"); String url = getEnvironment().getProperty("cthKeyCloakUrl"); String clientSecret = getEnvironment().getProperty("cthKeySecret"); System.out.println(userName+" "+passWord+" "+url+" "+clientSecret); return "Bearer " + getToken(clientSecret, userName, passWord, url); } protected String getToken(String clientSecret, String userName, String password, String url) { CloseableHttpClient client = HttpClientBuilder.create().build(); try { HttpPost post = new HttpPost(url); ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); parameters.add(new BasicNameValuePair("grant_type", "password")); parameters.add( new BasicNameValuePair("client_id", "ChannelNetwork")); parameters.add(new BasicNameValuePair("username", userName)); parameters.add(new BasicNameValuePair("password", password)); parameters.add( new BasicNameValuePair("client_secret", clientSecret)); post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8")); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity, "UTF-8"); JSONObject obj = new JSONObject(body); return obj.getString("access_token"); } catch (Exception e) { e.printStackTrace(); return null; } finally { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } protected void waitForProductAggrFinish() throws InterruptedException { //product muation needs a catalog product //aggr entry, so we wait for it to be populated boolean hasAggr = false; for (int i = 0; i < 30 && !hasAggr; i++) { hasAggr = getNamedParamJdbcTemplate().queryForObject( CATALOG_PRODUCT_AGGR_CNT, Collections.emptyMap(), Integer.class) > 0; System.out.println("Waiting for aggr"); Thread.sleep(10000); } } public void runProductAggrSproc() throws Exception { Connection connection = getJdbcTemplate().getDataSource() .getConnection(); CallableStatement callableStatement = connection .prepareCall( "{call channelnetworkpkg.populateproductaggrasync(?,?,?)}"); callableStatement.setString(1, "CTH"); callableStatement.setString(2, "{\"nmiProductCriteria\": \"(STRING_COL_1 is not null)\", \"nmiMaxAgeInDays\": 30 }"); callableStatement.setString(3, "OUT_VAR"); callableStatement.execute(); waitForProductAggrFinish(); } public void runSalesAggrSproc() throws Exception { Connection connection = getJdbcTemplate().getDataSource() .getConnection(); CallableStatement callableStatement = connection .prepareCall( "{call channelnetworkpkg.populatesliaggrasync(?,?,?)}"); callableStatement.setString(1, "CTH"); callableStatement.setString(2, "{\"nmiProductCriteria\": \"(STRING_COL_1 is not null)\", \"nmiMaxAgeInDays\": 30," + "\"needCorrectionSalesQueueName\": \"resign\" }"); callableStatement.setString(3, "OUT_VAR"); callableStatement.execute(); } public void runInvAggrSproc() throws Exception { Connection connection = getJdbcTemplate().getDataSource() .getConnection(); CallableStatement callableStatement = connection .prepareCall( "{call channelnetworkpkg.populateiliaggrasync(?,?,?)}"); callableStatement.setString(1, "CTH"); callableStatement.setString(2, "{\"nmiMaxAgeInDays\": 30," + "\"needCorrectionInvQueueName\": \"resign\" }"); callableStatement.setString(3, "OUT_VAR"); callableStatement.execute(); } }
Comments