forked from google/sqlcommenter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes google#243
- Loading branch information
Showing
12 changed files
with
594 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
java/sqlcommenter-java/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-bin.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
43 changes: 43 additions & 0 deletions
43
...src/main/java/com/google/cloud/sqlcommenter/interceptors/GrpcSQLCommenterInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// 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.google.cloud.sqlcommenter.interceptors; | ||
|
||
import com.google.cloud.sqlcommenter.threadlocalstorage.State; | ||
import io.grpc.Metadata; | ||
import io.grpc.ServerCall; | ||
import io.grpc.ServerCallHandler; | ||
import io.grpc.ServerInterceptor; | ||
|
||
public class GrpcSQLCommenterInterceptor implements ServerInterceptor { | ||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( | ||
ServerCall<ReqT, RespT> call, Metadata requestHeaders, ServerCallHandler<ReqT, RespT> next) { | ||
|
||
if (call == null || next == null) return null; | ||
|
||
if (call != null) { | ||
String actionName = call.getMethodDescriptor().getBareMethodName(); | ||
String serviceName = call.getMethodDescriptor().getServiceName(); | ||
State.Holder.set( | ||
State.newBuilder() | ||
.withControllerName(serviceName) | ||
.withActionName(actionName) | ||
.withFramework("spring-grpc") | ||
.build()); | ||
} | ||
|
||
return next.startCall(call, requestHeaders); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...-java/src/test/java/com/google/cloud/sqlcommenter/grpc/backend/GrpcSQLCommenterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.google.cloud.sqlcommenter.grpc.backend; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import com.google.cloud.sqlcommenter.grpc.backend.service.StudentServiceImpl; | ||
import com.google.cloud.sqlcommenter.grpc.stubs.StudentRequest; | ||
import com.google.cloud.sqlcommenter.grpc.stubs.StudentResponse; | ||
import com.google.cloud.sqlcommenter.threadlocalstorage.State; | ||
import com.google.cloud.sqlcommenter.util.SCHibernateWrapper; | ||
import io.grpc.stub.StreamObserver; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
public class GrpcSQLCommenterTests { | ||
|
||
@Test | ||
public void givenStudentService_whenGetStudentInfoIsCalled_thenSQLStatementsShouldBeTagged() { | ||
// given | ||
StudentServiceImpl studentService = new StudentServiceImpl(); | ||
StreamObserver<StudentResponse> observer = mock(StreamObserver.class); | ||
State.Holder.set( | ||
State.newBuilder() | ||
.withControllerName("StudentServiceImpl") | ||
.withActionName("getStudentInfo") | ||
.withFramework("grpc") | ||
.build()); | ||
|
||
SCHibernateWrapper.reset(); | ||
StudentRequest studentRequest = StudentRequest.newBuilder().setStudentId("st1").build(); | ||
|
||
// when | ||
studentService.getStudentInfo(studentRequest, observer); | ||
List<String> sqlStatements = SCHibernateWrapper.getAfterSqlStatements(); | ||
|
||
// then | ||
assertEquals(1, sqlStatements.size()); | ||
assertEquals( | ||
1, | ||
sqlStatements | ||
.stream() | ||
.filter( | ||
sql -> | ||
sql.contains( | ||
"/*action='getStudentInfo',controller='StudentServiceImpl',framework='grpc'*/")) | ||
.count()); | ||
|
||
SCHibernateWrapper.reset(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...mmenter-java/src/test/java/com/google/cloud/sqlcommenter/grpc/backend/dao/StudentDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// 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.google.cloud.sqlcommenter.grpc.backend.dao; | ||
|
||
import com.google.cloud.sqlcommenter.grpc.backend.domain.Student; | ||
import java.util.NoSuchElementException; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.EntityManagerFactory; | ||
import javax.persistence.Persistence; | ||
|
||
public class StudentDao { | ||
public Student findById(String studentId) { | ||
|
||
// We use entity managers to manage our two entities. | ||
// We use the factory design pattern to get the entity manager. | ||
// Here we should provide the name of the persistence unit that we provided in the | ||
// persistence.xml file. | ||
EntityManagerFactory emf = Persistence.createEntityManagerFactory("student-management-system"); | ||
EntityManager em = emf.createEntityManager(); | ||
|
||
// We can find a record in the database for a given id using the find method. | ||
// for the find method we have to provide our entity class and the id. | ||
Student student = em.find(Student.class, studentId); | ||
|
||
// If there is no record found with the provided student id, then we throw a NoSuchElement | ||
// exception. | ||
if (student == null) { | ||
throw new NoSuchElementException("NO DATA FOUND WITH THE ID " + studentId); | ||
} | ||
|
||
// If everything worked fine, return the result. | ||
return student; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...mmenter-java/src/test/java/com/google/cloud/sqlcommenter/grpc/backend/domain/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2019 Google LLC | ||
// | ||
// 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.google.cloud.sqlcommenter.grpc.backend.domain; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@ToString | ||
@Entity | ||
@Table(name = "student") | ||
public class Student { | ||
@Id private String studentId; | ||
private String name; | ||
private Integer age; | ||
} |
Oops, something went wrong.