Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
A-Little-Excited committed Apr 22, 2024
1 parent 5a1eb2d commit 2c8e9bf
Show file tree
Hide file tree
Showing 17 changed files with 15,374 additions and 56 deletions.
35 changes: 30 additions & 5 deletions athena-spring/athena-spring-boot-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.paiondata.athena</groupId>
<artifactId>athena-example-books</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -76,5 +71,35 @@
<artifactId>junit-platform-engine</artifactId>
<version>1.10.2</version>
</dependency>

<!-- Apache Commons -->
<dependency> <!-- Prefer DBCP over JDBI since lookup should provide low-level speed operations -->
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>

<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<scope>test</scope>
</dependency>

<!-- Spring WebFlux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>3.2.5</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
/*
* Copyright Paion Data
*
* * Copyright Paion Data
* *
* * 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.
* 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 com.paiondata.athena.spring.boot.autoconfigure.config;

import com.paiondata.athena.example.books.application.SQLMutationDataFetcher;
import com.paiondata.athena.example.books.application.SQLQueryDataFetcher;
import com.paiondata.athena.metadata.MetaData;
import com.paiondata.athena.metastore.MetaStore;
import com.paiondata.athena.metastore.graphql.GraphQLMetaStore;
import com.paiondata.athena.spring.boot.autoconfigure.datafetcher.SpringSQLMutationDataFetcher;
import com.paiondata.athena.spring.boot.autoconfigure.datafetcher.SpringSQLQueryDataFetcher;
import com.paiondata.athena.web.graphql.JacksonParser;
import com.paiondata.athena.web.graphql.JsonDocumentParser;

Expand All @@ -40,18 +37,18 @@
@Configuration
public class MetaConfig {

@Value("athena.spring.datasource.username")
private static String USERNAME;
@Value("athena.spring.datasource.password")
private static String PASSWORD;
@Value("athena.spring.datasource.url")
private static String URL;
@Value("athena.spring.datasource.driverclassname")
private static String DRIVER_CLASS_NAME;
@Value("${spring.datasource.username}")
private String USERNAME;
@Value("${spring.datasource.password}")
private String PASSWORD;
@Value("${spring.datasource.url}")
private String URL;
@Value("${spring.datasource.driver-class-name}")
private String DRIVER_CLASS_NAME;

@Bean
public JsonDocumentParser jsonDocumentParser() {
return new JacksonParser();
return JacksonParser.getInstance();
}

@Bean
Expand All @@ -64,12 +61,12 @@ public MetaStore graphQLMetaStore(

@Bean
public DataFetcher<MetaData> queryDataFetcher(@NotNull final DataSource dataSource) {
return new SQLQueryDataFetcher(dataSource);
return new SpringSQLQueryDataFetcher(dataSource);
}

@Bean
public DataFetcher<MetaData> mutationDataFetcher(@NotNull final DataSource dataSource) {
return new SQLMutationDataFetcher(dataSource);
return new SpringSQLMutationDataFetcher(dataSource);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -48,11 +47,9 @@
public class FileController {

@Autowired
@Qualifier("aliOssFileStore")
private FileStore fileStore;

@Autowired
@Qualifier("graphQLMetaStore")
private MetaStore metaStore;

private static final String FILE_ID = "fileId";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/*
* Copyright Paion Data
*
* * Copyright Paion Data
* *
* * 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.
* 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 com.paiondata.athena.spring.boot.autoconfigure.controller;

import static com.paiondata.athena.config.ErrorMessageFormat.INVALID_GRAPHQL_REQUEST;
Expand All @@ -26,7 +23,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -46,7 +42,6 @@ public class MetaController {
private static final Logger LOG = LoggerFactory.getLogger(MetaController.class);

@Autowired
@Qualifier("graphQLMetaStore")
private MetaStore metaStore;

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright Paion Data
*
* 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 com.paiondata.athena.spring.boot.autoconfigure.datafetcher;

import com.paiondata.athena.config.ErrorMessageFormat;
import com.paiondata.athena.config.SystemConfig;
import com.paiondata.athena.config.SystemConfigFactory;
import com.paiondata.athena.metadata.MetaData;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import jakarta.inject.Inject;
import jakarta.validation.constraints.NotNull;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.sql.DataSource;

/**
* {@link SpringSQLMutationDataFetcher} saves file meta data into a SQL data storage via a {@link DataSource}.
*/
public class SpringSQLMutationDataFetcher implements DataFetcher<MetaData> {

private static final Logger LOG = LoggerFactory.getLogger(SpringSQLMutationDataFetcher.class);
private static final SystemConfig SYSTEM_CONFIG = SystemConfigFactory.getInstance();
private static final String TABLE_NAME_KEY = "table_name";
private static final String TABLE_NAME = SYSTEM_CONFIG.getStringProperty(
SYSTEM_CONFIG.getPackageVariableName(TABLE_NAME_KEY)
).orElseThrow(() -> {
LOG.error(ErrorMessageFormat.CONFIG_NOT_FOUND.logFormat(TABLE_NAME_KEY));
return new IllegalStateException(ErrorMessageFormat.CONFIG_NOT_FOUND.format());
});

private final DataSource dataSource;

private static final String FILE_ID = "fileId";

private static final String META_DATA_PERSIST_QUERY_TEMPLATE =
"INSERT INTO " + TABLE_NAME + " (file_id, file_name, file_type) VALUES (?, ?, ?)";

/**
* Constructor.
*
* @param dataSource a client object against a SQL database to save meta data into
*
* @throws NullPointerException if {@code dataSource} is {@code null}
*/
@Inject
public SpringSQLMutationDataFetcher(final DataSource dataSource) {
this.dataSource = dataSource;
}

@Override
public MetaData get(final DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
final String fileId = dataFetchingEnvironment.getArgument(FILE_ID);
final String fileName = dataFetchingEnvironment.getArgument(MetaData.FILE_NAME);
final String fileType = dataFetchingEnvironment.getArgument(MetaData.FILE_TYPE);

try (
Connection connection = getDataSource().getConnection();
PreparedStatement statement = connection.prepareStatement(META_DATA_PERSIST_QUERY_TEMPLATE)
) {
statement.setString(1, fileId);
statement.setString(2, fileName);
statement.setString(3, fileType);
statement.executeUpdate();
}

return MetaData.of(
Stream.of(
new AbstractMap.SimpleImmutableEntry<>(MetaData.FILE_NAME, fileName),
new AbstractMap.SimpleImmutableEntry<>(MetaData.FILE_TYPE, fileType)
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))
);
}

@NotNull
private DataSource getDataSource() {
return dataSource;
}
}
Loading

0 comments on commit 2c8e9bf

Please sign in to comment.